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 )
{
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 // apply inactive selection background/foreground if list is not focused
if( isSelected && !hasFocus ) { Color oldSelectionBackground = list.getSelectionBackground();
if( rendererComponent.getBackground() == list.getSelectionBackground() ) Color oldSelectionForeground = list.getSelectionForeground();
rendererComponent.setBackground( selectionInactiveBackground ); list.setSelectionBackground( selectionInactiveBackground );
if( rendererComponent.getForeground() == list.getSelectionForeground() ) list.setSelectionForeground( selectionInactiveForeground );
rendererComponent.setForeground( selectionInactiveForeground );
}
int x = rowBounds.x; super.paint( g, c );
int width = rowBounds.width;
// reduce width to preferred width in JFileChooser list.setSelectionBackground( oldSelectionBackground );
if( Boolean.TRUE.equals( list.getClientProperty( "List.isFileList" ) ) ) { list.setSelectionForeground( oldSelectionForeground );
int w = Math.min( width, rendererComponent.getPreferredSize().width + 4 ); } else
if( !list.getComponentOrientation().isLeftToRight() ) super.paint( g, c );
x += (width - w);
width = w;
}
// paint renderer
rendererPane.paintComponent( g, rendererComponent, list,
x, rowBounds.y, width, rowBounds.height, true );
} }
} }