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