From 212ae90401be3f4c8cef5c97c6ae596d4048c864 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Sat, 30 Jan 2021 17:54:47 +0100 Subject: [PATCH] client property "JComponent.focusOwner" added to allow customizing detection of focused state (issue #185) --- .../formdev/flatlaf/FlatClientProperties.java | 19 +++++++++++++++++++ .../com/formdev/flatlaf/ui/FlatUIUtils.java | 11 +++++++++++ 2 files changed, 30 insertions(+) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java index 17d16c40..d03e5ae2 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatClientProperties.java @@ -172,6 +172,25 @@ public interface FlatClientProperties */ String OUTLINE_WARNING = "warning"; + /** + * Specifies a callback that is invoked to check whether a component is permanent focus owner. + * Used to paint focus indicators. + *

+ * May be useful in special cases for custom components. + *

+ * Use a {@link java.util.function.Predicate} that receives the component as parameter: + *

{@code
+	 * myComponent.putClientProperty( "JComponent.focusOwner",
+	 *     (Predicate) c -> {
+	 *         return ...; // check here
+	 *     } );
+	 * }
+ *

+ * Component {@link javax.swing.JComponent}
+ * Value type {@link java.util.function.Predicate<javax.swing.JComponent> + */ + String COMPONENT_FOCUS_OWNER = "JComponent.focusOwner"; + //---- Popup -------------------------------------------------------------- /** diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java index 58693238..1f49eb93 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIUtils.java @@ -39,6 +39,7 @@ import java.awt.geom.Rectangle2D; import java.awt.geom.RoundRectangle2D; import java.util.IdentityHashMap; import java.util.WeakHashMap; +import java.util.function.Predicate; import java.util.function.Supplier; import javax.swing.JComponent; import javax.swing.JTable; @@ -175,8 +176,18 @@ public class FlatUIUtils * Returns whether the given component is the permanent focus owner and * is in the active window. Used to paint focus indicators. */ + @SuppressWarnings( "unchecked" ) public static boolean isPermanentFocusOwner( Component c ) { KeyboardFocusManager keyboardFocusManager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); + + if( c instanceof JComponent ) { + Object value = ((JComponent)c).getClientProperty( FlatClientProperties.COMPONENT_FOCUS_OWNER ); + if( value instanceof Predicate ) { + return ((Predicate)value).test( (JComponent) c ) && + keyboardFocusManager.getActiveWindow() == SwingUtilities.windowForComponent( c ); + } + } + return keyboardFocusManager.getPermanentFocusOwner() == c && keyboardFocusManager.getActiveWindow() == SwingUtilities.windowForComponent( c ); }