📄 propertysheettable.java
字号:
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();
}
}
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();
}
}
}
}
private static class CategoryVisibilityToggle 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();
}
}
}
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;
}
private static class CellBorder implements Border {
private Color background = UIManager.getColor(PANEL_BACKGROUND_COLOR_KEY);
private int indentWidth; // space before hotspot
private boolean showToggle;
private boolean toggleState;
private Icon expandedIcon = (Icon) UIManager.get(TREE_EXPANDED_ICON_KEY);
private Icon collapsedIcon = (Icon) UIManager.get(TREE_COLLAPSED_ICON_KEY);
private Insets insets = new Insets(1, 0, 1, 1);
private boolean isProperty;
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(background);
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;
}
}
/**
* A {@link TableCellRenderer} for property names.
*/
private static class NameRenderer extends DefaultTableCellRenderer {
private Color background;
private Color foreground;
private Color propertyBackground;
private Color propertyForeground;
private Color selectedBackground;
private Color selectedForeground;
private CellBorder border;
public NameRenderer() {
this(UIManager.getColor(PANEL_BACKGROUND_COLOR_KEY), UIManager.getColor(
PANEL_BACKGROUND_COLOR_KEY).darker());
}
public NameRenderer(Color background, Color foreground) {
this.background = background;
this.foreground = foreground;
this.propertyBackground = UIManager.getColor(TABLE_BACKGROUND_COLOR_KEY);
this.propertyForeground = UIManager.getColor(TABLE_FOREGROUND_COLOR_KEY);
this.selectedBackground = UIManager.getColor(TABLE_SELECTED_BACKGROUND_COLOR_KEY);
this.selectedForeground = UIManager.getColor(TABLE_SELECTED_FOREGROUND_COLOR_KEY);
border = new CellBorder();
}
private Color getForeground(boolean isProperty, boolean isSelected) {
return (isProperty ? (isSelected ? selectedForeground : propertyForeground) : foreground);
}
private Color getBackground(boolean isProperty, boolean isSelected) {
return (isProperty ? (isSelected ? selectedBackground : propertyBackground) : background);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setBorder(border);
PropertySheetTableModel.Item item = (Item) value;
// 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());
setFont(getFont().deriveFont(item.isProperty() ? Font.PLAIN : Font.BOLD));
setText(item.getName());
return this;
}
}
private static class WrappedRenderer implements TableCellRenderer {
private TableCellRenderer renderer;
public WrappedRenderer(TableCellRenderer renderer) {
this.renderer = renderer;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
return renderer.getTableCellRendererComponent(table, value, false, false,
row, column);
}
}
private static class CategoryValueRenderer extends DefaultTableCellRenderer {
private Color background;
private Color foreground;
public CategoryValueRenderer() {
this(UIManager.getColor(PANEL_BACKGROUND_COLOR_KEY), UIManager.getColor(
PANEL_BACKGROUND_COLOR_KEY).darker().darker().darker());
}
public CategoryValueRenderer(Color background, Color foreground) {
this.background = background;
this.foreground = foreground;
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setBackground(background);
setForeground(foreground);
setText("");
return this;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -