From 0d44ade6ea8df0f71a7f93b61c798d1e2bd7e853 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 7 Sep 2021 14:31:09 +0200 Subject: [PATCH] Theme Editor: preview improvements: - remember state of "enabled", "focused" and "buttonType" and sync it with all editors - added "_" button near "JMenuBar" label to test menu underline selection --- .../themeeditor/FlatThemeFileEditor.java | 4 +- .../flatlaf/themeeditor/FlatThemePreview.java | 23 +++++-- .../themeeditor/FlatThemePreviewAll.java | 62 ++++++++++++++++++- .../themeeditor/FlatThemePreviewAll.jfd | 14 +++++ .../themeeditor/FlatThemePreviewButtons.java | 59 ++++++++++++++---- 5 files changed, 139 insertions(+), 23 deletions(-) diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java index 03c35552..0ba5110f 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemeFileEditor.java @@ -826,14 +826,14 @@ class FlatThemeFileEditor state.put( KEY_WINDOW_BOUNDS, x + "," + y + ',' + width + ',' + height ); } - private static void putPrefsBoolean( Preferences prefs, String key, boolean value, boolean defaultValue ) { + static void putPrefsBoolean( Preferences prefs, String key, boolean value, boolean defaultValue ) { if( value != defaultValue ) prefs.putBoolean( key, value ); else prefs.remove( key ); } - private static void putPrefsString( Preferences prefs, String key, String value ) { + static void putPrefsString( Preferences prefs, String key, String value ) { if( !StringUtils.isEmpty( value ) ) prefs.put( key, value ); else diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java index f8239751..5872f8ad 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreview.java @@ -41,7 +41,11 @@ class FlatThemePreview private final FlatSyntaxTextArea textArea; private final Timer timer; - private final Preferences state; + final Preferences state; + + private final FlatThemePreviewAll allTab; + private final FlatThemePreviewButtons buttonsTab; + private final FlatThemePreviewSwitches switchesTab; private final Map lazyValueCache = new WeakHashMap<>(); private int runWithUIDefaultsGetterLevel; @@ -53,9 +57,12 @@ class FlatThemePreview initComponents(); // add tabs - tabbedPane.addTab( "All", createPreviewTab( new FlatThemePreviewAll( this ) ) ); - tabbedPane.addTab( "Buttons", createPreviewTab( new FlatThemePreviewButtons() ) ); - tabbedPane.addTab( "Switches", createPreviewTab( new FlatThemePreviewSwitches() ) ); + allTab = new FlatThemePreviewAll( this ); + buttonsTab = new FlatThemePreviewButtons( this ); + switchesTab = new FlatThemePreviewSwitches(); + tabbedPane.addTab( "All", createPreviewTab( allTab ) ); + tabbedPane.addTab( "Buttons", createPreviewTab( buttonsTab ) ); + tabbedPane.addTab( "Switches", createPreviewTab( switchesTab ) ); selectRecentTab(); tabbedPane.addChangeListener( e -> selectedTabChanged() ); @@ -85,8 +92,14 @@ class FlatThemePreview private void selectRecentTab() { int selectedTab = state.getInt( KEY_SELECTED_TAB, -1 ); - if( selectedTab >= 0 && selectedTab < tabbedPane.getTabCount() ) + if( selectedTab >= 0 && selectedTab < tabbedPane.getTabCount() ) { tabbedPane.setSelectedIndex( selectedTab ); + + switch( selectedTab ) { + case 0: allTab.activated(); break; + case 1: buttonsTab.activated(); break; + } + } } private void selectedTabChanged() { diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.java index 9f6c2542..25b7bc57 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.java @@ -39,6 +39,10 @@ import net.miginfocom.swing.*; class FlatThemePreviewAll extends JPanel { + private static final String KEY_ENABLED = "preview.enabled"; + private static final String KEY_FOCUSED = "preview.focused"; + private static final String KEY_MENU_UNDERLINE_SELECTION = "preview.menuUnderlineSelection"; + private final FlatThemePreview preview; FlatThemePreviewAll( FlatThemePreview preview ) { @@ -78,6 +82,27 @@ class FlatThemePreviewAll } ); } + void activated() { + boolean enabled = preview.state.getBoolean( KEY_ENABLED, true ); + boolean focused = preview.state.getBoolean( KEY_FOCUSED, false ); + boolean menuUnderlineSelection = preview.state.getBoolean( KEY_MENU_UNDERLINE_SELECTION, false ); + + if( enabled != enabledCheckBox.isSelected() ) { + enabledCheckBox.setSelected( enabled ); + enabledChanged(); + } + + if( focused != focusedCheckBox.isSelected() ) { + focusedCheckBox.setSelected( focused ); + focusedChanged(); + } + + if( menuUnderlineSelection != menuUnderlineSelectionButton.isSelected() ) { + menuUnderlineSelectionButton.setSelected( menuUnderlineSelection ); + menuUnderlineSelectionChanged(); + } + } + private void enabledChanged() { boolean enabled = enabledCheckBox.isSelected(); @@ -89,10 +114,12 @@ class FlatThemePreviewAll preview.runWithUIDefaultsGetter( () -> { enableDisable( this, enabled ); } ); + + FlatThemeFileEditor.putPrefsBoolean( preview.state, KEY_ENABLED, enabled, true ); } private void enableDisable( Component comp, boolean enabled ) { - if( comp != enabledCheckBox && comp != focusedCheckBox && comp != menu2 ) + if( !isControlComponent( comp ) ) comp.setEnabled( enabled ); if( !(comp instanceof Container) || comp instanceof JInternalFrame ) @@ -120,15 +147,19 @@ class FlatThemePreviewAll } private void focusedChanged() { - Predicate value = focusedCheckBox.isSelected() && enabledCheckBox.isSelected() + boolean focused = focusedCheckBox.isSelected(); + + Predicate value = focused && enabledCheckBox.isSelected() ? value = c -> true : null; focusComponent( this, value ); repaint(); + + FlatThemeFileEditor.putPrefsBoolean( preview.state, KEY_FOCUSED, focused,false ); } private void focusComponent( Component comp, Object value ) { - if( comp != enabledCheckBox && comp != focusedCheckBox && comp != menu2 && comp instanceof JComponent ) + if( !isControlComponent( comp ) && comp instanceof JComponent ) ((JComponent)comp).putClientProperty( FlatClientProperties.COMPONENT_FOCUS_OWNER, value ); if( !(comp instanceof Container) || comp instanceof JInternalFrame ) @@ -142,6 +173,20 @@ class FlatThemePreviewAll } } + private boolean isControlComponent( Component c ) { + return c == enabledCheckBox || + c == focusedCheckBox || + c == menuUnderlineSelectionButton || + c == menu2; + } + + private void menuUnderlineSelectionChanged() { + boolean menuUnderlineSelection = menuUnderlineSelectionButton.isSelected(); + UIManager.put( "MenuItem.selectionType", menuUnderlineSelection ? "underline" : null ); + + FlatThemeFileEditor.putPrefsBoolean( preview.state, KEY_MENU_UNDERLINE_SELECTION, menuUnderlineSelection, false ); + } + private void changeProgress() { int value = slider3.getValue(); progressBar1.setValue( value ); @@ -204,6 +249,7 @@ class FlatThemePreviewAll JScrollPane scrollPane9 = new JScrollPane(); JTextPane textPane1 = new JTextPane(); JLabel menuBarLabel = new JLabel(); + menuUnderlineSelectionButton = new FlatToggleButton(); JMenuBar menuBar1 = new JMenuBar(); menu2 = new JMenu(); JMenuItem menuItem3 = new JMenuItem(); @@ -482,6 +528,15 @@ class FlatThemePreviewAll menuBarLabel.setText("JMenuBar:"); add(menuBarLabel, "cell 0 12"); + //---- menuUnderlineSelectionButton ---- + menuUnderlineSelectionButton.setText("_"); + menuUnderlineSelectionButton.setButtonType(FlatButton.ButtonType.toolBarButton); + menuUnderlineSelectionButton.setToolTipText("menu underline selection"); + menuUnderlineSelectionButton.setFocusable(false); + menuUnderlineSelectionButton.setFont(menuUnderlineSelectionButton.getFont().deriveFont(menuUnderlineSelectionButton.getFont().getSize() - 2f)); + menuUnderlineSelectionButton.addActionListener(e -> menuUnderlineSelectionChanged()); + add(menuUnderlineSelectionButton, "cell 0 12"); + //======== menuBar1 ======== { @@ -769,6 +824,7 @@ class FlatThemePreviewAll private JCheckBox enabledCheckBox; private JCheckBox focusedCheckBox; private FlatTextField textField1; + private FlatToggleButton menuUnderlineSelectionButton; private JMenu menu2; private JSlider slider1; private JSlider slider3; diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.jfd b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.jfd index 34b3de15..017ba596 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.jfd +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewAll.jfd @@ -296,6 +296,20 @@ new FormModel { }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { "value": "cell 0 12" } ) + add( new FormComponent( "com.formdev.flatlaf.extras.components.FlatToggleButton" ) { + name: "menuUnderlineSelectionButton" + "text": "_" + "buttonType": enum com.formdev.flatlaf.extras.components.FlatButton$ButtonType toolBarButton + "toolTipText": "menu underline selection" + "focusable": false + "font": new com.jformdesigner.model.SwingDerivedFont( null, 0, -2, false ) + auxiliary() { + "JavaCodeGenerator.variableLocal": false + } + addEvent( new FormEvent( "java.awt.event.ActionListener", "actionPerformed", "menuUnderlineSelectionChanged", false ) ) + }, new FormLayoutConstraints( class net.miginfocom.layout.CC ) { + "value": "cell 0 12" + } ) add( new FormContainer( "javax.swing.JMenuBar", new FormLayoutManager( class javax.swing.JMenuBar ) ) { name: "menuBar1" add( new FormContainer( "javax.swing.JMenu", new FormLayoutManager( class javax.swing.JMenu ) ) { diff --git a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewButtons.java b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewButtons.java index 4771bda9..b358acda 100644 --- a/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewButtons.java +++ b/flatlaf-theme-editor/src/main/java/com/formdev/flatlaf/themeeditor/FlatThemePreviewButtons.java @@ -16,11 +16,11 @@ package com.formdev.flatlaf.themeeditor; +import static com.formdev.flatlaf.FlatClientProperties.*; import java.awt.Component; import java.util.Objects; import java.util.function.Predicate; import javax.swing.*; -import com.formdev.flatlaf.FlatClientProperties; import net.miginfocom.swing.*; /** @@ -29,31 +29,64 @@ import net.miginfocom.swing.*; class FlatThemePreviewButtons extends JPanel { - FlatThemePreviewButtons() { + private static final String KEY_BUTTON_TYPE = "preview.buttonType"; + + private final FlatThemePreview preview; + + FlatThemePreviewButtons( FlatThemePreview preview ) { + this.preview = preview; + initComponents(); } - private void buttonTypeChanged() { - Object buttonType = null; + void activated() { + String buttonType = preview.state.get( KEY_BUTTON_TYPE, null ); + + if( !Objects.equals( buttonType, getButtonType() ) ) { + setButtonType( buttonType ); + buttonTypeChanged(); + } + } + + private String getButtonType() { + String buttonType = null; if( squareButton.isSelected() ) - buttonType = FlatClientProperties.BUTTON_TYPE_SQUARE; + buttonType = BUTTON_TYPE_SQUARE; else if( roundRectButton.isSelected() ) - buttonType = FlatClientProperties.BUTTON_TYPE_ROUND_RECT; + buttonType = BUTTON_TYPE_ROUND_RECT; else if( tabButton.isSelected() ) - buttonType = FlatClientProperties.BUTTON_TYPE_TAB; + buttonType = BUTTON_TYPE_TAB; else if( toolBarButtonButton.isSelected() ) - buttonType = FlatClientProperties.BUTTON_TYPE_TOOLBAR_BUTTON; + buttonType = BUTTON_TYPE_TOOLBAR_BUTTON; else if( borderlessButton.isSelected() ) - buttonType = FlatClientProperties.BUTTON_TYPE_BORDERLESS; + buttonType = BUTTON_TYPE_BORDERLESS; + return buttonType; + } + + private void setButtonType( String buttonType ) { + switch( String.valueOf( buttonType ) ) { + case BUTTON_TYPE_SQUARE: squareButton.setSelected( true ); break; + case BUTTON_TYPE_ROUND_RECT: roundRectButton.setSelected( true ); break; + case BUTTON_TYPE_TAB: tabButton.setSelected( true ); break; + case BUTTON_TYPE_TOOLBAR_BUTTON: toolBarButtonButton.setSelected( true ); break; + case BUTTON_TYPE_BORDERLESS: borderlessButton.setSelected( true ); break; + default: noneButton.setSelected( true ); break; + } + } + + private void buttonTypeChanged() { + String buttonType = getButtonType(); for( Component c : getComponents() ) { if( !(c instanceof AbstractButton) ) continue; AbstractButton b = (AbstractButton) c; - if( !Objects.equals( b.getClientProperty( FlatClientProperties.BUTTON_TYPE ), FlatClientProperties.BUTTON_TYPE_HELP ) ) - b.putClientProperty( FlatClientProperties.BUTTON_TYPE, buttonType ); + if( !Objects.equals( b.getClientProperty( BUTTON_TYPE ), BUTTON_TYPE_HELP ) ) + b.putClientProperty( BUTTON_TYPE, buttonType ); } + + FlatThemeFileEditor.putPrefsString( preview.state, KEY_BUTTON_TYPE, buttonType ); } private void initComponents() { @@ -664,7 +697,7 @@ class FlatThemePreviewButtons } } ); - putClientProperty( FlatClientProperties.COMPONENT_FOCUS_OWNER, + putClientProperty( COMPONENT_FOCUS_OWNER, (Predicate) c -> { return ((TestStateButton)c).isStateFocused(); } ); @@ -734,7 +767,7 @@ class FlatThemePreviewButtons } } ); - putClientProperty( FlatClientProperties.COMPONENT_FOCUS_OWNER, + putClientProperty( COMPONENT_FOCUS_OWNER, (Predicate) c -> { return ((TestStateToggleButton)c).isStateFocused(); } );