Tree: fill cell background if DefaultTreeCellRenderer.setBackgroundNonSelectionColor(Color) was used (issue #322)

This commit is contained in:
Karl Tauber
2021-05-12 15:45:36 +02:00
parent 03b42749cd
commit 38a3a0768d
3 changed files with 56 additions and 14 deletions

View File

@@ -44,6 +44,9 @@ FlatLaf Change Log
internal frames in dock. internal frames in dock.
- PopupFactory: Fixed occasional `NullPointerException` in - PopupFactory: Fixed occasional `NullPointerException` in
`FlatPopupFactory.fixToolTipLocation()`. (issue #305) `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 - IntelliJ Themes: Fixed background colors of DesktopPane and DesktopIcon in all
themes. themes.
- Native window decorations: Fixed occasional double window title bar when - Native window decorations: Fixed occasional double window title bar when

View File

@@ -107,6 +107,8 @@ public class FlatTreeUI
protected boolean wideSelection; protected boolean wideSelection;
protected boolean showCellFocusIndicator; protected boolean showCellFocusIndicator;
private Color defaultCellNonSelectionBackground;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTreeUI(); return new FlatTreeUI();
} }
@@ -125,6 +127,8 @@ public class FlatTreeUI
wideSelection = UIManager.getBoolean( "Tree.wideSelection" ); wideSelection = UIManager.getBoolean( "Tree.wideSelection" );
showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" ); showCellFocusIndicator = UIManager.getBoolean( "Tree.showCellFocusIndicator" );
defaultCellNonSelectionBackground = UIManager.getColor( "Tree.textBackground" );
// scale // scale
int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 ); int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 );
if( rowHeight > 0 ) if( rowHeight > 0 )
@@ -144,6 +148,8 @@ public class FlatTreeUI
selectionInactiveBackground = null; selectionInactiveBackground = null;
selectionInactiveForeground = null; selectionInactiveForeground = null;
selectionBorderColor = null; selectionBorderColor = null;
defaultCellNonSelectionBackground = null;
} }
@Override @Override
@@ -306,24 +312,24 @@ public class FlatTreeUI
} }
} else { } else {
// non-wide selection // non-wide selection
int xOffset = 0; paintCellBackground( g, rendererComponent, bounds );
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 );
} }
// this is actually not necessary because renderer should always set color // this is actually not necessary because renderer should always set color
// before painting, but doing anyway to avoid any side effect (in bad renderers) // before painting, but doing anyway to avoid any side effect (in bad renderers)
g.setColor( oldColor ); 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 // paint renderer
@@ -337,6 +343,22 @@ public class FlatTreeUI
((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor ); ((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. * Checks whether dropping on a row.
* See DefaultTreeCellRenderer.getTreeCellRendererComponent(). * See DefaultTreeCellRenderer.getTreeCellRendererComponent().

View File

@@ -1200,10 +1200,27 @@ public class FlatComponents2Test
extends DefaultTreeCellRenderer extends DefaultTreeCellRenderer
{ {
public TestDefaultTreeCellRenderer() { public TestDefaultTreeCellRenderer() {
setBackgroundNonSelectionColor( Color.red );
setBackgroundSelectionColor( Color.green ); setBackgroundSelectionColor( Color.green );
setTextSelectionColor( Color.blue ); 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 ------------------------------------ //---- class TestLabelTreeCellRenderer ------------------------------------