diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java index a7faf572..8366895d 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java @@ -16,10 +16,18 @@ package com.formdev.flatlaf.ui; +import java.awt.Color; +import java.awt.Component; +import java.awt.Graphics; +import java.awt.Insets; +import java.awt.Rectangle; import javax.swing.JComponent; import javax.swing.LookAndFeel; +import javax.swing.UIManager; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.basic.BasicTreeUI; +import javax.swing.tree.DefaultTreeCellRenderer; +import javax.swing.tree.TreePath; import com.formdev.flatlaf.util.UIScale; /** @@ -27,13 +35,22 @@ import com.formdev.flatlaf.util.UIScale; * * TODO document used UI defaults of superclass * - * @uiDefault Tree.border Border + * @uiDefault Tree.border Border + * @uiDefault Tree.selectionBackground Color + * @uiDefault Tree.selectionForeground Color + * @uiDefault Tree.selectionInactiveBackground Color + * @uiDefault Tree.selectionInactiveForeground Color * * @author Karl Tauber */ public class FlatTreeUI extends BasicTreeUI { + protected Color selectionBackground; + protected Color selectionForeground; + protected Color selectionInactiveBackground; + protected Color selectionInactiveForeground; + public static ComponentUI createUI( JComponent c ) { return new FlatTreeUI(); } @@ -44,6 +61,11 @@ public class FlatTreeUI LookAndFeel.installBorder( tree, "Tree.border" ); + selectionBackground = UIManager.getColor( "Tree.selectionBackground" ); + selectionForeground = UIManager.getColor( "Tree.selectionForeground" ); + selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" ); + selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" ); + // scale int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 ); if( rowHeight > 0 ) @@ -57,5 +79,54 @@ public class FlatTreeUI super.uninstallDefaults(); LookAndFeel.uninstallBorder( tree ); + + selectionBackground = null; + selectionForeground = null; + selectionInactiveBackground = null; + selectionInactiveForeground = null; + } + + /** + * Same as super.paintRow(), but uses inactive selection background/foreground if tree is not focused. + */ + @Override + protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectangle bounds, TreePath path, int row, + boolean isExpanded, boolean hasBeenExpanded, boolean isLeaf ) + { + if( editingComponent != null && editingRow == row ) + return; + + boolean hasFocus = tree.hasFocus(); + boolean cellHasFocus = hasFocus && (row == getLeadSelectionRow()); + boolean isSelected = tree.isRowSelected( row ); + + // get renderer component + Component rendererComponent = currentCellRenderer.getTreeCellRendererComponent( tree, + path.getLastPathComponent(), isSelected, isExpanded, isLeaf, row, cellHasFocus ); + + // apply inactive selection background/foreground if tree is not focused + Color oldBackgroundSelectionColor = null; + if( isSelected && !hasFocus ) { + if( rendererComponent instanceof DefaultTreeCellRenderer ) { + DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent; + if( renderer.getBackgroundSelectionColor() == selectionBackground ) { + oldBackgroundSelectionColor = renderer.getBackgroundSelectionColor(); + renderer.setBackgroundSelectionColor( selectionInactiveBackground ); + } + } else { + if( rendererComponent.getBackground() == selectionBackground ) + rendererComponent.setBackground( selectionInactiveBackground ); + } + + if( rendererComponent.getForeground() == selectionForeground ) + rendererComponent.setForeground( selectionInactiveForeground ); + } + + // paint renderer + rendererPane.paintComponent( g, rendererComponent, tree, bounds.x, bounds.y, bounds.width, bounds.height, true ); + + // restore background selection color + if( oldBackgroundSelectionColor != null ) + ((DefaultTreeCellRenderer)rendererComponent).setBackgroundSelectionColor( oldBackgroundSelectionColor ); } } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 8d6cb4e7..2dc12421 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -388,6 +388,8 @@ ToolTip.border=2,6,2,6,@@Component.borderColor #---- Tree ---- Tree.border=1,1,1,1 +Tree.selectionInactiveBackground=@selectionInactiveBackground +Tree.selectionInactiveForeground=@selectionInactiveForeground Tree.textBackground=@@Tree.background Tree.selectionBorderColor=@cellFocusColor Tree.rendererMargins=1,2,1,2