List and Tree: paint cell focus indicator (black rectangle) only if more than one item is selected

This commit is contained in:
Karl Tauber
2019-12-21 21:46:36 +01:00
parent 27c439d728
commit 56c161fea0
5 changed files with 103 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ FlatLaf Change Log
- Updated colors in "Flat Light" and "Flat IntelliJ" themes with colors from - Updated colors in "Flat Light" and "Flat IntelliJ" themes with colors from
"IntelliJ Light Theme", which provides blue coloring that better match "IntelliJ Light Theme", which provides blue coloring that better match
platform colors. platform colors.
- List and Tree: Paint cell focus indicator (black rectangle) only if more than
one item is selected.
- Fixed link color (in HTML text) and separator color in IntelliJ platform - Fixed link color (in HTML text) and separator color in IntelliJ platform
themes. themes.

View File

@@ -0,0 +1,72 @@
/*
* Copyright 2019 FormDev Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.formdev.flatlaf.ui;
import java.awt.Component;
import java.awt.Graphics;
import javax.swing.JList;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
* Cell border for {@link javax.swing.DefaultListCellRenderer}.
*
* Uses separate cell margins from UI defaults to allow easy customizing.
*
* @author Karl Tauber
*/
public class FlatListCellBorder
extends FlatLineBorder
{
protected FlatListCellBorder() {
super( UIManager.getInsets( "List.cellMargins" ), UIManager.getColor( "List.cellFocusColor" ) );
}
//---- class Default ------------------------------------------------------
public static class Default
extends FlatListCellBorder
{
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
// do not paint border
}
}
//---- class Focused ------------------------------------------------------
public static class Focused
extends FlatListCellBorder
{
}
//---- class Selected -----------------------------------------------------
public static class Selected
extends FlatListCellBorder
{
@Override
public void paintBorder( Component c, Graphics g, int x, int y, int width, int height ) {
// paint border only if exactly one item is selected
JList<?> list = (JList<?>) SwingUtilities.getAncestorOfClass( JList.class, c );
if( list != null && list.getMinSelectionIndex() == list.getMaxSelectionIndex() )
return;
super.paintBorder( c, g, x, y, width, height );
}
}
}

View File

@@ -52,6 +52,11 @@ import javax.swing.plaf.basic.BasicListUI;
* @uiDefault List.selectionInactiveBackground Color * @uiDefault List.selectionInactiveBackground Color
* @uiDefault List.selectionInactiveForeground Color * @uiDefault List.selectionInactiveForeground Color
* *
* <!-- FlatListCellBorder -->
*
* @uiDefault List.cellMargins Insets
* @uiDefault List.cellFocusColor Color
*
* @author Karl Tauber * @author Karl Tauber
*/ */
public class FlatListUI public class FlatListUI

View File

@@ -85,6 +85,7 @@ public class FlatTreeUI
protected Color selectionForeground; protected Color selectionForeground;
protected Color selectionInactiveBackground; protected Color selectionInactiveBackground;
protected Color selectionInactiveForeground; protected Color selectionInactiveForeground;
protected Color selectionBorderColor;
public static ComponentUI createUI( JComponent c ) { public static ComponentUI createUI( JComponent c ) {
return new FlatTreeUI(); return new FlatTreeUI();
@@ -100,6 +101,7 @@ public class FlatTreeUI
selectionForeground = UIManager.getColor( "Tree.selectionForeground" ); selectionForeground = UIManager.getColor( "Tree.selectionForeground" );
selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" ); selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" );
selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" ); selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" );
selectionBorderColor = UIManager.getColor( "Tree.selectionBorderColor" );
// scale // scale
int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 ); int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 );
@@ -119,6 +121,7 @@ public class FlatTreeUI
selectionForeground = null; selectionForeground = null;
selectionInactiveBackground = null; selectionInactiveBackground = null;
selectionInactiveForeground = null; selectionInactiveForeground = null;
selectionBorderColor = null;
} }
/** /**
@@ -157,11 +160,26 @@ public class FlatTreeUI
rendererComponent.setForeground( selectionInactiveForeground ); rendererComponent.setForeground( selectionInactiveForeground );
} }
// remove selection border if exactly one item is selected
Color oldBorderSelectionColor = null;
if( isSelected && hasFocus &&
tree.getMinSelectionRow() == tree.getMaxSelectionRow() &&
rendererComponent instanceof DefaultTreeCellRenderer )
{
DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) rendererComponent;
if( renderer.getBorderSelectionColor() == selectionBorderColor ) {
oldBorderSelectionColor = renderer.getBorderSelectionColor();
renderer.setBorderSelectionColor( null );
}
}
// paint renderer // paint renderer
rendererPane.paintComponent( g, rendererComponent, tree, bounds.x, bounds.y, bounds.width, bounds.height, true ); rendererPane.paintComponent( g, rendererComponent, tree, bounds.x, bounds.y, bounds.width, bounds.height, true );
// restore background selection color // restore background selection color and border selection color
if( oldBackgroundSelectionColor != null ) if( oldBackgroundSelectionColor != null )
((DefaultTreeCellRenderer)rendererComponent).setBackgroundSelectionColor( oldBackgroundSelectionColor ); ((DefaultTreeCellRenderer)rendererComponent).setBackgroundSelectionColor( oldBackgroundSelectionColor );
if( oldBorderSelectionColor != null )
((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor );
} }
} }

View File

@@ -196,9 +196,11 @@ HelpButton.disabledQuestionMarkColor=@@CheckBox.icon.disabledCheckmarkColor
#---- List ---- #---- List ----
List.border=1,0,1,0 List.border=1,0,1,0
List.cellNoFocusBorder=1,6,1,6 List.cellMargins=1,6,1,6
List.focusCellHighlightBorder=1,6,1,6,@cellFocusColor List.cellFocusColor=@cellFocusColor
List.focusSelectedCellHighlightBorder=1,6,1,6,@cellFocusColor List.cellNoFocusBorder=com.formdev.flatlaf.ui.FlatListCellBorder$Default
List.focusCellHighlightBorder=com.formdev.flatlaf.ui.FlatListCellBorder$Focused
List.focusSelectedCellHighlightBorder=com.formdev.flatlaf.ui.FlatListCellBorder$Selected
List.selectionInactiveBackground=@selectionInactiveBackground List.selectionInactiveBackground=@selectionInactiveBackground
List.selectionInactiveForeground=@selectionInactiveForeground List.selectionInactiveForeground=@selectionInactiveForeground