1 /*
  2  * Copyright (c) 2003, 2023, 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 4863121
 27  * @summary JFormattedTextField's NotifyAction should invoke invalidEdit if
 28    commit fails
 29  * @key headful
 30  * @run main bug4863121
 31  */
 32 
 33 import java.awt.Robot;
 34 import java.awt.event.FocusAdapter;
 35 import java.awt.event.FocusEvent;
 36 import java.awt.event.KeyEvent;
 37 import java.text.Format;
 38 import java.text.DecimalFormat;
 39 import javax.swing.JFormattedTextField;
 40 import javax.swing.JFrame;
 41 import javax.swing.SwingUtilities;
 42 
 43 public class bug4863121 {
 44 
 45     static TestFormattedTextField ftf;
 46     static JFrame fr;
 47     static Robot robot;
 48 
 49     private static volatile boolean focused = false;
 50     private static volatile boolean passed = false;
 51 
 52     public static void main(String[] args) throws Exception {
 53         try {
 54             robot = new Robot();
 55             robot.setAutoDelay(100);
 56             SwingUtilities.invokeAndWait(() -> {
 57                 fr = new JFrame("Test");
 58                 ftf = new TestFormattedTextField(new DecimalFormat("####"));
 59                 ftf.setText("q");
 60                 fr.getContentPane().add(ftf);
 61 
 62                 ftf.addFocusListener(new FocusAdapter() {
 63                     public void focusGained(FocusEvent e) {
 64                         focused = true;
 65                     }
 66                 });
 67                 fr.pack();
 68                 fr.setLocationRelativeTo(null);
 69                 fr.setVisible(true);
 70             });
 71             robot.waitForIdle();
 72             robot.delay(1000);
 73             SwingUtilities.invokeAndWait(() -> {
 74                 ftf.requestFocus();
 75             });
 76             robot.waitForIdle();
 77             robot.delay(500);
 78             robot.keyPress(KeyEvent.VK_ENTER);
 79             robot.keyRelease(KeyEvent.VK_ENTER);
 80             if (!passed) {
 81                 throw new RuntimeException("JFormattedTextField's NotifyAction " +
 82                         "should invoke invalidEdit if commit fails");
 83             }
 84         } finally {
 85             SwingUtilities.invokeAndWait(() -> {
 86                 if (fr != null) {
 87                     fr.dispose();
 88                 }
 89             });
 90         }
 91     }
 92 
 93     public static class TestFormattedTextField extends JFormattedTextField {
 94         public TestFormattedTextField(Format f) {
 95             super(f);
 96         }
 97         protected void invalidEdit() {
 98             passed = true;
 99         }
100     }
101 }