List: use inactive selection background/foreground if list is not focused

This commit is contained in:
Karl Tauber
2019-09-13 13:12:05 +02:00
parent f61782d87f
commit 09d84a12ae
5 changed files with 68 additions and 0 deletions

View File

@@ -16,7 +16,15 @@
package com.formdev.flatlaf.ui;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicListUI;
@@ -25,12 +33,64 @@ import javax.swing.plaf.basic.BasicListUI;
*
* TODO document used UI defaults of superclass
*
* @uiDefault List.selectionInactiveBackground Color
* @uiDefault List.selectionInactiveForeground Color
*
* @author Karl Tauber
*/
public class FlatListUI
extends BasicListUI
{
protected Color selectionInactiveBackground;
protected Color selectionInactiveForeground;
public static ComponentUI createUI( JComponent c ) {
return new FlatListUI();
}
@Override
protected void installDefaults() {
super.installDefaults();
selectionInactiveBackground = UIManager.getColor( "List.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "List.selectionInactiveForeground" );
}
@Override
protected void uninstallDefaults() {
super.uninstallDefaults();
selectionInactiveBackground = null;
selectionInactiveForeground = null;
}
/**
* Same as super.paintCell(), but uses inactive selection background/foreground if list is not focused.
*/
@Override
@SuppressWarnings( { "rawtypes", "unchecked" } )
protected void paintCell( Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer,
ListModel dataModel, ListSelectionModel selModel, int leadIndex )
{
Object value = dataModel.getElementAt( row );
boolean hasFocus = list.hasFocus();
boolean cellHasFocus = hasFocus && (row == leadIndex);
boolean isSelected = selModel.isSelectedIndex( row );
// get renderer component
Component rendererComponent = cellRenderer.getListCellRendererComponent(
list, value, row, isSelected, cellHasFocus );
// apply inactive selection background/foreground if list is not focused
if( isSelected && !hasFocus ) {
if( rendererComponent.getBackground() == list.getSelectionBackground() )
rendererComponent.setBackground( selectionInactiveBackground );
if( rendererComponent.getForeground() == list.getSelectionForeground() )
rendererComponent.setForeground( selectionInactiveForeground );
}
// paint renderer
rendererPane.paintComponent( g, rendererComponent, list,
rowBounds.x, rowBounds.y, rowBounds.width, rowBounds.height, true );
}
}

View File

@@ -24,6 +24,8 @@
@foreground=bbbbbb
@selectionBackground=4B6EAF
@selectionForeground=@foreground
@selectionInactiveBackground=0D293E
@selectionInactiveForeground=@foreground
@disabledText=777777
@textComponentBackground=45494A
@cellFocusColor=000000

View File

@@ -165,6 +165,8 @@ List.border=1,0,1,0
List.cellNoFocusBorder=1,6,1,6
List.focusCellHighlightBorder=1,6,1,6,@cellFocusColor
List.focusSelectedCellHighlightBorder=1,6,1,6,@cellFocusColor
List.selectionInactiveBackground=@selectionInactiveBackground
List.selectionInactiveForeground=@selectionInactiveForeground
#---- Menu ----

View File

@@ -24,6 +24,8 @@
@foreground=000000
@selectionBackground=4A6EB7
@selectionForeground=ffffff
@selectionInactiveBackground=d4d4d4
@selectionInactiveForeground=@foreground
@disabledText=999999
@textComponentBackground=ffffff
@cellFocusColor=000000

View File

@@ -18,6 +18,8 @@
@background=ccffcc
@selectionBackground=00aa00
@selectionInactiveBackground=888888
@selectionInactiveForeground=ffffff
@textComponentBackground=ffffff
@cellFocusColor=ff0000
@icon=afafaf