From dd866309bc50a679bb412d623e8ebdc50b28a603 Mon Sep 17 00:00:00 2001 From: weisj Date: Fri, 10 Jul 2020 23:46:19 +0200 Subject: [PATCH] Use correct background color on DefaultTreeCellRenderer. --- .../weisj/darklaf/ui/cell/CellUtil.java | 19 ++++++++++++++++++- .../ui/cell/hint/CellHintPopupListener.java | 4 ++-- .../table/renderer/DarkTableCellEditor.java | 2 +- .../ui/tree/DarkTreeCellRendererDelegate.java | 1 + core/src/test/java/ui/table/TableDemo.java | 6 +++--- .../weisj/darklaf/util/PropertyUtil.java | 15 +++++++-------- 6 files changed, 32 insertions(+), 15 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 13ab03ce..4e29bf6e 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 @@ -27,7 +27,10 @@ package com.github.weisj.darklaf.ui.cell; import java.awt.*; import javax.swing.*; +import javax.swing.plaf.UIResource; +import javax.swing.tree.DefaultTreeCellRenderer; +import com.github.weisj.darklaf.graphics.ColorWrapper; import com.github.weisj.darklaf.ui.list.DarkListUI; import com.github.weisj.darklaf.ui.table.DarkTableUI; import com.github.weisj.darklaf.ui.table.renderer.IconWrapper; @@ -334,7 +337,14 @@ public class CellUtil { public static void setupTreeBackground(final Component comp, final JTree parent, final boolean selected, final int row) { - setupBackground(comp, getTreeBackground(comp, parent, selected, row)); + Color bg = getTreeBackground(comp, parent, selected, row); + setupBackground(comp, bg); + if (comp instanceof DefaultTreeCellRenderer) { + Color c = comp.getBackground(); + if (c == null || c instanceof UIResource || c instanceof NonUIResourceColorWrapper) { + comp.setBackground(new NonUIResourceColorWrapper(bg)); + } + } } public static Color getTreeBackground(final JTree tree, final boolean selected, final int row) { @@ -554,4 +564,11 @@ public class CellUtil { Rectangle rect = table.getVisibleRect(); return table.rowAtPoint(rect.getLocation()); } + + private static class NonUIResourceColorWrapper extends ColorWrapper { + + public NonUIResourceColorWrapper(final Color color) { + super(color); + } + } } diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/cell/hint/CellHintPopupListener.java b/core/src/main/java/com/github/weisj/darklaf/ui/cell/hint/CellHintPopupListener.java index e1062bbf..1ae17e51 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/cell/hint/CellHintPopupListener.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/cell/hint/CellHintPopupListener.java @@ -95,8 +95,8 @@ public class CellHintPopupListener extends MouseInputAd if (visibleBounds.contains(p)) { final Component comp = cellContainer.getEffectiveCellRendererComponent(index, isEditing); final Dimension prefSize = isEditing - ? comp.getBounds().getSize() - : comp.getPreferredSize(); + ? comp.getBounds().getSize() + : comp.getPreferredSize(); if (!(visibleBounds.width >= prefSize.width && visibleBounds.height >= prefSize.height)) { Point popupLocation = cellContainer.getComponent().getLocationOnScreen(); Rectangle rect = calculatePopupBounds(cellBounds, visibleBounds, !isEditing); diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellEditor.java b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellEditor.java index 3d0f7549..fa143d0e 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellEditor.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/table/renderer/DarkTableCellEditor.java @@ -130,7 +130,7 @@ public class DarkTableCellEditor extends DefaultCellEditor { } } } - } catch (ParseException ignore) { } + } catch (ParseException ignore) {} } public boolean shouldSelectCell(final EventObject anEvent) { diff --git a/core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java b/core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java index 59304108..de8375c8 100644 --- a/core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java +++ b/core/src/main/java/com/github/weisj/darklaf/ui/tree/DarkTreeCellRendererDelegate.java @@ -74,6 +74,7 @@ public class DarkTreeCellRendererDelegate extends TreeCellRendererDelegate imple renderer = super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, isFocused); } CellUtil.setupTreeForeground(renderer, tree, selected); + CellUtil.setupTreeBackground(renderer, tree, selected, row); return renderer; } diff --git a/core/src/test/java/ui/table/TableDemo.java b/core/src/test/java/ui/table/TableDemo.java index c8825459..27b64581 100644 --- a/core/src/test/java/ui/table/TableDemo.java +++ b/core/src/test/java/ui/table/TableDemo.java @@ -31,14 +31,14 @@ import javax.swing.*; import javax.swing.table.JTableHeader; import javax.swing.table.TableCellEditor; +import ui.ComponentDemo; +import ui.DemoPanel; + import com.github.weisj.darklaf.ui.table.DarkTableUI; import com.github.weisj.darklaf.ui.table.renderer.DarkTableCellEditor; import com.github.weisj.darklaf.util.PropertyKey; import com.github.weisj.darklaf.util.PropertyUtil; -import ui.ComponentDemo; -import ui.DemoPanel; - public class TableDemo implements ComponentDemo { public static void main(final String[] args) { diff --git a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java index 38a2eca8..7dcba59d 100644 --- a/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java +++ b/utils/src/main/java/com/github/weisj/darklaf/util/PropertyUtil.java @@ -38,18 +38,12 @@ public class PropertyUtil { public static void installBackground(final Component component, final Color color) { if (component == null || color == null) return; - Color c = component.getBackground(); - if (c == null || c instanceof UIResource) { - component.setBackground(color); - } + component.setBackground(chooseColor(component.getBackground(), color)); } public static void installForeground(final Component component, final Color color) { if (component == null || color == null) return; - Color c = component.getForeground(); - if (c == null || c instanceof UIResource) { - component.setForeground(color); - } + component.setForeground(chooseColor(component.getForeground(), color)); } public static void installBorder(final JComponent component, final Border border) { @@ -187,4 +181,9 @@ public class PropertyUtil { .map(type::cast) .collect(Collectors.toList()); } + + public static Color chooseColor(final Color currentColor, final Color newColor) { + if (currentColor == null || currentColor instanceof UIResource) return newColor; + return currentColor; + } }