1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 /*
  24  * @test
  25  * @bug 8043315
  26  * @summary  Verifies if setting Nimbus.Overrides property affects
  27  *           keymap installation
  28  * @run main TestNimbusOverride
  29  */
  30 import java.awt.BorderLayout;
  31 import java.awt.Component;
  32 import java.awt.Dimension;
  33 import java.awt.EventQueue;
  34 import java.awt.event.ActionEvent;
  35 import java.awt.Robot;
  36 import java.awt.event.KeyEvent;
  37 
  38 import javax.swing.AbstractAction;
  39 import javax.swing.JEditorPane;
  40 import javax.swing.JFrame;
  41 import javax.swing.JOptionPane;
  42 import javax.swing.JPanel;
  43 import javax.swing.KeyStroke;
  44 import javax.swing.SwingUtilities;
  45 import javax.swing.UIDefaults;
  46 import javax.swing.UIManager;
  47 import javax.swing.text.Keymap;
  48 
  49 
  50 public class TestNimbusOverride extends JFrame
  51 {
  52     private static TestNimbusOverride tf;
  53     private static boolean passed = false;
  54 
  55     public static void main(String [] args) throws Exception {
  56         Robot robot = new Robot();
  57         SwingUtilities.invokeAndWait(() -> {
  58             try {
  59                 UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  60             } catch (Exception e) {
  61                 throw new RuntimeException(e);
  62             }
  63             tf = new TestNimbusOverride();
  64             tf.pack();
  65             tf.setVisible(true);
  66         });
  67         robot.setAutoDelay(100);
  68         robot.waitForIdle();
  69         robot.keyPress(KeyEvent.VK_SPACE);
  70         robot.keyRelease(KeyEvent.VK_SPACE);
  71         robot.waitForIdle();
  72         SwingUtilities.invokeAndWait(() -> tf.dispose());
  73         if (!passed) {
  74                 throw new RuntimeException(
  75                         "Setting Nimbus.Overrides property affects custom keymap installation");
  76             }
  77     }
  78 
  79     public TestNimbusOverride()
  80     {
  81         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  82 
  83         /*
  84          * Create a frame containing a JEditorPane, and override the action for the space bar to show
  85          * a dialog.
  86          */
  87         JEditorPane pp = new JEditorPane();
  88         UIDefaults defaults = new UIDefaults();
  89 
  90         pp.putClientProperty("Nimbus.Overrides", defaults);
  91 
  92         JPanel contentPanel = new JPanel();
  93         contentPanel.setLayout(new BorderLayout());
  94         setContentPane(contentPanel);
  95 
  96         contentPanel.setPreferredSize(new Dimension(400, 300));
  97         contentPanel.add(pp, BorderLayout.CENTER);
  98 
  99         Keymap origKeymap = pp.getKeymap();
 100         Keymap km = JEditorPane.addKeymap("Test keymap", origKeymap);
 101 
 102         km.addActionForKeyStroke(KeyStroke.getKeyStroke(' '), new AbstractAction("SHOW_SPACE") {
 103             @Override
 104             public void actionPerformed(ActionEvent e)
 105             {
 106                 passed = true;
 107             }
 108         });
 109 
 110         pp.setKeymap(km);
 111     }
 112 }