From b63cd241d235b26ec2b8ac48a1da146c2472622d Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 18 Feb 2020 18:09:49 +0100 Subject: [PATCH] refactored InputMap code into own class --- .../com/formdev/flatlaf/FlatInputMaps.java | 116 ++++++++++++++++++ .../java/com/formdev/flatlaf/FlatLaf.java | 92 +------------- 2 files changed, 117 insertions(+), 91 deletions(-) create mode 100644 flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java new file mode 100644 index 00000000..f7ca8569 --- /dev/null +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java @@ -0,0 +1,116 @@ +/* + * Copyright 2020 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 + * + * https://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; + +import javax.swing.InputMap; +import javax.swing.KeyStroke; +import javax.swing.UIDefaults; +import javax.swing.UIDefaults.LazyValue; +import com.formdev.flatlaf.util.SystemInfo; + +/** + * @author Karl Tauber + */ +class FlatInputMaps +{ + static void initInputMaps( UIDefaults defaults ) { + if( SystemInfo.IS_MAC ) { + // AquaLookAndFeel (the base for UI defaults on macOS) uses special + // action keys (e.g. "aquaExpandNode") for some macOS specific behaviour. + // Those action keys are not available in FlatLaf, which makes it + // necessary to make some modifications. + + // combobox + defaults.put( "ComboBox.ancestorInputMap", new UIDefaults.LazyInputMap( new Object[] { + "ESCAPE", "hidePopup", + "PAGE_UP", "pageUpPassThrough", + "PAGE_DOWN", "pageDownPassThrough", + "HOME", "homePassThrough", + "END", "endPassThrough", + "DOWN", "selectNext", + "KP_DOWN", "selectNext", + "SPACE", "spacePopup", + "ENTER", "enterPressed", + "UP", "selectPrevious", + "KP_UP", "selectPrevious" + } ) ); + + // tree node expanding/collapsing + modifyInputMap( defaults, "Tree.focusInputMap", + "RIGHT", "selectChild", + "KP_RIGHT", "selectChild", + "LEFT", "selectParent", + "KP_LEFT", "selectParent", + "shift RIGHT", null, + "shift KP_RIGHT", null, + "shift LEFT", null, + "shift KP_LEFT", null, + "ctrl LEFT", null, + "ctrl KP_LEFT", null, + "ctrl RIGHT", null, + "ctrl KP_RIGHT", null + ); + defaults.put( "Tree.focusInputMap.RightToLeft", new UIDefaults.LazyInputMap( new Object[] { + "RIGHT", "selectParent", + "KP_RIGHT", "selectParent", + "LEFT", "selectChild", + "KP_LEFT", "selectChild" + } ) ); + } + } + + private static void modifyInputMap( UIDefaults defaults, String key, Object... bindings ) { + // Note: not using `defaults.get(key)` here because this would resolve the lazy value + defaults.put( key, new LazyModifyInputMap( defaults.remove( key ), bindings ) ); + } + + //---- class LazyModifyInputMap ------------------------------------------- + + /** + * Takes a (lazy) base input map and lazily applies modifications to it specified in bindings. + */ + private static class LazyModifyInputMap + implements LazyValue + { + private final Object baseInputMap; + private final Object[] bindings; + + public LazyModifyInputMap( Object baseInputMap, Object[] bindings ) { + this.baseInputMap = baseInputMap; + this.bindings = bindings; + } + + @Override + public Object createValue( UIDefaults table ) { + // get base input map + InputMap inputMap = (baseInputMap instanceof LazyValue) + ? (InputMap) ((LazyValue)baseInputMap).createValue( table ) + : (InputMap) baseInputMap; + + // modify input map (replace or remove) + for( int i = 0; i < bindings.length; i += 2 ) { + KeyStroke keyStroke = KeyStroke.getKeyStroke( (String) bindings[i] ); + if( bindings[i + 1] != null ) + inputMap.put( keyStroke, bindings[i + 1] ); + else + inputMap.remove( keyStroke ); + } + + return inputMap; + } + } +} diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java index bba1fa43..050f97eb 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatLaf.java @@ -35,16 +35,13 @@ import java.util.function.Consumer; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.AbstractButton; -import javax.swing.InputMap; import javax.swing.JLabel; import javax.swing.JRootPane; import javax.swing.JTabbedPane; -import javax.swing.KeyStroke; import javax.swing.LookAndFeel; import javax.swing.PopupFactory; import javax.swing.SwingUtilities; import javax.swing.UIDefaults; -import javax.swing.UIDefaults.LazyValue; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import javax.swing.plaf.ColorUIResource; @@ -250,7 +247,7 @@ public abstract class FlatLaf initFonts( defaults ); initIconColors( defaults, isDark() ); - initInputMaps( defaults ); + FlatInputMaps.initInputMaps( defaults ); // load defaults from properties List> lafClassesForDefaultsLoading = getLafClassesForDefaultsLoading(); @@ -356,57 +353,6 @@ public abstract class FlatLaf defaults.put( "Objects.BlackText", new ColorUIResource( 0x231F20 ) ); } - private void initInputMaps( UIDefaults defaults ) { - if( SystemInfo.IS_MAC ) { - // AquaLookAndFeel (the base for UI defaults on macOS) uses special - // action keys (e.g. "aquaExpandNode") for some macOS specific behaviour. - // Those action keys are not available in FlatLaf, which makes it - // necessary to make some modifications. - - // combobox - defaults.put( "ComboBox.ancestorInputMap", new UIDefaults.LazyInputMap( new Object[] { - "ESCAPE", "hidePopup", - "PAGE_UP", "pageUpPassThrough", - "PAGE_DOWN", "pageDownPassThrough", - "HOME", "homePassThrough", - "END", "endPassThrough", - "DOWN", "selectNext", - "KP_DOWN", "selectNext", - "SPACE", "spacePopup", - "ENTER", "enterPressed", - "UP", "selectPrevious", - "KP_UP", "selectPrevious" - } ) ); - - // tree node expanding/collapsing - modifyInputMap( defaults, "Tree.focusInputMap", - "RIGHT", "selectChild", - "KP_RIGHT", "selectChild", - "LEFT", "selectParent", - "KP_LEFT", "selectParent", - "shift RIGHT", null, - "shift KP_RIGHT", null, - "shift LEFT", null, - "shift KP_LEFT", null, - "ctrl LEFT", null, - "ctrl KP_LEFT", null, - "ctrl RIGHT", null, - "ctrl KP_RIGHT", null - ); - defaults.put( "Tree.focusInputMap.RightToLeft", new UIDefaults.LazyInputMap( new Object[] { - "RIGHT", "selectParent", - "KP_RIGHT", "selectParent", - "LEFT", "selectChild", - "KP_LEFT", "selectChild" - } ) ); - } - } - - private void modifyInputMap( UIDefaults defaults, String key, Object... bindings ) { - // Note: not using `defaults.get(key)` here because this would resolve the lazy value - defaults.put( key, new LazyModifyInputMap( defaults.remove( key ), bindings ) ); - } - private void putDefaults( UIDefaults defaults, Object value, String... keys ) { for( String key : keys ) defaults.put( key, value ); @@ -523,40 +469,4 @@ public abstract class FlatLaf return false; } - - //---- class LazyModifyInputMap ------------------------------------------- - - /** - * Takes a (lazy) base input map and lazily applies modifications to it specified in bindings. - */ - private static class LazyModifyInputMap - implements LazyValue - { - private final Object baseInputMap; - private final Object[] bindings; - - public LazyModifyInputMap( Object baseInputMap, Object[] bindings ) { - this.baseInputMap = baseInputMap; - this.bindings = bindings; - } - - @Override - public Object createValue( UIDefaults table ) { - // get base input map - InputMap inputMap = (baseInputMap instanceof LazyValue) - ? (InputMap) ((LazyValue)baseInputMap).createValue( table ) - : (InputMap) baseInputMap; - - // modify input map (replace or remove) - for( int i = 0; i < bindings.length; i += 2 ) { - KeyStroke keyStroke = KeyStroke.getKeyStroke( (String) bindings[i] ); - if( bindings[i + 1] != null ) - inputMap.put( keyStroke, bindings[i + 1] ); - else - inputMap.remove( keyStroke ); - } - - return inputMap; - } - } }