TabbedPane: fixed actions scrollTabsForwardAction and scrollTabsBackwardAction when used from outside (e.g. in NetBeans)

This commit is contained in:
Karl Tauber
2021-04-08 01:15:29 +02:00
parent 3b84314c45
commit 77fc564e70
3 changed files with 111 additions and 1 deletions

View File

@@ -58,6 +58,8 @@ import java.util.function.BiConsumer;
import java.util.function.IntConsumer;
import javax.accessibility.Accessible;
import javax.accessibility.AccessibleContext;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.ButtonModel;
import javax.swing.Icon;
import javax.swing.JButton;
@@ -490,6 +492,20 @@ public class FlatTabbedPaneUI
}
}
@Override
protected void installKeyboardActions() {
super.installKeyboardActions();
// get shared action map, used for all tabbed panes
ActionMap map = SwingUtilities.getUIActionMap( tabPane );
if( map != null ) {
// this is required for the case that those actions are used from outside
// (e.g. wheel tab scroller in NetBeans)
RunWithOriginalLayoutManagerDelegateAction.install( map, "scrollTabsForwardAction" );
RunWithOriginalLayoutManagerDelegateAction.install( map, "scrollTabsBackwardAction" );
}
}
private Handler getHandler() {
if( handler == null )
handler = new Handler();
@@ -2959,4 +2975,51 @@ public class FlatTabbedPaneUI
scrollBackwardButtonPrefSize = backwardButton.getPreferredSize();
}
}
//---- class RunWithOriginalLayoutManagerDelegateAction -------------------
private static class RunWithOriginalLayoutManagerDelegateAction
implements Action
{
private final Action delegate;
static void install( ActionMap map, String key ) {
Action oldAction = map.get( key );
if( oldAction == null || oldAction instanceof RunWithOriginalLayoutManagerDelegateAction )
return; // not found or already installed
map.put( key, new RunWithOriginalLayoutManagerDelegateAction( oldAction ) );
}
private RunWithOriginalLayoutManagerDelegateAction( Action delegate ) {
this.delegate = 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();
ComponentUI ui = tabbedPane.getUI();
if( ui instanceof FlatTabbedPaneUI ) {
((FlatTabbedPaneUI)ui).runWithOriginalLayoutManager( () -> {
delegate.actionPerformed( e );
} );
} else
delegate.actionPerformed( e );
}
}
}

View File

@@ -18,7 +18,10 @@ package com.formdev.flatlaf.testing;
import static com.formdev.flatlaf.FlatClientProperties.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
import javax.swing.border.*;
import com.formdev.flatlaf.FlatLaf;
@@ -418,6 +421,32 @@ public class FlatContainerTest
tabbedPane.setMaximumTabWidth( maximumTabWidth );
}
private void customWheelScrollingChanged() {
if( custoMouseWheelScroller != null ) {
for( FlatTabbedPane tabbedPane : allTabbedPanes )
tabbedPane.removeMouseWheelListener( custoMouseWheelScroller );
custoMouseWheelScroller = null;
}
if( customWheelScrollingCheckBox.isSelected() ) {
custoMouseWheelScroller = new MouseWheelListener() {
@Override
public void mouseWheelMoved( MouseWheelEvent e ) {
if( e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL ) {
JTabbedPane tabbedPane = (JTabbedPane) e.getComponent();
ActionMap actionMap = tabbedPane.getActionMap();
Action scrollAction = actionMap.get( (e.getWheelRotation() < 0)
? "scrollTabsBackwardAction" : "scrollTabsForwardAction" );
if( scrollAction != null && scrollAction.isEnabled() )
scrollAction.actionPerformed( new ActionEvent( tabbedPane, 0, "" ) );
}
}
};
for( FlatTabbedPane tabbedPane : allTabbedPanes )
tabbedPane.addMouseWheelListener( custoMouseWheelScroller );
}
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
JPanel panel9 = new JPanel();
@@ -475,6 +504,7 @@ public class FlatContainerTest
showTabSeparatorsCheckBox = new JCheckBox();
secondTabWiderCheckBox = new JCheckBox();
hideTabAreaWithOneTabCheckBox = new JCheckBox();
customWheelScrollingCheckBox = new JCheckBox();
CellConstraints cc = new CellConstraints();
//======== this ========
@@ -773,6 +803,11 @@ public class FlatContainerTest
hideTabAreaWithOneTabCheckBox.setText("Hide tab area with one tab");
hideTabAreaWithOneTabCheckBox.addActionListener(e -> hideTabAreaWithOneTabChanged());
tabbedPaneControlPanel.add(hideTabAreaWithOneTabCheckBox, "cell 1 10");
//---- customWheelScrollingCheckBox ----
customWheelScrollingCheckBox.setText("Custom wheel scrolling");
customWheelScrollingCheckBox.addActionListener(e -> customWheelScrollingChanged());
tabbedPaneControlPanel.add(customWheelScrollingCheckBox, "cell 2 10");
}
panel9.add(tabbedPaneControlPanel, cc.xywh(1, 11, 3, 1));
}
@@ -818,9 +853,11 @@ public class FlatContainerTest
private JCheckBox showTabSeparatorsCheckBox;
private JCheckBox secondTabWiderCheckBox;
private JCheckBox hideTabAreaWithOneTabCheckBox;
private JCheckBox customWheelScrollingCheckBox;
// JFormDesigner - End of variables declaration //GEN-END:variables
private FlatTabbedPane[] allTabbedPanes;
private MouseWheelListener custoMouseWheelScroller;
//---- enum TabPlacement --------------------------------------------------

View File

@@ -1,4 +1,4 @@
JFDML JFormDesigner: "7.0.2.0.298" Java: "15" encoding: "UTF-8"
JFDML JFormDesigner: "7.0.3.1.342" Java: "16" encoding: "UTF-8"
new FormModel {
contentType: "form/swing"
@@ -502,6 +502,16 @@ new FormModel {
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 1 10"
} )
add( new FormComponent( "javax.swing.JCheckBox" ) {
name: "customWheelScrollingCheckBox"
"text": "Custom wheel scrolling"
auxiliary() {
"JavaCodeGenerator.variableLocal": false
}
addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "customWheelScrollingChanged", false ) )
}, new FormLayoutConstraints( class net.miginfocom.layout.CC ) {
"value": "cell 2 10"
} )
}, new FormLayoutConstraints( class com.jgoodies.forms.layout.CellConstraints ) {
"gridY": 11
"gridWidth": 3