📄 minitable.java
字号:
check.setHorizontalAlignment(JLabel.CENTER);
tc.setCellEditor(new DefaultCellEditor(check));
}
m_minWidth.add(new Integer(30));
}
// Date
else if (c == Timestamp.class)
{
tc.setCellRenderer(new VCellRenderer(DisplayType.Date));
if (readOnly)
tc.setCellEditor(new ROCellEditor());
else
tc.setCellEditor(new MiniCellEditor(c));
m_minWidth.add(new Integer(30));
}
// Amount
else if (c == BigDecimal.class)
{
tc.setCellRenderer(new VCellRenderer(DisplayType.Amount));
if (readOnly)
{
tc.setCellEditor(new ROCellEditor());
m_minWidth.add(new Integer(70));
}
else
{
tc.setCellEditor(new MiniCellEditor(c));
m_minWidth.add(new Integer(80));
}
}
// Number
else if (c == Double.class)
{
tc.setCellRenderer(new VCellRenderer(DisplayType.Number));
if (readOnly)
{
tc.setCellEditor(new ROCellEditor());
m_minWidth.add(new Integer(70));
}
else
{
tc.setCellEditor(new MiniCellEditor(c));
m_minWidth.add(new Integer(80));
}
}
// Integer
else if (c == Integer.class)
{
tc.setCellRenderer(new VCellRenderer(DisplayType.Integer));
if (readOnly)
tc.setCellEditor(new ROCellEditor());
else
tc.setCellEditor(new MiniCellEditor(c));
m_minWidth.add(new Integer(30));
}
// String
else
{
tc.setCellRenderer(new VCellRenderer(DisplayType.String));
if (readOnly)
tc.setCellEditor(new ROCellEditor());
else
tc.setCellEditor(new MiniCellEditor(String.class));
m_minWidth.add(new Integer(30));
}
// log.fine( "Renderer=" + tc.getCellRenderer().toString() + ", Editor=" + tc.getCellEditor().toString());
} // setColumnClass
/**
* Clear Table Content
* @param no number of rows
*/
public void setRowCount (int no)
{
if (getModel() instanceof DefaultTableModel)
{
DefaultTableModel model = (DefaultTableModel)getModel();
model.setRowCount(no);
// log.config( "MiniTable.setRowCount", "rows=" + getRowCount() + ", cols=" + getColumnCount());
}
else
throw new IllegalArgumentException("Model must be instance of DefaultTableModel");
} // setRowCount
/**************************************************************************
* Load Table from ResultSet - The ResultSet is not closed
*
* @param rs ResultSet with the column layout defined in prepareTable
*/
public void loadTable(ResultSet rs)
{
if (m_layout == null)
throw new UnsupportedOperationException("Layout not defined");
// Clear Table
setRowCount(0);
//
try
{
while (rs.next())
{
int row = getRowCount();
setRowCount(row+1);
int colOffset = 1; // columns start with 1
for (int col = 0; col < m_layout.length; col++)
{
Object data = null;
Class c = m_layout[col].getColClass();
int colIndex = col + colOffset;
if (c == IDColumn.class)
data = new IDColumn(rs.getInt(colIndex));
else if (c == Boolean.class)
data = new Boolean(rs.getString(colIndex).equals("Y"));
else if (c == Timestamp.class)
data = rs.getTimestamp(colIndex);
else if (c == BigDecimal.class)
data = rs.getBigDecimal(colIndex);
else if (c == Double.class)
data = new Double(rs.getDouble(colIndex));
else if (c == Integer.class)
data = new Integer(rs.getInt(colIndex));
else if (c == KeyNamePair.class)
{
String display = rs.getString(colIndex);
int key = rs.getInt(colIndex+1);
data = new KeyNamePair(key, display);
colOffset++;
}
else
{
String s = rs.getString(colIndex);
if (s != null)
data = s.trim(); // problems with NCHAR
}
// store
setValueAt(data, row, col);
// log.fine( "r=" + row + ", c=" + col + " " + m_layout[col].getColHeader(),
// "data=" + data.toString() + " " + data.getClass().getName() + " * " + m_table.getCellRenderer(row, col));
}
}
}
catch (SQLException e)
{
log.log(Level.SEVERE, "", e);
}
autoSize();
log.config("Row(rs)=" + getRowCount());
} // loadTable
/**
* Load Table from Object Array
* @param pos array of POs
*/
public void loadTable(PO[] pos)
{
if (m_layout == null)
throw new UnsupportedOperationException("Layout not defined");
// Clear Table
setRowCount(0);
//
for (int i = 0; i < pos.length; i++)
{
PO myPO = pos[i];
int row = getRowCount();
setRowCount(row+1);
for (int col = 0; col < m_layout.length; col++)
{
String columnName = m_layout[col].getColSQL();
Object data = myPO.get_Value(columnName);
if (data != null)
{
Class c = m_layout[col].getColClass();
if (c == IDColumn.class)
data = new IDColumn(((Integer)data).intValue());
else if (c == Double.class)
data = new Double(((BigDecimal)data).doubleValue());
}
// store
setValueAt(data, row, col);
}
}
autoSize();
log.config("Row(array)=" + getRowCount());
} // loadTable
/**
* Get the key of currently selected row based on layout defined in prepareTable
* @return ID if key
*/
public Integer getSelectedRowKey()
{
if (m_layout == null)
throw new UnsupportedOperationException("Layout not defined");
int row = getSelectedRow();
if (row != -1 && p_keyColumnIndex != -1)
{
Object data = getModel().getValueAt(row, p_keyColumnIndex);
if (data instanceof IDColumn)
data = ((IDColumn)data).getRecord_ID();
if (data instanceof Integer)
return (Integer)data;
}
return null;
} // getSelectedRowKey
/**************************************************************************
* Get Layout
* @return Array of ColumnInfo
*/
public ColumnInfo[] getLayoutInfo()
{
return m_layout;
} // getLayout
/**
* Set Single Selection
* @param multiSelection multiple selections
*/
public void setMultiSelection (boolean multiSelection)
{
m_multiSelection = multiSelection;
} // setMultiSelection
/**
* Single Selection Table
* @return true if multiple rows can be selected
*/
public boolean isMultiSelection()
{
return m_multiSelection;
} // isMultiSelection
/**
* Set the Column to determine the color of the row (based on model index)
* @param modelIndex model index
*/
public void setColorColumn (int modelIndex)
{
m_colorColumnIndex = modelIndex;
} // setColorColumn
/**
* Set ColorColumn comparison criteria
* @param dataCompare data
*/
public void setColorCompare (Object dataCompare)
{
m_colorDataCompare = dataCompare;
} //
/**
* Get ColorCode for Row.
* <pre>
* If numerical value in compare column is
* negative = -1,
* positive = 1,
* otherwise = 0
* If Timestamp
* </pre>
* @param row row
* @return color code
*/
public int getColorCode (int row)
{
if (m_colorColumnIndex == -1)
return 0;
Object data = getModel().getValueAt(row, m_colorColumnIndex);
int cmp = 0;
// We need to have a Number
if (data == null)
return 0;
try
{
if (data instanceof Timestamp)
{
if (m_colorDataCompare == null || !(m_colorDataCompare instanceof Timestamp))
m_colorDataCompare = new Timestamp(System.currentTimeMillis());
cmp = ((Timestamp)m_colorDataCompare).compareTo((Timestamp)data);
}
else
{
if (m_colorDataCompare == null || !(m_colorDataCompare instanceof BigDecimal))
m_colorDataCompare = Env.ZERO;
if (!(data instanceof BigDecimal))
data = new BigDecimal(data.toString());
cmp = ((BigDecimal)m_colorDataCompare).compareTo((BigDecimal)data);
}
}
catch (Exception e)
{
return 0;
}
if (cmp > 0)
return -1;
if (cmp < 0)
return 1;
return 0;
} // getColorCode
} // MiniTable
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -