1 /*
   2  * Copyright (c) 2007, 2011, 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 6256140
  27  * @summary Esc key doesn't restore old value in JFormattedtextField when ToolTip is set
  28  * @author Alexander Potochkin
  29  * @run main Test6256140
  30  */
  31 
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.KeyEvent;
  35 
  36 public class Test6256140 {
  37 
  38     private static volatile JFormattedTextField ft;
  39 
  40     private final static String initialText = "value";
  41     private final static JLabel toolTipLabel = new JLabel("tip");
  42 
  43     public static void main(String[] args) throws Exception {
  44 
  45         Robot robot = new Robot();
  46         robot.setAutoDelay(10);
  47 
  48         SwingUtilities.invokeAndWait(new Runnable() {
  49             public void run() {
  50                 createAndShowGUI();
  51             }
  52         });
  53         robot.waitForIdle();
  54 
  55         Point point = ft.getLocationOnScreen();
  56         robot.mouseMove(point.x, point.y);
  57         robot.mouseMove(point.x + 3, point.y + 3);
  58 
  59         robot.keyPress(KeyEvent.VK_A);
  60         robot.keyRelease(KeyEvent.VK_A);
  61         robot.waitForIdle();
  62 
  63         if (!isTooltipShowning()) {
  64             throw new RuntimeException("Tooltip is not shown");
  65         }
  66 
  67         robot.keyPress(KeyEvent.VK_ESCAPE);
  68         robot.keyRelease(KeyEvent.VK_ESCAPE);
  69         robot.waitForIdle();
  70 
  71         if (isTooltipShowning()) {
  72             throw new RuntimeException("Tooltip must be hidden now");
  73         }
  74 
  75         if (isTextEqual()) {
  76             throw new RuntimeException("FormattedTextField must *not* cancel the updated value this time");
  77         }
  78 
  79         robot.keyPress(KeyEvent.VK_ESCAPE);
  80         robot.keyRelease(KeyEvent.VK_ESCAPE);
  81         robot.waitForIdle();
  82 
  83         if (!isTextEqual()) {
  84             throw new RuntimeException("FormattedTextField must cancel the updated value");
  85         }
  86     }
  87 
  88     private static boolean isTooltipShowning() throws Exception {
  89         final boolean[] result = new boolean[1];
  90 
  91         SwingUtilities.invokeAndWait(new Runnable() {
  92             @Override
  93             public void run() {
  94                 result[0] = toolTipLabel.isShowing();
  95             }
  96         });
  97 
  98         return result[0];
  99     }
 100 
 101     private static boolean isTextEqual() throws Exception {
 102         final boolean[] result = new boolean[1];
 103 
 104         SwingUtilities.invokeAndWait(new Runnable() {
 105             @Override
 106             public void run() {
 107                 result[0] = initialText.equals(ft.getText());
 108             }
 109         });
 110 
 111         return result[0];
 112     }
 113 
 114     private static void createAndShowGUI() {
 115         ToolTipManager.sharedInstance().setDismissDelay(Integer.MAX_VALUE);
 116         ToolTipManager.sharedInstance().setInitialDelay(0);
 117 
 118         final JFrame frame = new JFrame();
 119         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 120         frame.setLayout(new FlowLayout());
 121 
 122         ft = new JFormattedTextField() {
 123 
 124             public JToolTip createToolTip() {
 125                 JToolTip toolTip = super.createToolTip();
 126                 toolTip.setLayout(new BorderLayout());
 127                 toolTip.add(toolTipLabel);
 128                 return toolTip;
 129             }
 130         };
 131         ft.setToolTipText("   ");
 132         ft.setValue(initialText);
 133         frame.add(ft);
 134 
 135         frame.pack();
 136         frame.setLocationRelativeTo(null);
 137         frame.setVisible(true);
 138         ft.requestFocus();
 139     }
 140 }