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