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 /**
  25  * @test
  26  * @bug 8043315
  27  * @summary  Verifies if setting Nimbus.Overrides property affects
  28  *           keymap installation
  29  * @key headful
  30  * @run main TestNimbusOverride
  31  */
  32 
  33 import java.awt.BorderLayout;
  34 import java.awt.Component;
  35 import java.awt.Dimension;
  36 import java.awt.EventQueue;
  37 import java.awt.event.ActionEvent;
  38 import java.awt.Robot;
  39 import java.awt.event.KeyEvent;
  40 
  41 import javax.swing.AbstractAction;
  42 import javax.swing.JEditorPane;
  43 import javax.swing.JFrame;
  44 import javax.swing.JOptionPane;
  45 import javax.swing.JPanel;
  46 import javax.swing.KeyStroke;
  47 import javax.swing.SwingUtilities;
  48 import javax.swing.UIDefaults;
  49 import javax.swing.UIManager;
  50 import javax.swing.text.Keymap;
  51 
  52 
  53 public class TestNimbusOverride extends JFrame
  54 {
  55     private static TestNimbusOverride tf;
  56     private static boolean passed = false;
  57 
  58     public static void main(String [] args) throws Exception {
  59         Robot robot = new Robot();
  60         SwingUtilities.invokeAndWait(() -> {
  61             try {
  62                 UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
  63             } catch (Exception e) {
  64                 throw new RuntimeException(e);
  65             }
  66             tf = new TestNimbusOverride();
  67             tf.pack();
  68             tf.setVisible(true);
  69         });
  70         robot.setAutoDelay(100);
  71         robot.waitForIdle();
  72         robot.keyPress(KeyEvent.VK_SPACE);
  73         robot.keyRelease(KeyEvent.VK_SPACE);
  74         robot.waitForIdle();
  75         SwingUtilities.invokeAndWait(() -> tf.dispose());
  76         if (!passed) {
  77                 throw new RuntimeException(
  78                         "Setting Nimbus.Overrides property affects custom keymap installation");
  79             }
  80     }
  81 
  82     public TestNimbusOverride()
  83     {
  84         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  85 
  86         /*
  87          * Create a frame containing a JEditorPane, and override the action for the space bar to show
  88          * a dialog.
  89          */
  90         JEditorPane pp = new JEditorPane();
  91         UIDefaults defaults = new UIDefaults();
  92 
  93         pp.putClientProperty("Nimbus.Overrides", defaults);
  94 
  95         JPanel contentPanel = new JPanel();
  96         contentPanel.setLayout(new BorderLayout());
  97         setContentPane(contentPanel);
  98 
  99         contentPanel.setPreferredSize(new Dimension(400, 300));
 100         contentPanel.add(pp, BorderLayout.CENTER);
 101 
 102         Keymap origKeymap = pp.getKeymap();
 103         Keymap km = JEditorPane.addKeymap("Test keymap", origKeymap);
 104 
 105         km.addActionForKeyStroke(KeyStroke.getKeyStroke(' '), new AbstractAction("SHOW_SPACE") {
 106             @Override
 107             public void actionPerformed(ActionEvent e)
 108             {
 109                 passed = true;
 110             }
 111         });
 112 
 113         pp.setKeymap(km);
 114     }
 115 }