5342N/A/*
5342N/A * Copyright (c) 2011 Oracle and/or its affiliates. All rights reserved.
5342N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5342N/A *
5342N/A * This code is free software; you can redistribute it and/or modify it
5342N/A * under the terms of the GNU General Public License version 2 only, as
5342N/A * published by the Free Software Foundation.
5342N/A *
5342N/A * This code is distributed in the hope that it will be useful, but WITHOUT
5342N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5342N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
5342N/A * version 2 for more details (a copy is included in the LICENSE file that
5342N/A * accompanied this code).
5342N/A *
5342N/A * You should have received a copy of the GNU General Public License version
5342N/A * 2 along with this work; if not, write to the Free Software Foundation,
5342N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5342N/A *
5342N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5342N/A * or visit www.oracle.com if you need additional information or have any
5342N/A * questions.
5342N/A */
5342N/A
5342N/A/*
5342N/A * Portions Copyright (c) 2011 IBM Corporation
5342N/A */
5342N/A
5342N/A/* @test
5342N/A * @bug 7049024
5342N/A * @summary DnD fails with JTextArea and JTextField
5342N/A * @author Sean Chou
5342N/A */
5342N/A
5342N/Aimport sun.awt.SunToolkit;
5342N/A
5342N/Aimport javax.swing.*;
5342N/Aimport javax.swing.text.DefaultCaret;
5342N/Aimport java.awt.*;
5342N/Aimport java.awt.datatransfer.Clipboard;
5342N/Aimport java.awt.datatransfer.DataFlavor;
5342N/A
5342N/Apublic class bug7049024 {
5342N/A public static Clipboard clipboard = null;
5342N/A
5342N/A public static JTextField textField = null;
5342N/A
5342N/A // This button is used to move focus away from textField.
5342N/A public static JButton button = null;
5342N/A
5342N/A public static JFrame frame = null;
5342N/A
5342N/A public static DefaultCaret caret = null;
5342N/A
5342N/A public static void main(String[] args) throws Exception {
5342N/A
5342N/A SunToolkit toolkit = (SunToolkit) Toolkit.getDefaultToolkit();
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A frame = new JFrame("Test");
5342N/A textField = new JTextField("test selection for textfield");
5342N/A button = new JButton("To compete the focus");
5342N/A
5342N/A frame.setLayout(new FlowLayout());
5342N/A frame.getContentPane().add(textField);
5342N/A frame.getContentPane().add(button);
5342N/A
5342N/A frame.pack();
5342N/A frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
5342N/A frame.setVisible(true);
5342N/A }
5342N/A });
5342N/A toolkit.realSync();
5342N/A
5342N/A clipboard = textField.getToolkit().getSystemSelection();
5342N/A if (null == clipboard) {
5342N/A return;
5342N/A }
5342N/A
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A textField.requestFocusInWindow();
5342N/A }
5342N/A });
5342N/A toolkit.realSync();
5342N/A
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A caret = (DefaultCaret) textField.getCaret();
5342N/A caret.setDot(2);
5342N/A caret.moveDot(4);
5342N/A }
5342N/A });
5342N/A toolkit.realSync();
5342N/A
5342N/A String oldSelection = (String) clipboard.getData(DataFlavor.stringFlavor);
5342N/A System.out.println("oldSelection is " + oldSelection);
5342N/A
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A button.requestFocusInWindow();
5342N/A }
5342N/A });
5342N/A toolkit.realSync(); // So JTextField loses the focus.
5342N/A
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A caret.setDot(4);
5342N/A caret.moveDot(6);
5342N/A }
5342N/A });
5342N/A toolkit.realSync();
5342N/A
5342N/A String newSelection = (String) clipboard.getData(DataFlavor.stringFlavor);
5342N/A System.out.println("newSelection is " + newSelection);
5342N/A
5342N/A boolean passed = newSelection.equals(oldSelection);
5342N/A
5342N/A SwingUtilities.invokeAndWait(new Runnable() {
5342N/A @Override
5342N/A public void run() {
5342N/A frame.dispose();
5342N/A }
5342N/A });
5342N/A
5342N/A if (!passed) {
5342N/A throw new RuntimeException("The test for bug 7049024 failed");
5342N/A }
5342N/A }
5342N/A}