From 8f161b4b5ae834bba148a23727c9b6b37fe0381d Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Mon, 5 Feb 2024 19:03:29 +0100 Subject: [PATCH] introduced FlatUIAction --- .../com/formdev/flatlaf/ui/FlatMenuBarUI.java | 20 +++--- .../formdev/flatlaf/ui/FlatTabbedPaneUI.java | 21 +------ .../com/formdev/flatlaf/ui/FlatUIAction.java | 62 +++++++++++++++++++ 3 files changed, 74 insertions(+), 29 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIAction.java diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java index 4cdb44fa..36698073 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java @@ -27,7 +27,6 @@ import java.awt.event.ActionEvent; import java.beans.PropertyChangeListener; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import javax.swing.AbstractAction; import javax.swing.ActionMap; import javax.swing.BoxLayout; import javax.swing.JComponent; @@ -39,7 +38,6 @@ import javax.swing.MenuElement; import javax.swing.MenuSelectionManager; import javax.swing.SwingUtilities; import javax.swing.UIManager; -import javax.swing.plaf.ActionMapUIResource; import javax.swing.plaf.ComponentUI; import javax.swing.plaf.UIResource; import javax.swing.plaf.basic.BasicMenuBarUI; @@ -144,12 +142,10 @@ public class FlatMenuBarUI protected void installKeyboardActions() { super.installKeyboardActions(); + // get shared action map, used for all menu bars ActionMap map = SwingUtilities.getUIActionMap( menuBar ); - if( map == null ) { - map = new ActionMapUIResource(); - SwingUtilities.replaceUIActionMap( menuBar, map ); - } - map.put( "takeFocus", new TakeFocus() ); + if( map != null && !(map.get( "takeFocus" ) instanceof TakeFocusAction) ) + map.put( "takeFocus", new TakeFocusAction( "takeFocus" ) ); } /** @since 2 */ @@ -365,16 +361,20 @@ public class FlatMenuBarUI } } - //---- class TakeFocus ---------------------------------------------------- + //---- class TakeFocusAction ---------------------------------------------- /** * Activates the menu bar and shows mnemonics. * On Windows, the popup of the first menu is not shown. * On other platforms, the popup of the first menu is shown. */ - private static class TakeFocus - extends AbstractAction + private static class TakeFocusAction + extends FlatUIAction { + TakeFocusAction( String name ) { + super( name ); + } + @Override public void actionPerformed( ActionEvent e ) { JMenuBar menuBar = (JMenuBar) e.getSource(); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java index 4a202f86..afa242a6 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java @@ -3985,10 +3985,8 @@ debug*/ //---- class RunWithOriginalLayoutManagerDelegateAction ------------------- private static class RunWithOriginalLayoutManagerDelegateAction - implements Action + extends FlatUIAction { - private final Action delegate; - static void install( ActionMap map, String key ) { Action oldAction = map.get( key ); if( oldAction == null || oldAction instanceof RunWithOriginalLayoutManagerDelegateAction ) @@ -3998,24 +3996,9 @@ debug*/ } private RunWithOriginalLayoutManagerDelegateAction( Action delegate ) { - this.delegate = delegate; + super( delegate ); } - @Override - public Object getValue( String key ) { - return delegate.getValue( key ); - } - - @Override - public boolean isEnabled() { - return delegate.isEnabled(); - } - - @Override public void putValue( String key, Object value ) {} - @Override public void setEnabled( boolean b ) {} - @Override public void addPropertyChangeListener( PropertyChangeListener listener ) {} - @Override public void removePropertyChangeListener( PropertyChangeListener listener ) {} - @Override public void actionPerformed( ActionEvent e ) { JTabbedPane tabbedPane = (JTabbedPane) e.getSource(); diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIAction.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIAction.java new file mode 100644 index 00000000..0ba151dc --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatUIAction.java @@ -0,0 +1,62 @@ +/* + * Copyright 2024 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.beans.PropertyChangeListener; +import javax.swing.Action; + +/** + * Base class for UI actions used in ActionMap. + * (similar to class sun.swing.UIAction) + * + * @author Karl Tauber + * @since 3.4 + */ +public abstract class FlatUIAction + implements Action +{ + protected final String name; + protected final Action delegate; + + protected FlatUIAction( String name ) { + this.name = name; + this.delegate = null; + } + + protected FlatUIAction( Action delegate ) { + this.name = null; + this.delegate = delegate; + } + + @Override + public Object getValue( String key ) { + if( key == NAME && delegate == null ) + return name; + return (delegate != null) ? delegate.getValue( key ) : null; + } + + @Override + public boolean isEnabled() { + return (delegate != null) ? delegate.isEnabled() : true; + } + + // do nothing in following methods because this class is immutable + @Override public void putValue( String key, Object value ) {} + @Override public void setEnabled( boolean b ) {} + @Override public void addPropertyChangeListener( PropertyChangeListener listener ) {} + @Override public void removePropertyChangeListener( PropertyChangeListener listener ) {} +}