From f758a9b3437a272349e1b9d982685825effe3b28 Mon Sep 17 00:00:00 2001 From: weisj Date: Mon, 17 Feb 2020 23:59:04 +0100 Subject: [PATCH] Added table demo. Fixed insets for combobox and spinner when used as table cell editors. Signed-off-by: weisj --- .../darklaf/ui/spinner/DarkSpinnerBorder.java | 2 +- .../darklaf/ui/spinner/DarkSpinnerUI.java | 10 +- .../darklaf/ui/table/DarkTableCellEditor.java | 10 +- .../weisj/darklaf/ui/table/DarkTableUI.java | 5 +- .../darklaf/properties/ui/comboBox.properties | 2 +- core/src/test/java/ui/table/TableDemo.java | 131 ++++++++++++++++++ 6 files changed, 153 insertions(+), 7 deletions(-) create mode 100644 core/src/test/java/ui/table/TableDemo.java diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerBorder.java b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerBorder.java index bae16d44..0dd9142a 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerBorder.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerBorder.java @@ -119,7 +119,7 @@ public class DarkSpinnerBorder implements Border, UIResource { @Override public Insets getBorderInsets(final Component c) { if (DarkSpinnerUI.isTableCellEditor(c) || DarkSpinnerUI.isTreeCellEditor(c)) { - return new InsetsUIResource(cellInsets.top, insets.left, insets.bottom, insets.right); + return new InsetsUIResource(cellInsets.top, cellInsets.left, cellInsets.bottom, cellInsets.right); } return new InsetsUIResource(insets.top + borderSize, insets.left + borderSize, insets.bottom + borderSize, insets.right + borderSize); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java index 65414919..f19f3203 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/spinner/DarkSpinnerUI.java @@ -238,6 +238,7 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements PropertyChangeListe int width = c.getWidth(); int height = c.getHeight(); JComponent editor = spinner.getEditor(); + if (editorComponent != null) { editorComponent.setBackground(getBackground(c)); g.setColor(editorComponent.getBackground()); @@ -280,8 +281,13 @@ public class DarkSpinnerUI extends BasicSpinnerUI implements PropertyChangeListe Rectangle bounds = prevButton.getBounds(); boolean leftToRight = spinner.getComponentOrientation().isLeftToRight(); int off = leftToRight ? bounds.x : bounds.x + bounds.width; - Area rect = new Area(new RoundRectangle2D.Double(bSize - 1, bSize - 1, width - 2 * bSize + 2, height - 2 * bSize + 1, - arc, arc)); + Area rect; + if (!isTableCellEditor(spinner)) { + rect = new Area(new RoundRectangle2D.Double(bSize - 1, bSize - 1, width - 2 * bSize + 2, height - 2 * bSize + 1, + arc, arc)); + } else { + rect = new Area(new Rectangle(0, 0, width, height)); + } Area iconRect = new Area(new Rectangle(off, 0, width, height)); if (leftToRight) { rect.intersect(iconRect); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableCellEditor.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableCellEditor.java index d99d082f..d2783890 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableCellEditor.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableCellEditor.java @@ -220,8 +220,14 @@ public class DarkTableCellEditor extends DefaultCellEditor { if (editorComponent instanceof JComboBox) { ((JComboBox) editorComponent).removeAllItems(); - ((JComboBox) editorComponent).addItem(value); - ((JComboBox) editorComponent).setSelectedItem(value); + if (value != null && value.getClass().isArray()) { + for (Object obj : (Object[]) value) { + ((JComboBox) editorComponent).addItem(obj); + } + } else { + ((JComboBox) editorComponent).addItem(value); + ((JComboBox) editorComponent).setSelectedItem(value); + } } else if (editorComponent instanceof JSpinner) { Component rendererComp = table.getCellRenderer(row, column) .getTableCellRendererComponent(table, value, isSelected, false, row, column); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java index 8568f4dd..b46054f1 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/DarkTableUI.java @@ -62,7 +62,7 @@ public class DarkTableUI extends DarkTableUIBridge { if (oldVal instanceof Component) { Container oldUnwrapped = SwingUtilities.getUnwrappedParent((Component) oldVal); if ((oldUnwrapped instanceof JScrollPane) - && ((JComponent) oldUnwrapped).getBorder() instanceof UIResource) { + && ((JComponent) oldUnwrapped).getBorder() instanceof UIResource) { LookAndFeel.uninstallBorder((JComponent) oldUnwrapped); } } @@ -72,6 +72,9 @@ public class DarkTableUI extends DarkTableUIBridge { LookAndFeel.installBorder((JComponent) newUnwrapped, "Table.scrollPaneBorder"); } } + } else if ("componentOrientation".equals(key)) { + table.doLayout(); + table.repaint(); } }; protected Color selectionBackground; diff --git a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/comboBox.properties b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/comboBox.properties index 3e6a1edc..b895955e 100644 --- a/core/src/main/resources/com/github/weisj/darklaf/properties/ui/comboBox.properties +++ b/core/src/main/resources/com/github/weisj/darklaf/properties/ui/comboBox.properties @@ -40,7 +40,7 @@ ComboBox.arc = %arc ComboBox.borderThickness = %borderThickness ComboBox.selectionForeground = %textSelectionForeground ComboBox.insets = 2,2,2,2 -ComboBox.cellEditorInsets = 2,5,2,5 +ComboBox.cellEditorInsets = 2,1,2,1 ComboBox.buttonPad = 4 #Icons diff --git a/core/src/test/java/ui/table/TableDemo.java b/core/src/test/java/ui/table/TableDemo.java new file mode 100644 index 00000000..e9d26e21 --- /dev/null +++ b/core/src/test/java/ui/table/TableDemo.java @@ -0,0 +1,131 @@ +/* + * MIT License + * + * Copyright (c) 2020 Jannis Weis + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package ui.table; + +import com.github.weisj.darklaf.ui.table.DarkTableCellEditor; +import ui.ComponentDemo; +import ui.DemoPanel; + +import javax.swing.*; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableCellEditor; +import java.awt.*; +import java.util.concurrent.atomic.AtomicBoolean; + +public class TableDemo implements ComponentDemo { + + public static void main(final String[] args) { + ComponentDemo.showDemo(new TableDemo()); + } + + @Override + public JComponent createComponent() { + String[] columns = new String[]{ + "Id", "Name", "Hourly Rate", "Part Time", "Components" + }; + + Object[][] data = new Object[][]{ + {1, "John", 40.0, false, "Item"}, + {2, "Rambo", 70.0, false, 10}, + {3, "Zorro", 60.0, true, "cell"}, + }; + AtomicBoolean editable = new AtomicBoolean(true); + JTable table = new JTable(data, columns) { + TableCellEditor comboEditor = new DarkTableCellEditor(new JComboBox<>()); + TableCellEditor spinnerEditor = new DarkTableCellEditor(new JSpinner()); + + @Override + public boolean isCellEditable(final int row, final int column) { + return editable.get() && super.isCellEditable(row, column); + } + + @Override + public TableCellEditor getCellEditor(final int row, final int column) { + if (row == 0 && column == 4) { + return comboEditor; + } else if (row == 1 && column == 4) { + return spinnerEditor; + } else { + return super.getCellEditor(row, column); + } + } + }; + + JTableHeader header = table.getTableHeader(); + DemoPanel panel = new DemoPanel(new JScrollPane(table)); + + JPanel controlPanel = panel.getControls(); + controlPanel.setLayout(new GridLayout(4, 2)); + controlPanel.add(new JCheckBox("enabled") {{ + setSelected(table.isEnabled()); + addActionListener(e -> table.setEnabled(isSelected())); + }}); + controlPanel.add(new JCheckBox("editable") {{ + setSelected(editable.get()); + addActionListener(e -> editable.set(isSelected())); + }}); + controlPanel.add(new JCheckBox("horizontal lines") {{ + setSelected(table.getShowHorizontalLines()); + addActionListener(e -> table.setShowHorizontalLines(isSelected())); + }}); + controlPanel.add(new JCheckBox("vertical lines") {{ + setSelected(table.getShowVerticalLines()); + addActionListener(e -> table.setShowVerticalLines(isSelected())); + }}); + controlPanel.add(new JCheckBox("LeftToRight") {{ + setSelected(table.getComponentOrientation().isLeftToRight()); + addActionListener(e -> table.setComponentOrientation(isSelected() ? ComponentOrientation.LEFT_TO_RIGHT + : ComponentOrientation.RIGHT_TO_LEFT)); + }}); + controlPanel.add(new JCheckBox("reordering") {{ + setSelected(header.getReorderingAllowed()); + addActionListener(e -> header.setReorderingAllowed(isSelected())); + }}); + JRadioButton column = new JRadioButton("column selection allowed") {{ + setSelected(table.getColumnSelectionAllowed()); + addActionListener(e -> { + table.setColumnSelectionAllowed(isSelected()); + table.setRowSelectionAllowed(!isSelected()); + }); + }}; + JRadioButton row = new JRadioButton("row selection allowed") {{ + setSelected(table.getRowSelectionAllowed()); + addActionListener(e -> { + table.setRowSelectionAllowed(isSelected()); + table.setColumnSelectionAllowed(!isSelected()); + }); + }}; + ButtonGroup group = new ButtonGroup(); + group.add(column); + group.add(row); + controlPanel.add(column); + controlPanel.add(row); + return panel; + } + + @Override + public String getTitle() { + return "Table Demo"; + } +}