📄 allblockingregionspanel.java
字号:
else if (index-1>=0)
regions.getSelectionModel().setSelectionInterval(index-1, index-1);
else
regions.getSelectionModel().clearSelection();
}
}
});
/**
* Constructs a new RegionTable
*/
public RegionTable() {
super(new RegionTableModel());
blockRenderer = new BlockingElementRenderer();
delete.setFocusable(false);
deleteRenderer = new ButtonCellEditor(delete);
setRowHeight(ROW_HEIGHT);
// Sets column size
getColumnModel().getColumn(0).setPreferredWidth(120);
getColumnModel().getColumn(1).setPreferredWidth(50);
getColumnModel().getColumn(2).setMaxWidth(20);
getColumnModel().getColumn(3).setMaxWidth(ROW_HEIGHT);
getColumnModel().setColumnSelectionAllowed(false);
}
/**
* Returns an appropriate renderer for the cell specified by this row and
* column. If the <code>TableColumn</code> for this column has a non-null
* renderer, returns that. If not, finds the class of the data in
* this column (using <code>getColumnClass</code>)
* and returns the default renderer for this type of data.
*
* @param row the row of the cell to render, where 0 is the first row
* @param column the column of the cell to render,
* where 0 is the first column
* @return the assigned renderer; if <code>null</code>
* returns the default renderer
* for this type of object
*/
public TableCellRenderer getCellRenderer(int row, int column) {
switch(column) {
case 0: return blockRenderer;
case 1: return gray;
case 2: return super.getDefaultRenderer(Boolean.class);
case 3: return deleteRenderer;
default: return null;
}
}
/**
* Returns an appropriate editor for the cell specified by
* <code>row</code> and <code>column</code>. If the
* <code>TableColumn</code> for this column has a non-null editor,
* returns that. If not, finds the class of the data in this
* column (using <code>getColumnClass</code>)
* and returns the default editor for this type of data.
*
* @param row the row of the cell to edit, where 0 is the first row
* @param column the column of the cell to edit,
* where 0 is the first column
* @return the editor for this cell;
* if <code>null</code> return the default editor for
* this type of cell
*/
public TableCellEditor getCellEditor(int row, int column) {
switch(column) {
case 0: return new BlockingTableEditor();
case 1: return super.getDefaultEditor(String.class);
case 2: return super.getDefaultEditor(Boolean.class);
case 3: return new ButtonCellEditor(delete);
default: return null;
}
}
}
protected class RegionTableModel extends AbstractTableModel {
/**
* Returns the number of columns in the model. A
* <code>JTable</code> uses this method to determine how many columns it
* should create and display by default.
*
* @return the number of columns in the model
* @see #getRowCount
*/
public int getColumnCount() {
return 4;
}
/**
* Returns a default name for the column using spreadsheet conventions:
* A, B, C, ... Z, AA, AB, etc. If <code>column</code> cannot be found,
* returns an empty string.
*
* @param column the column being queried
* @return a string containing the default name of <code>column</code>
*/
public String getColumnName(int column) {
switch(column) {
case 0: return "Name";
case 1: return "Capacity";
case 2: return "\u221e";
case 3: return "";
default: return null;
}
}
/**
* Returns the number of rows in the model. A
* <code>JTable</code> uses this method to determine how many rows it
* should display. This method should be quick, as it
* is called frequently during rendering.
*
* @return the number of rows in the model
* @see #getColumnCount
*/
public int getRowCount() {
return bd.getRegionKeys().size();
}
/**
* Returns the value for the cell at <code>columnIndex</code> and
* <code>rowIndex</code>.
*
* @param rowIndex the row whose value is to be queried
* @param columnIndex the column whose value is to be queried
* @return the value Object at the specified cell
*/
public Object getValueAt(int rowIndex, int columnIndex) {
// Try catch for concurrency problems when deleting regions
try {
Object key = bd.getRegionKeys().get(rowIndex);
switch(columnIndex) {
case 0: return key;
case 1:
if (bd.getRegionCustomerConstraint(key).intValue() > 0)
return bd.getRegionCustomerConstraint(key);
else
return "\u221e";
case 2:
return Boolean.valueOf(bd.getRegionCustomerConstraint(key).intValue() <= 0);
default: return null;
}
} catch (Exception e) {
return null;
}
}
/**
* This empty implementation is provided so users don't have to implement
* this method if their data model is not editable.
*
* @param aValue value to assign to cell
* @param rowIndex row of cell
* @param columnIndex column of cell
*/
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
Integer value;
Object key = bd.getRegionKeys().get(rowIndex);
switch (columnIndex) {
case (1):
try {
value = Integer.decode((String)aValue);
if (value.intValue() > 0)
bd.setRegionCustomerConstraint(key, value);
} catch (NumberFormatException e) {
// Do nothing
}
break;
case (2):
boolean val = ((Boolean)aValue).booleanValue();
if (val)
bd.setRegionCustomerConstraint(key, new Integer(-1));
else {
value = Defaults.getAsInteger("blockingMaxJobs");
if (value.intValue() <= 0)
value = new Integer(1);
bd.setRegionCustomerConstraint(key, value);
}
break;
}
repaint();
}
/**
* Cell is editable
*
* @param rowIndex the row being queried
* @param columnIndex the column being queried
* @return false
*/
public boolean isCellEditable(int rowIndex, int columnIndex) {
return columnIndex != 1 || getValueAt(rowIndex, 2).equals(Boolean.FALSE);
}
}
/**
* Inner class used to paint the blocking region's list elements
*/
private class BlockingElementRenderer implements TableCellRenderer {
/**
* Returns the component used for drawing the cell. This method is
* used to configure the renderer appropriately before drawing.
*
* @param table the <code>JTable</code> that is asking the
* renderer to draw; can be <code>null</code>
* @param value the value of the cell to be rendered. It is
* up to the specific renderer to interpret
* and draw the value. For example, if
* <code>value</code>
* is the string "true", it could be rendered as a
* string or it could be rendered as a check
* box that is checked. <code>null</code> is a
* valid value
* @param isSelected true if the cell is to be rendered with the
* selection highlighted; otherwise false
* @param hasFocus if true, render cell appropriately. For
* example, put a special border on the cell, if
* the cell can be edited, render in the color used
* to indicate editing
* @param row the row index of the cell being drawn. When
* drawing the header, the value of
* <code>row</code> is -1
* @param column the column index of the cell being drawn
*/
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JLabel label = new JLabel(bd.getRegionName(value), ImageLoader.loadImage("BlockingCombo"), JLabel.LEFT);
label.setOpaque(true);
label.setBorder(
new LineBorder(hasFocus ? Color.BLUE : Color.WHITE)
);
label.setBackground(
isSelected ? table.getSelectionBackground() : Color.WHITE
);
label.setForeground(
isSelected ? table.getSelectionForeground() : Color.BLACK
);
label.setFont(isSelected ?
label.getFont().deriveFont(Font.BOLD) :
label.getFont().deriveFont(Font.ROMAN_BASELINE));
return label; }
}
/**
* Custom editor for blocking region name as the table contains search's key and not name.
*/
private class BlockingTableEditor extends DefaultCellEditor {
JTextField field;
Object key; // Search's key for blocking region
/**
* Constructs a <code>BlockingTableEditor</code>.
*
*/
public BlockingTableEditor() {
super(new JTextField());
}
/**
* Sets an initial <code>value</code> for the editor. This will cause
* the editor to <code>stopEditing</code> and lose any partially
* edited value if the editor is editing when this method is called. <p>
* <p/>
* Returns the component that should be added to the client's
* <code>Component</code> hierarchy. Once installed in the client's
* hierarchy this component will then be able to draw and receive
* user input.
*
* @param table the <code>JTable</code> that is asking the
* editor to edit; can be <code>null</code>
* @param value the value of the cell to be edited; it is
* up to the specific editor to interpret
* and draw the value. For example, if value is
* the string "true", it could be rendered as a
* string or it could be rendered as a check
* box that is checked. <code>null</code>
* is a valid value
* @param isSelected true if the cell is to be rendered with
* highlighting
* @param row the row of the cell being edited
* @param column the column of the cell being edited
* @return the component for editing
*/
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
field = (JTextField) super.getTableCellEditorComponent(table, value, isSelected, row, column);
key = value;
field.setText(bd.getRegionName(key));
return field;
}
/**
* Returns the value contained in the editor. and sets name of the blocking region
*
* @return the value contained in the editor
*/
public Object getCellEditorValue() {
if (!field.getText().equals(""))
bd.setRegionName(key, field.getText());
return key;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -