Skip to content

Commit 3628a03

Browse files
committed
introduced FlatUIAction
1 parent 6ce2198 commit 3628a03

3 files changed

Lines changed: 70 additions & 22 deletions

File tree

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatMenuBarUI.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.beans.PropertyChangeListener;
2828
import java.util.Map;
2929
import java.util.concurrent.atomic.AtomicBoolean;
30-
import javax.swing.AbstractAction;
3130
import javax.swing.ActionMap;
3231
import javax.swing.BoxLayout;
3332
import javax.swing.JComponent;
@@ -149,7 +148,7 @@ protected void installKeyboardActions() {
149148
map = new ActionMapUIResource();
150149
SwingUtilities.replaceUIActionMap( menuBar, map );
151150
}
152-
map.put( "takeFocus", new TakeFocus() );
151+
map.put( "takeFocus", new TakeFocus( "takeFocus" ) );
153152
}
154153

155154
/** @since 2 */
@@ -373,8 +372,12 @@ public void layoutContainer( Container target ) {
373372
* On other platforms, the popup of the first menu is shown.
374373
*/
375374
private static class TakeFocus
376-
extends AbstractAction
375+
extends FlatUIAction
377376
{
377+
public TakeFocus( String name ) {
378+
super( name );
379+
}
380+
378381
@Override
379382
public void actionPerformed( ActionEvent e ) {
380383
JMenuBar menuBar = (JMenuBar) e.getSource();

flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatTabbedPaneUI.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,10 +3520,8 @@ else if( direction == EAST || direction == SOUTH )
35203520
//---- class RunWithOriginalLayoutManagerDelegateAction -------------------
35213521

35223522
private static class RunWithOriginalLayoutManagerDelegateAction
3523-
implements Action
3523+
extends FlatUIAction
35243524
{
3525-
private final Action delegate;
3526-
35273525
static void install( ActionMap map, String key ) {
35283526
Action oldAction = map.get( key );
35293527
if( oldAction == null || oldAction instanceof RunWithOriginalLayoutManagerDelegateAction )
@@ -3533,24 +3531,9 @@ static void install( ActionMap map, String key ) {
35333531
}
35343532

35353533
private RunWithOriginalLayoutManagerDelegateAction( Action delegate ) {
3536-
this.delegate = delegate;
3537-
}
3538-
3539-
@Override
3540-
public Object getValue( String key ) {
3541-
return delegate.getValue( key );
3534+
super( delegate );
35423535
}
35433536

3544-
@Override
3545-
public boolean isEnabled() {
3546-
return delegate.isEnabled();
3547-
}
3548-
3549-
@Override public void putValue( String key, Object value ) {}
3550-
@Override public void setEnabled( boolean b ) {}
3551-
@Override public void addPropertyChangeListener( PropertyChangeListener listener ) {}
3552-
@Override public void removePropertyChangeListener( PropertyChangeListener listener ) {}
3553-
35543537
@Override
35553538
public void actionPerformed( ActionEvent e ) {
35563539
JTabbedPane tabbedPane = (JTabbedPane) e.getSource();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2023 FormDev Software GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.formdev.flatlaf.ui;
18+
19+
import java.beans.PropertyChangeListener;
20+
import javax.swing.Action;
21+
22+
/**
23+
* Base class for UI actions used in ActionMap.
24+
* (similar to class sun.swing.UIAction)
25+
*
26+
* @author Karl Tauber
27+
* @since 3.3
28+
*/
29+
public abstract class FlatUIAction
30+
implements Action
31+
{
32+
protected final String name;
33+
protected final Action delegate;
34+
35+
protected FlatUIAction( String name ) {
36+
this.name = name;
37+
this.delegate = null;
38+
}
39+
40+
protected FlatUIAction( Action delegate ) {
41+
this.name = null;
42+
this.delegate = delegate;
43+
}
44+
45+
@Override
46+
public Object getValue( String key ) {
47+
if( key == NAME && delegate == null )
48+
return name;
49+
return (delegate != null) ? delegate.getValue( key ) : null;
50+
}
51+
52+
@Override
53+
public boolean isEnabled() {
54+
return (delegate != null) ? delegate.isEnabled() : true;
55+
}
56+
57+
// do nothing in following methods because this class is immutable
58+
@Override public void putValue( String key, Object value ) {}
59+
@Override public void setEnabled( boolean b ) {}
60+
@Override public void addPropertyChangeListener( PropertyChangeListener listener ) {}
61+
@Override public void removePropertyChangeListener( PropertyChangeListener listener ) {}
62+
}

0 commit comments

Comments
 (0)