client property "JComponent.focusOwner" added to allow customizing detection of focused state (issue #185)

This commit is contained in:
Karl Tauber
2021-01-30 17:54:47 +01:00
parent d4e5d0be45
commit 212ae90401
2 changed files with 30 additions and 0 deletions

View File

@@ -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.
* <p>
* May be useful in special cases for custom components.
* <p>
* Use a {@link java.util.function.Predicate} that receives the component as parameter:
* <pre>{@code
* myComponent.putClientProperty( "JComponent.focusOwner",
* (Predicate) c -> {
* return ...; // check here
* } );
* }</pre>
* <p>
* <strong>Component</strong> {@link javax.swing.JComponent}<br>
* <strong>Value type</strong> {@link java.util.function.Predicate&lt;javax.swing.JComponent&gt;
*/
String COMPONENT_FOCUS_OWNER = "JComponent.focusOwner";
//---- Popup --------------------------------------------------------------
/**

View File

@@ -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<JComponent>)value).test( (JComponent) c ) &&
keyboardFocusManager.getActiveWindow() == SwingUtilities.windowForComponent( c );
}
}
return keyboardFocusManager.getPermanentFocusOwner() == c &&
keyboardFocusManager.getActiveWindow() == SwingUtilities.windowForComponent( c );
}