From 56c161fea068f3c1b43921708adbd9cbbcd07500 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 21 Dec 2019 21:46:36 +0100 Subject: [PATCH] List and Tree: paint cell focus indicator (black rectangle) only if more than one item is selected --- CHANGELOG.md | 2 + .../flatlaf/ui/FlatListCellBorder.java | 72 +++++++++++++++++++ .../com/formdev/flatlaf/ui/FlatListUI.java | 5 ++ .../com/formdev/flatlaf/ui/FlatTreeUI.java | 20 +++++- .../com/formdev/flatlaf/FlatLaf.properties | 8 ++- 5 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java diff --git a/CHANGELOG.md b/CHANGELOG.md index a3d75adf..e9c96fbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ FlatLaf Change Log - Updated colors in "Flat Light" and "Flat IntelliJ" themes with colors from "IntelliJ Light Theme", which provides blue coloring that better match 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 themes. diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java new file mode 100644 index 00000000..823a448a --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListCellBorder.java @@ -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 ); + } + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java index 1e60de21..c5efd11f 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatListUI.java @@ -52,6 +52,11 @@ import javax.swing.plaf.basic.BasicListUI; * @uiDefault List.selectionInactiveBackground Color * @uiDefault List.selectionInactiveForeground Color * + * + * + * @uiDefault List.cellMargins Insets + * @uiDefault List.cellFocusColor Color + * * @author Karl Tauber */ public class FlatListUI diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java index 2bf47c93..89ce08cf 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTreeUI.java @@ -85,6 +85,7 @@ public class FlatTreeUI protected Color selectionForeground; protected Color selectionInactiveBackground; protected Color selectionInactiveForeground; + protected Color selectionBorderColor; public static ComponentUI createUI( JComponent c ) { return new FlatTreeUI(); @@ -100,6 +101,7 @@ public class FlatTreeUI selectionForeground = UIManager.getColor( "Tree.selectionForeground" ); selectionInactiveBackground = UIManager.getColor( "Tree.selectionInactiveBackground" ); selectionInactiveForeground = UIManager.getColor( "Tree.selectionInactiveForeground" ); + selectionBorderColor = UIManager.getColor( "Tree.selectionBorderColor" ); // scale int rowHeight = FlatUIUtils.getUIInt( "Tree.rowHeight", 16 ); @@ -119,6 +121,7 @@ public class FlatTreeUI selectionForeground = null; selectionInactiveBackground = null; selectionInactiveForeground = null; + selectionBorderColor = null; } /** @@ -157,11 +160,26 @@ public class FlatTreeUI 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 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 ) ((DefaultTreeCellRenderer)rendererComponent).setBackgroundSelectionColor( oldBackgroundSelectionColor ); + if( oldBorderSelectionColor != null ) + ((DefaultTreeCellRenderer)rendererComponent).setBorderSelectionColor( oldBorderSelectionColor ); } } diff --git a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties index 1f848c1d..256e7c0b 100644 --- a/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties +++ b/flatlaf-core/src/main/resources/com/formdev/flatlaf/FlatLaf.properties @@ -196,9 +196,11 @@ HelpButton.disabledQuestionMarkColor=@@CheckBox.icon.disabledCheckmarkColor #---- List ---- 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.cellMargins=1,6,1,6 +List.cellFocusColor=@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.selectionInactiveForeground=@selectionInactiveForeground