?? propertysheettable.java
字號(hào):
}
/**
* Helper method to lookup a cell renderer based on type.
* @param type the type for which a renderer should be found
* @return a renderer for the given object type
*/
private TableCellRenderer getCellRenderer(Class type) {
// try to create one from the factory
TableCellRenderer renderer = getRendererFactory().createTableCellRenderer(type);
// if that fails, recursively try again with the superclass
if (renderer == null && type != null)
renderer = getCellRenderer(type.getSuperclass());
// if that fails, just use the default Object renderer
if (renderer == null)
renderer = super.getDefaultRenderer(Object.class);
return renderer;
}
public final PropertySheetTableModel getSheetModel() {
return (PropertySheetTableModel) getModel();
}
/**
* Overriden
* <li>to prevent the cell focus rect to be painted
* <li>to disable ({@link Component#setEnabled(boolean)} the renderer if the
* Property is not editable
*/
public Component prepareRenderer(TableCellRenderer renderer, int row,
int column) {
Object value = getValueAt(row, column);
boolean isSelected = isCellSelected(row, column);
Component component = renderer.getTableCellRendererComponent(this, value,
isSelected, false, row, column);
PropertySheetTableModel.Item item = getSheetModel()
.getPropertySheetElement(row);
if (item.isProperty()) {
component.setEnabled(item.getProperty().isEditable());
}
return component;
}
/**
* Overriden to register a listener on the model. This listener ensures
* editing is cancelled when editing row is being changed.
*
* @see javax.swing.JTable#setModel(javax.swing.table.TableModel)
* @throws IllegalArgumentException
* if dataModel is not a {@link PropertySheetTableModel}
*/
public void setModel(TableModel newModel) {
if (!(newModel instanceof PropertySheetTableModel)) {
throw new IllegalArgumentException("dataModel must be of type "
+ PropertySheetTableModel.class.getName());
}
if (cancelEditing == null) {
cancelEditing = new CancelEditing();
}
TableModel oldModel = getModel();
if (oldModel != null) {
oldModel.removeTableModelListener(cancelEditing);
}
super.setModel(newModel);
newModel.addTableModelListener(cancelEditing);
// ensure the "value" column can not be resized
getColumnModel().getColumn(1).setResizable(false);
}
/**
* @see #setWantsExtraIndent(boolean)
*/
public boolean getWantsExtraIndent() {
return wantsExtraIndent;
}
/**
* By default, properties with children are painted with the same indent level
* as other properties and categories. When nested properties exist within the
* set of properties, the end-user might be confused by the category and
* property handles. Sets this property to true to add an extra indent level
* to properties.
*
* @param wantsExtraIndent
*/
public void setWantsExtraIndent(boolean wantsExtraIndent) {
this.wantsExtraIndent = wantsExtraIndent;
repaint();
}
/**
* Ensures the table uses the full height of its parent
* {@link javax.swing.JViewport}.
*/
public boolean getScrollableTracksViewportHeight() {
return getPreferredSize().height < getParent().getHeight();
}
/**
* Commits on-going cell editing
*/
public void commitEditing() {
TableCellEditor editor = getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
}
/**
* Cancels on-going cell editing
*/
public void cancelEditing() {
TableCellEditor editor = getCellEditor();
if (editor != null) {
editor.cancelCellEditing();
}
}
/**
* Cancels the cell editing if any update happens while modifying a value.
*/
private class CancelEditing implements TableModelListener {
public void tableChanged(TableModelEvent e) {
// in case the table changes for the following reasons:
// * the editing row has changed
// * the editing row was removed
// * all rows were changed
// * rows were added
//
// it is better to cancel the editing of the row as our editor
// may no longer be the right one. It happens when you play with
// the sorting while having the focus in one editor.
if (e.getType() == TableModelEvent.UPDATE) {
int first = e.getFirstRow();
int last = e.getLastRow();
int editingRow = PropertySheetTable.this.getEditingRow();
TableCellEditor editor = PropertySheetTable.this.getCellEditor();
if (editor != null && first <= editingRow && editingRow <= last) {
editor.cancelCellEditing();
}
}
}
}
/**
* Starts value cell editing even if value cell does not have the focus but
* only if row is selected.
*/
private static class StartEditingAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
JTable table = (JTable)e.getSource();
if (!table.hasFocus()) {
CellEditor cellEditor = table.getCellEditor();
if (cellEditor != null && !cellEditor.stopCellEditing()) { return; }
table.requestFocus();
return;
}
ListSelectionModel rsm = table.getSelectionModel();
int anchorRow = rsm.getAnchorSelectionIndex();
table.editCellAt(anchorRow, PropertySheetTableModel.VALUE_COLUMN);
Component editorComp = table.getEditorComponent();
if (editorComp != null) {
editorComp.requestFocus();
}
}
}
/**
* Toggles the state of a row between expanded/collapsed. Works only for rows
* with "toggle" knob.
*/
private class ToggleAction extends AbstractAction {
public void actionPerformed(ActionEvent e) {
int row = PropertySheetTable.this.getSelectedRow();
Item item = PropertySheetTable.this.getSheetModel()
.getPropertySheetElement(row);
item.toggle();
PropertySheetTable.this.addRowSelectionInterval(row, row);
}
public boolean isEnabled() {
int row = PropertySheetTable.this.getSelectedRow();
if (row != -1) {
Item item = PropertySheetTable.this.getSheetModel()
.getPropertySheetElement(row);
return item.hasToggle();
} else {
return false;
}
}
}
/**
* @see ToggleAction
*/
private static class ToggleMouseHandler extends MouseAdapter {
public void mouseReleased(MouseEvent event) {
PropertySheetTable table = (PropertySheetTable) event.getComponent();
int row = table.rowAtPoint(event.getPoint());
int column = table.columnAtPoint(event.getPoint());
if (row != -1 && column == 0) {
// if we clicked on an Item, see if we clicked on its hotspot
Item item = table.getSheetModel().getPropertySheetElement(row);
int x = event.getX() - getIndent(table, item);
if (x > 0 && x < HOTSPOT_SIZE)
item.toggle();
}
}
}
/**
* Calculates the required left indent for a given item, given its type and
* its hierarchy level.
*/
static int getIndent(PropertySheetTable table, Item item) {
int indent = 0;
if (item.isProperty()) {
// it is a property, it has no parent or a category, and no child
if ((item.getParent() == null || !item.getParent().isProperty())
&& !item.hasToggle()) {
indent = table.getWantsExtraIndent()?HOTSPOT_SIZE:0;
} else {
// it is a property with children
if (item.hasToggle()) {
indent = item.getDepth() * HOTSPOT_SIZE;
} else {
indent = (item.getDepth() + 1) * HOTSPOT_SIZE;
}
}
if (table.getSheetModel().getMode() == PropertySheet.VIEW_AS_CATEGORIES
&& table.getWantsExtraIndent()) {
indent += HOTSPOT_SIZE;
}
} else {
// category has no indent
indent = 0;
}
return indent;
}
/**
* Paints the border around the name cell. It handles the indent from the left
* side and the painting of the toggle knob.
*/
private static class CellBorder implements Border {
private int indentWidth; // space before hotspot
private boolean showToggle;
private boolean toggleState;
private Icon expandedIcon;
private Icon collapsedIcon;
private Insets insets = new Insets(1, 0, 1, 1);
private boolean isProperty;
public CellBorder() {
expandedIcon = (Icon)UIManager.get(TREE_EXPANDED_ICON_KEY);
collapsedIcon = (Icon)UIManager.get(TREE_COLLAPSED_ICON_KEY);
if (expandedIcon == null) {
expandedIcon = new ExpandedIcon();
}
if (collapsedIcon == null) {
collapsedIcon = new CollapsedIcon();
}
}
public void configure(PropertySheetTable table, Item item) {
isProperty = item.isProperty();
toggleState = item.isVisible();
showToggle = item.hasToggle();
indentWidth = getIndent(table, item);
insets.left = indentWidth + (showToggle?HOTSPOT_SIZE:0) + 2;;
}
public Insets getBorderInsets(Component c) {
return insets;
}
public void paintBorder(Component c, Graphics g, int x, int y, int width,
int height) {
if (!isProperty) {
Color oldColor = g.getColor();
g.setColor(c.getBackground());
g.fillRect(x, y, x + HOTSPOT_SIZE - 2, y + height);
g.setColor(oldColor);
}
if (showToggle) {
Icon drawIcon = (toggleState ? expandedIcon : collapsedIcon);
drawIcon.paintIcon(c, g,
x + indentWidth + (HOTSPOT_SIZE - 2 - drawIcon.getIconWidth()) / 2,
y + (height - drawIcon.getIconHeight()) / 2);
}
}
public boolean isBorderOpaque() {
return true;
}
}
private static class ExpandedIcon implements Icon {
public void paintIcon(Component c, Graphics g, int x, int y) {
Color backgroundColor = c.getBackground();
if (backgroundColor != null)
g.setColor(backgroundColor);
else g.setColor(Color.white);
g.fillRect(x, y, 8, 8);
g.setColor(Color.gray);
g.drawRect(x, y, 8, 8);
g.setColor(Color.black);
g.drawLine(x + 2, y + 4, x + (6), y + 4);
}
public int getIconWidth() {
return 9;
}
public int getIconHeight() {
return 9;
}
}
private static class CollapsedIcon extends ExpandedIcon {
public void paintIcon(Component c, Graphics g, int x, int y) {
super.paintIcon(c, g, x, y);
g.drawLine(x + 4, y + 2, x + 4, y + 6);
}
}
/**
* A {@link TableCellRenderer} for property names.
*/
private class NameRenderer extends DefaultTableCellRenderer {
private CellBorder border;
public NameRenderer() {
border = new CellBorder();
}
private Color getForeground(boolean isProperty, boolean isSelected) {
return (isProperty ? (isSelected ? selectedPropertyForeground : propertyForeground) :
(isSelected ? selectedCategoryForeground : categoryForeground));
}
private Color getBackground(boolean isProperty, boolean isSelected) {
return (isProperty ? (isSelected ? selectedPropertyBackground : propertyBackground) :
(isSelected ? selectedCategoryBackground : categoryBackground));
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, false, row, column);
PropertySheetTableModel.Item item = (Item) value;
// shortcut if we are painting the category column
if (column == PropertySheetTableModel.VALUE_COLUMN && !item.isProperty()) {
setBackground(getBackground(item.isProperty(), isSelected));
setText("");
return this;
}
setBorder(border);
// configure the border
border.configure((PropertySheetTable)table, item);
setBackground(getBackground(item.isProperty(), isSelected));
setForeground(getForeground(item.isProperty(), isSelected));
setEnabled(isSelected || !item.isProperty() ? true : item.getProperty().isEditable());
setText(item.getName());
return this;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -