8043315: Nimbus: Setting Nimbus.Overrides property affects custom keymap installation
Reviewed-by: serb, azvegint
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthEditorPaneUI.java Wed Jun 21 10:30:25 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthEditorPaneUI.java Thu Jun 22 19:28:00 2017 +0530
@@ -47,6 +47,8 @@
*/
private Boolean localTrue = Boolean.TRUE;
+ private boolean updateKBAction = true;
+
/**
* Creates a new UI object for the given component.
*
@@ -70,7 +72,7 @@
if (clientProperty == null) {
c.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, localTrue);
}
- updateStyle(getComponent());
+ updateStyle(getComponent(), true);
}
/**
@@ -106,13 +108,22 @@
*/
@Override
protected void propertyChange(PropertyChangeEvent evt) {
+
+ if (evt.getPropertyName().equals("keymap")) {
+ if (evt.getNewValue() != null)
+ {
+ updateKBAction = false;
+ } else {
+ updateKBAction = true;
+ }
+ }
if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
- updateStyle((JTextComponent)evt.getSource());
+ updateStyle((JTextComponent)evt.getSource(), updateKBAction);
}
super.propertyChange(evt);
}
- private void updateStyle(JTextComponent comp) {
+ private void updateStyle(JTextComponent comp, boolean updateKBAction) {
SynthContext context = getContext(comp, ENABLED);
SynthStyle oldStyle = style;
@@ -121,7 +132,7 @@
if (style != oldStyle) {
SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
- if (oldStyle != null) {
+ if (oldStyle != null && updateKBAction) {
uninstallKeyboardActions();
installKeyboardActions();
}
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthTextAreaUI.java Wed Jun 21 10:30:25 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthTextAreaUI.java Thu Jun 22 19:28:00 2017 +0530
@@ -55,7 +55,7 @@
public class SynthTextAreaUI extends BasicTextAreaUI implements SynthUI {
private Handler handler = new Handler();
private SynthStyle style;
-
+ private boolean updateKBAction = true;
/**
* Creates a UI object for a JTextArea.
*
@@ -73,7 +73,7 @@
protected void installDefaults() {
// Installs the text cursor on the component
super.installDefaults();
- updateStyle(getComponent());
+ updateStyle(getComponent(), true);
getComponent().addFocusListener(handler);
}
@@ -92,7 +92,7 @@
super.uninstallDefaults();
}
- private void updateStyle(JTextComponent comp) {
+ private void updateStyle(JTextComponent comp, boolean updateKBAction) {
SynthContext context = getContext(comp, ENABLED);
SynthStyle oldStyle = style;
@@ -101,7 +101,7 @@
if (style != oldStyle) {
SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
- if (oldStyle != null) {
+ if (oldStyle != null && updateKBAction) {
uninstallKeyboardActions();
installKeyboardActions();
}
@@ -184,8 +184,16 @@
*/
@Override
protected void propertyChange(PropertyChangeEvent evt) {
+ if (evt.getPropertyName().equals("keymap")) {
+ if (evt.getNewValue() != null)
+ {
+ updateKBAction = false;
+ } else {
+ updateKBAction = true;
+ }
+ }
if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
- updateStyle((JTextComponent)evt.getSource());
+ updateStyle((JTextComponent)evt.getSource(), updateKBAction);
}
super.propertyChange(evt);
}
--- a/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthTextFieldUI.java Wed Jun 21 10:30:25 2017 +0530
+++ b/jdk/src/java.desktop/share/classes/javax/swing/plaf/synth/SynthTextFieldUI.java Thu Jun 22 19:28:00 2017 +0530
@@ -54,6 +54,7 @@
public class SynthTextFieldUI extends BasicTextFieldUI implements SynthUI {
private Handler handler = new Handler();
private SynthStyle style;
+ private boolean updateKBAction = true;
/**
* Creates a UI for a JTextField.
@@ -65,7 +66,7 @@
return new SynthTextFieldUI();
}
- private void updateStyle(JTextComponent comp) {
+ private void updateStyle(JTextComponent comp, boolean updateKBAction) {
SynthContext context = getContext(comp, ENABLED);
SynthStyle oldStyle = style;
@@ -74,7 +75,7 @@
if (style != oldStyle) {
SynthTextFieldUI.updateStyle(comp, context, getPropertyPrefix());
- if (oldStyle != null) {
+ if (oldStyle != null && updateKBAction) {
uninstallKeyboardActions();
installKeyboardActions();
}
@@ -232,8 +233,16 @@
*/
@Override
protected void propertyChange(PropertyChangeEvent evt) {
+ if (evt.getPropertyName().equals("keymap")) {
+ if (evt.getNewValue() != null)
+ {
+ updateKBAction = false;
+ } else {
+ updateKBAction = true;
+ }
+ }
if (SynthLookAndFeel.shouldUpdateStyle(evt)) {
- updateStyle((JTextComponent)evt.getSource());
+ updateStyle((JTextComponent)evt.getSource(), updateKBAction);
}
super.propertyChange(evt);
}
@@ -245,7 +254,7 @@
protected void installDefaults() {
// Installs the text cursor on the component
super.installDefaults();
- updateStyle(getComponent());
+ updateStyle(getComponent(), true);
getComponent().addFocusListener(handler);
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/javax/swing/plaf/nimbus/TestNimbusOverride.java Thu Jun 22 19:28:00 2017 +0530
@@ -0,0 +1,112 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+/*
+ * @test
+ * @bug 8043315
+ * @summary Verifies if setting Nimbus.Overrides property affects
+ * keymap installation
+ * @run main TestNimbusOverride
+ */
+import java.awt.BorderLayout;
+import java.awt.Component;
+import java.awt.Dimension;
+import java.awt.EventQueue;
+import java.awt.event.ActionEvent;
+import java.awt.Robot;
+import java.awt.event.KeyEvent;
+
+import javax.swing.AbstractAction;
+import javax.swing.JEditorPane;
+import javax.swing.JFrame;
+import javax.swing.JOptionPane;
+import javax.swing.JPanel;
+import javax.swing.KeyStroke;
+import javax.swing.SwingUtilities;
+import javax.swing.UIDefaults;
+import javax.swing.UIManager;
+import javax.swing.text.Keymap;
+
+
+public class TestNimbusOverride extends JFrame
+{
+ private static TestNimbusOverride tf;
+ private static boolean passed = false;
+
+ public static void main(String [] args) throws Exception {
+ Robot robot = new Robot();
+ SwingUtilities.invokeAndWait(() -> {
+ try {
+ UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ tf = new TestNimbusOverride();
+ tf.pack();
+ tf.setVisible(true);
+ });
+ robot.setAutoDelay(100);
+ robot.waitForIdle();
+ robot.keyPress(KeyEvent.VK_SPACE);
+ robot.keyRelease(KeyEvent.VK_SPACE);
+ robot.waitForIdle();
+ SwingUtilities.invokeAndWait(() -> tf.dispose());
+ if (!passed) {
+ throw new RuntimeException(
+ "Setting Nimbus.Overrides property affects custom keymap installation");
+ }
+ }
+
+ public TestNimbusOverride()
+ {
+ setDefaultCloseOperation(DISPOSE_ON_CLOSE);
+
+ /*
+ * Create a frame containing a JEditorPane, and override the action for the space bar to show
+ * a dialog.
+ */
+ JEditorPane pp = new JEditorPane();
+ UIDefaults defaults = new UIDefaults();
+
+ pp.putClientProperty("Nimbus.Overrides", defaults);
+
+ JPanel contentPanel = new JPanel();
+ contentPanel.setLayout(new BorderLayout());
+ setContentPane(contentPanel);
+
+ contentPanel.setPreferredSize(new Dimension(400, 300));
+ contentPanel.add(pp, BorderLayout.CENTER);
+
+ Keymap origKeymap = pp.getKeymap();
+ Keymap km = JEditorPane.addKeymap("Test keymap", origKeymap);
+
+ km.addActionForKeyStroke(KeyStroke.getKeyStroke(' '), new AbstractAction("SHOW_SPACE") {
+ @Override
+ public void actionPerformed(ActionEvent e)
+ {
+ passed = true;
+ }
+ });
+
+ pp.setKeymap(km);
+ }
+}