From b1ca895e55dd7453adfbb08b766c1267beda2e08 Mon Sep 17 00:00:00 2001 From: weisj Date: Tue, 3 Nov 2020 21:46:22 +0100 Subject: [PATCH] Fix color renderer/editor for tables. --- .../weisj/darklaf/ui/cell/CellUtil.java | 2 +- .../darklaf/ui/table/TableConstants.java | 1 + .../DarkColorTableCellRendererEditor.java | 14 +++++++------ .../table/renderer/DarkMultiCellEditor.java | 14 +++++++++++-- .../table/renderer/DarkTableCellRenderer.java | 13 +++++++++++- .../github/weisj/darklaf/util/ColorUtil.java | 21 ++++++++++++++++--- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java b/core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java index bce5c3a8..b830d34c 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/cell/CellUtil.java @@ -326,7 +326,7 @@ public class CellUtil { if (comp instanceof DefaultTreeCellRenderer) { Color c = comp.getBackground(); if (ColorUtil.canOverwriteColor(c)) { - comp.setBackground(ColorUtil.stripUIResource(bg)); + comp.setBackground(ColorUtil.stripUIResource(bg, true)); } } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java index 4729f6a5..0c1545f7 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/TableConstants.java @@ -42,6 +42,7 @@ public interface TableConstants extends CellConstants { String KEY_VERTICAL_LINES = "showVerticalLines"; String KEY_IS_FILE_LIST = "Table.isFileList"; String KEY_IS_PRINT_MODE = "Table.printMode"; + String KEY_CELL_VALUE_DETERMINES_EDITOR_CLASS = "Table.cellDeterminesClass"; static boolean isBooleanRenderingEnabled(final JTable table) { return PropertyUtil.getBooleanProperty(table, DarkTableUI.KEY_RENDER_BOOLEAN_AS_CHECKBOX); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java index a543ac52..fcc39a6f 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkColorTableCellRendererEditor.java @@ -29,15 +29,17 @@ import javax.swing.*; import javax.swing.table.TableCellEditor; import javax.swing.table.TableCellRenderer; +import com.github.weisj.darklaf.util.ColorUtil; + /** @author Jannis Weis */ public class DarkColorTableCellRendererEditor extends AbstractCellEditor implements TableCellRenderer, TableCellEditor { - private final JLabel label; + private final JComponent colorComp; private Color savedColor; public DarkColorTableCellRendererEditor() { - label = new JLabel(); - label.setOpaque(true); + colorComp = new JPanel(); + colorComp.setOpaque(true); } @Override @@ -57,14 +59,14 @@ public class DarkColorTableCellRendererEditor extends AbstractCellEditor impleme changeColor(color); } }); - return label; + return colorComp; } } private void changeColor(final Color color) { if (color != null) { savedColor = color; - label.setBackground(color); + colorComp.setBackground(ColorUtil.stripUIResource(color, false)); } } @@ -85,6 +87,6 @@ public class DarkColorTableCellRendererEditor extends AbstractCellEditor impleme public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { changeColor((Color) value); - return label; + return colorComp; } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java index 6ee6b0f7..c3a0f544 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkMultiCellEditor.java @@ -28,14 +28,18 @@ import javax.swing.*; import javax.swing.table.TableCellEditor; import com.github.weisj.darklaf.delegate.TableCellEditorDelegate; +import com.github.weisj.darklaf.ui.table.TableConstants; import com.github.weisj.darklaf.util.LazyValue; +import com.github.weisj.darklaf.util.PropertyUtil; public class DarkMultiCellEditor extends TableCellEditorDelegate { - private final LazyValue numberEditor = + private final LazyValue numberEditor = new LazyValue<>(() -> new DarkTableCellEditor(new JSpinner())); - private final LazyValue dateEditor = + private final LazyValue dateEditor = new LazyValue<>(() -> new DarkTableCellEditor(new JSpinner(new SpinnerDateModel()))); + private final LazyValue colorEditor = + new LazyValue<>(DarkColorTableCellRendererEditor::new); private TableCellEditor currentEditor; public DarkMultiCellEditor() { @@ -51,11 +55,17 @@ public class DarkMultiCellEditor extends TableCellEditorDelegate { private TableCellEditor getEditor(final JTable table, final Object value, final int column) { Class columnClass = table.getColumnClass(column); + if (value != null + && PropertyUtil.getBooleanProperty(table, TableConstants.KEY_CELL_VALUE_DETERMINES_EDITOR_CLASS)) { + columnClass = value.getClass(); + } if (columnClass != null && columnClass.isInstance(value)) { if (Number.class.isAssignableFrom(columnClass)) { return numberEditor.get(); } else if (Date.class.isAssignableFrom(columnClass)) { return dateEditor.get(); + } else if (Color.class.isAssignableFrom(columnClass)) { + return colorEditor.get(); } } return null; diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java index 64cf9811..11bd6dfa 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellRenderer.java @@ -26,13 +26,24 @@ import java.awt.*; import javax.swing.*; import javax.swing.table.DefaultTableCellRenderer; +import com.github.weisj.darklaf.util.ColorUtil; + /** @author Jannis Weis */ public class DarkTableCellRenderer extends DefaultTableCellRenderer { @Override public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) { - Component component = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); + setBackground(null); + Object effectiveValue = value; + if (value instanceof Color) { + effectiveValue = ""; + } + Component component = + super.getTableCellRendererComponent(table, effectiveValue, isSelected, hasFocus, row, column); + if (value instanceof Color) { + component.setBackground(ColorUtil.stripUIResource((Color) value, false)); + } int horizontalAlignment = table.getComponentOrientation().isLeftToRight() ? SwingConstants.LEFT : SwingConstants.RIGHT; diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java index b561058f..8cfa802c 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/ColorUtil.java @@ -89,11 +89,19 @@ public final class ColorUtil { } public static boolean canOverwriteColor(final Color c) { - return c == null || c instanceof UIResource || c instanceof NonUIResourceColorWrapper; + return c == null || c instanceof UIResource || c instanceof NonUIResourceOverwritableColorWrapper; } - public static Color stripUIResource(final Color c) { - return c instanceof UIResource ? new NonUIResourceColorWrapper(c) : c; + public static Color stripUIResource(final Color c, final boolean canOverwrite) { + if (c instanceof UIResource) { + if (canOverwrite) { + return new NonUIResourceOverwritableColorWrapper(c); + } else { + return new NonUIResourceColorWrapper(c); + } + } else { + return c; + } } public static Color removeAlpha(final Color color) { @@ -131,4 +139,11 @@ public final class ColorUtil { super(color); } } + + public static class NonUIResourceOverwritableColorWrapper extends NonUIResourceColorWrapper { + + public NonUIResourceOverwritableColorWrapper(final Color color) { + super(color); + } + } }