List: use same mechanism as in FlatTableUI for inactive selection background/foreground

This commit is contained in:
Karl Tauber
2019-09-13 15:37:06 +02:00
parent 1cd30d42ad
commit 0acd633d3d

View File

@@ -17,13 +17,8 @@
package com.formdev.flatlaf.ui; package com.formdev.flatlaf.ui;
import java.awt.Color; import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics; import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent; import javax.swing.JComponent;
import javax.swing.ListCellRenderer;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager; import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI; import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicListUI; import javax.swing.plaf.basic.BasicListUI;
@@ -64,44 +59,20 @@ public class FlatListUI
selectionInactiveForeground = null; selectionInactiveForeground = null;
} }
/**
* Same as super.paintCell(), but uses inactive selection background/foreground if list is not focused.
*/
@Override @Override
@SuppressWarnings( { "rawtypes", "unchecked" } ) public void paint( Graphics g, JComponent c ) {
protected void paintCell( Graphics g, int row, Rectangle rowBounds, ListCellRenderer cellRenderer, if( !list.hasFocus() ) {
ListModel dataModel, ListSelectionModel selModel, int leadIndex ) // apply inactive selection background/foreground if list is not focused
{ Color oldSelectionBackground = list.getSelectionBackground();
Object value = dataModel.getElementAt( row ); Color oldSelectionForeground = list.getSelectionForeground();
boolean hasFocus = list.hasFocus(); list.setSelectionBackground( selectionInactiveBackground );
boolean cellHasFocus = hasFocus && (row == leadIndex); list.setSelectionForeground( selectionInactiveForeground );
boolean isSelected = selModel.isSelectedIndex( row );
// get renderer component super.paint( g, c );
Component rendererComponent = cellRenderer.getListCellRendererComponent(
list, value, row, isSelected, cellHasFocus );
// apply inactive selection background/foreground if list is not focused list.setSelectionBackground( oldSelectionBackground );
if( isSelected && !hasFocus ) { list.setSelectionForeground( oldSelectionForeground );
if( rendererComponent.getBackground() == list.getSelectionBackground() ) } else
rendererComponent.setBackground( selectionInactiveBackground ); super.paint( g, c );
if( rendererComponent.getForeground() == list.getSelectionForeground() )
rendererComponent.setForeground( selectionInactiveForeground );
}
int x = rowBounds.x;
int width = rowBounds.width;
// reduce width to preferred width in JFileChooser
if( Boolean.TRUE.equals( list.getClientProperty( "List.isFileList" ) ) ) {
int w = Math.min( width, rendererComponent.getPreferredSize().width + 4 );
if( !list.getComponentOrientation().isLeftToRight() )
x += (width - w);
width = w;
}
// paint renderer
rendererPane.paintComponent( g, rendererComponent, list,
x, rowBounds.y, width, rowBounds.height, true );
} }
} }