mirror of
https://github.com/JFormDesigner/FlatLaf.git
synced 2026-02-10 22:17:13 -06:00
Tree: fill cell background if DefaultTreeCellRenderer.setBackgroundNonSelectionColor(Color) was used (issue #322)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
@@ -125,6 +127,8 @@ public class FlatTreeUI
|
||||
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 )
|
||||
@@ -144,6 +148,8 @@ public class FlatTreeUI
|
||||
selectionInactiveBackground = null;
|
||||
selectionInactiveForeground = null;
|
||||
selectionBorderColor = null;
|
||||
|
||||
defaultCellNonSelectionBackground = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -306,24 +312,24 @@ public class FlatTreeUI
|
||||
}
|
||||
} 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
|
||||
@@ -337,6 +343,22 @@ public class FlatTreeUI
|
||||
((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().
|
||||
|
||||
@@ -1200,10 +1200,27 @@ public class FlatComponents2Test
|
||||
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 ------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user