diff --git a/CHANGELOG.md b/CHANGELOG.md index faf1024e..33934cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ FlatLaf Change Log - Native Windows libraries: Fixed crash when running in Java 8 and newer Java version is installed in `PATH` environment variable and using class `SystemInfo` before AWT initialization. (issue #673) +- ComboBox: Fixed search in item list for text with spaces. (issue #691) - FormattedTextField: On Linux, fixed `IllegalArgumentException: Invalid location` if `JFormattedTextField.setDocument()` is invoked in a focus gained listener on that formatted text field. (issue #698) diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java index 29297e51..58a26ff1 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/FlatInputMaps.java @@ -50,7 +50,8 @@ class FlatInputMaps } modifyInputMap( defaults, "ComboBox.ancestorInputMap", - "SPACE", "spacePopup", +// Space key still shows popup, but from FlatComboBoxUI.FlatKeySelectionManager +// "SPACE", "spacePopup", "UP", mac( "selectPrevious2", "selectPrevious" ), "DOWN", mac( "selectNext2", "selectNext" ), diff --git a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java index f24bb543..37a11438 100644 --- a/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java +++ b/flatlaf-core/src/main/java/com/formdev/flatlaf/ui/FlatComboBoxUI.java @@ -24,6 +24,7 @@ import java.awt.Component; import java.awt.ComponentOrientation; import java.awt.Container; import java.awt.Dimension; +import java.awt.EventQueue; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; @@ -48,10 +49,12 @@ import java.util.concurrent.atomic.AtomicBoolean; import javax.swing.AbstractAction; import javax.swing.BorderFactory; import javax.swing.CellRendererPane; +import javax.swing.ComboBoxModel; import javax.swing.DefaultListCellRenderer; import javax.swing.InputMap; import javax.swing.JButton; import javax.swing.JComboBox; +import javax.swing.JComboBox.KeySelectionManager; import javax.swing.JComponent; import javax.swing.JList; import javax.swing.JPanel; @@ -182,6 +185,9 @@ public class FlatComboBoxUI private void installUIImpl( JComponent c ) { super.installUI( c ); + // install key selection manager that shows popup when Space key is pressed + comboBox.setKeySelectionManager( new FlatKeySelectionManager( comboBox.getKeySelectionManager() ) ); + installStyle(); } @@ -1232,4 +1238,46 @@ public class FlatComboBoxUI } } } + + //---- class FlatKeySelectionManager -------------------------------------- + + /** + * Key selection manager that delegates to the default manager. + * Shows the popup if Space key is pressed and "typed characters" buffer is empty. + * If items contain spaces (e.g. "a b") it is still possible to select them + * by pressing keys a, Space and b. + */ + private class FlatKeySelectionManager + implements JComboBox.KeySelectionManager, UIResource + { + private final KeySelectionManager delegate; + private final long timeFactor; + private long lastTime; + + FlatKeySelectionManager( JComboBox.KeySelectionManager delegate ) { + this.delegate = delegate; + + Long value = (Long) UIManager.get( "ComboBox.timeFactor" ); + timeFactor = (value != null) ? value : 1000; + } + + @SuppressWarnings( "rawtypes" ) + @Override + public int selectionForKey( char aKey, ComboBoxModel aModel ) { + long time = EventQueue.getMostRecentEventTime(); + long oldLastTime = lastTime; + lastTime = time; + + // SPACE key shows popup if not yet visible + if( aKey == ' ' && + time - oldLastTime >= timeFactor && + !comboBox.isPopupVisible() ) + { + comboBox.setPopupVisible( true ); + return -1; + } + + return delegate.selectionForKey( aKey, aModel ); + } + } } diff --git a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_InputMap_1.8.0.txt b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_InputMap_1.8.0.txt index a34906f7..4389a367 100644 --- a/flatlaf-testing/dumps/uidefaults/FlatLightLaf_InputMap_1.8.0.txt +++ b/flatlaf-testing/dumps/uidefaults/FlatLightLaf_InputMap_1.8.0.txt @@ -23,7 +23,7 @@ CheckBox.focusInputMap [lazy] 2 javax.swing.plaf.InputMapUIResource [ #---- ComboBox ---- -ComboBox.ancestorInputMap [lazy] 15 javax.swing.plaf.InputMapUIResource [UI] +ComboBox.ancestorInputMap [lazy] 14 javax.swing.plaf.InputMapUIResource [UI] alt DOWN togglePopup alt KP_DOWN togglePopup alt KP_UP togglePopup @@ -37,7 +37,6 @@ ComboBox.ancestorInputMap [lazy] 15 javax.swing.plaf.InputMapUIResource KP_UP selectPrevious2 PAGE_DOWN pageDownPassThrough PAGE_UP pageUpPassThrough - SPACE spacePopup UP selectPrevious2