bug7049024.java revision 11018:66682f651425
1/* 2 * Copyright (c) 2011, 2012, 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 * Portions Copyright (c) 2011 IBM Corporation 26 */ 27 28/* @test 29 * @bug 7049024 30 * @summary DnD fails with JTextArea and JTextField 31 * @author Sean Chou 32 */ 33 34import javax.swing.*; 35import javax.swing.text.DefaultCaret; 36import java.awt.*; 37import java.awt.datatransfer.Clipboard; 38import java.awt.datatransfer.DataFlavor; 39 40public class bug7049024 { 41 public static Clipboard clipboard = null; 42 43 public static JTextField textField = null; 44 45 // This button is used to move focus away from textField. 46 public static JButton button = null; 47 48 public static JFrame frame = null; 49 50 public static DefaultCaret caret = null; 51 52 public static void main(String[] args) throws Exception { 53 54 Robot robot = new Robot(); 55 SwingUtilities.invokeAndWait(new Runnable() { 56 @Override 57 public void run() { 58 frame = new JFrame("Test"); 59 textField = new JTextField("test selection for textfield"); 60 button = new JButton("To compete the focus"); 61 62 frame.setLayout(new FlowLayout()); 63 frame.getContentPane().add(textField); 64 frame.getContentPane().add(button); 65 66 frame.pack(); 67 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 68 frame.setVisible(true); 69 } 70 }); 71 robot.waitForIdle(); 72 73 clipboard = textField.getToolkit().getSystemSelection(); 74 if (null == clipboard) { 75 return; 76 } 77 78 SwingUtilities.invokeAndWait(new Runnable() { 79 @Override 80 public void run() { 81 textField.requestFocusInWindow(); 82 } 83 }); 84 robot.waitForIdle(); 85 86 SwingUtilities.invokeAndWait(new Runnable() { 87 @Override 88 public void run() { 89 caret = (DefaultCaret) textField.getCaret(); 90 caret.setDot(2); 91 caret.moveDot(4); 92 } 93 }); 94 robot.waitForIdle(); 95 96 String oldSelection = (String) clipboard.getData(DataFlavor.stringFlavor); 97 System.out.println("oldSelection is " + oldSelection); 98 99 SwingUtilities.invokeAndWait(new Runnable() { 100 @Override 101 public void run() { 102 button.requestFocusInWindow(); 103 } 104 }); 105 robot.waitForIdle(); // So JTextField loses the focus. 106 107 SwingUtilities.invokeAndWait(new Runnable() { 108 @Override 109 public void run() { 110 caret.setDot(4); 111 caret.moveDot(6); 112 } 113 }); 114 robot.waitForIdle(); 115 116 String newSelection = (String) clipboard.getData(DataFlavor.stringFlavor); 117 System.out.println("newSelection is " + newSelection); 118 119 boolean passed = newSelection.equals(oldSelection); 120 121 SwingUtilities.invokeAndWait(new Runnable() { 122 @Override 123 public void run() { 124 frame.dispose(); 125 } 126 }); 127 128 if (!passed) { 129 throw new RuntimeException("The test for bug 7049024 failed"); 130 } 131 } 132} 133