Skip to content

Commit

Permalink
Tree: fill cell background if `DefaultTreeCellRenderer.setBackgroundN…
Browse files Browse the repository at this point in the history
…onSelectionColor(Color)` was used (issue #322)
  • Loading branch information
DevCharly committed May 12, 2021
1 parent 03b4274 commit 38a3a07
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ FlatLaf Change Log
internal frames in dock.
- PopupFactory: Fixed occasional `NullPointerException` in
`FlatPopupFactory.fixToolTipLocation()`. (issue #305)
- Tree: Fill cell background if
`DefaultTreeCellRenderer.setBackgroundNonSelectionColor(Color)` was used.
(issue #322)
- IntelliJ Themes: Fixed background colors of DesktopPane and DesktopIcon in all
themes.
- Native window decorations: Fixed occasional double window title bar when
Expand Down
48 changes: 35 additions & 13 deletions flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ public class FlatTreeUI
protected boolean wideSelection;
protected boolean showCellFocusIndicator;

private Color defaultCellNonSelectionBackground;

public static ComponentUI createUI( JComponent c ) {
return new FlatTreeUI();
}
Expand All @@ -125,6 +127,8 @@ protected void installDefaults() {
wideSelection = UIManager.getBoolean( "Tree.wideSelection" );
showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" );

defaultCellNonSelectionBackground = UIManager.getColor( "Tree.textBackground" );

// scale
int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 );
if( rowHeight > 0 )
Expand All @@ -144,6 +148,8 @@ protected void uninstallDefaults() {
selectionInactiveBackground = null;
selectionInactiveForeground = null;
selectionBorderColor = null;

defaultCellNonSelectionBackground = null;
}

@Override
Expand Down Expand Up @@ -306,24 +312,24 @@ protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectan
}
} else {
// non-wide selection
int xOffset = 0;
int imageOffset = 0;

if( rendererComponent instanceof JLabel ) {
JLabel label = (JLabel) rendererComponent;
Icon icon = label.getIcon();
imageOffset = (icon != null && label.getText() != null)
? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 )
: 0;
xOffset = label.getComponentOrientation().isLeftToRight() ? imageOffset : 0;
}

g.fillRect( bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height );
paintCellBackground( g, rendererComponent, bounds );
}

// this is actually not necessary because renderer should always set color
// before painting, but doing anyway to avoid any side effect (in bad renderers)
g.setColor( oldColor );
} else {
// paint cell background if DefaultTreeCellRenderer.getBackgroundNonSelectionColor() is set
if( rendererComponent instanceof DefaultTreeCellRenderer ) {
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent;
Color bg = renderer.getBackgroundNonSelectionColor();
if( bg != null && !bg.equals( defaultCellNonSelectionBackground ) ) {
Color oldColor = g.getColor();
g.setColor( bg );
paintCellBackground( g, rendererComponent, bounds );
g.setColor( oldColor );
}
}
}

// paint renderer
Expand All @@ -337,6 +343,22 @@ protected void paintRow( Graphics g, Rectangle clipBounds, Insets insets, Rectan
((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor );
}

private void paintCellBackground( Graphics g, Component rendererComponent, Rectangle bounds ) {
int xOffset = 0;
int imageOffset = 0;

if( rendererComponent instanceof JLabel ) {
JLabel label = (JLabel) rendererComponent;
Icon icon = label.getIcon();
imageOffset = (icon != null && label.getText() != null)
? icon.getIconWidth() + Math.max( label.getIconTextGap() - 1, 0 )
: 0;
xOffset = label.getComponentOrientation().isLeftToRight() ? imageOffset : 0;
}

g.fillRect( bounds.x + xOffset, bounds.y, bounds.width - imageOffset, bounds.height );
}

/**
* Checks whether dropping on a row.
* See DefaultTreeCellRenderer.getTreeCellRendererComponent().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1200,10 +1200,27 @@ private static class TestDefaultTreeCellRenderer
extends DefaultTreeCellRenderer
{
public TestDefaultTreeCellRenderer() {
setBackgroundNonSelectionColor( Color.red );
setBackgroundSelectionColor( Color.green );
setTextSelectionColor( Color.blue );
}

@Override
public Component getTreeCellRendererComponent( JTree tree, Object value, boolean sel, boolean expanded,
boolean leaf, int row, boolean hasFocus )
{
Color nonSelectionBg = null;
Color nonSelectionFg = null;
switch( String.valueOf( value ) ) {
case "blue": nonSelectionFg = Color.blue; break;
case "red": nonSelectionFg = Color.red; break;
case "yellow": nonSelectionBg = Color.yellow; break;
case "violet": nonSelectionBg = Color.magenta; break;
}
setBackgroundNonSelectionColor( nonSelectionBg );
setTextNonSelectionColor( (nonSelectionFg != null) ? nonSelectionFg : UIManager.getColor( "Tree.textForeground" ) );

return super.getTreeCellRendererComponent( tree, value, sel, expanded, leaf, row, hasFocus );
}
}

//---- class TestLabelTreeCellRenderer ------------------------------------
Expand Down

0 comments on commit 38a3a07

Please sign in to comment.