1 /* 2 * Copyright (c) 2016, 2020, 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 import java.awt.BorderLayout; 25 import java.awt.Component; 26 import java.awt.Dimension; 27 import java.awt.GraphicsDevice; 28 import java.awt.GraphicsEnvironment; 29 import java.awt.Point; 30 import java.awt.Rectangle; 31 import java.awt.Robot; 32 import java.awt.event.InputEvent; 33 import javax.swing.JFrame; 34 import javax.swing.JPanel; 35 import javax.swing.JTextArea; 36 import javax.swing.SwingUtilities; 37 38 /** 39 * @test 40 * @key headful 41 * @bug 8149849 8211999 42 * @summary [hidpi] DnD issues (cannot DnD from JFileChooser to JEditorPane or 43 * other text component) when scale > 1 44 * @run main/othervm DNDTextToScaledArea 45 * @run main/othervm -Dsun.java2d.uiScale=2 DNDTextToScaledArea 46 */ 47 public class DNDTextToScaledArea { 48 49 private static final int SIZE = 300; 50 private static final String TEXT = "ABCDEFGH"; 51 private static JFrame frame; 52 private static JTextArea srcTextArea; 53 private static JTextArea dstTextArea; 54 private static volatile Point srcPoint; 55 private static volatile Point dstPoint; 56 private static volatile boolean passed = false; 57 58 public static void main(String[] args) throws Exception { 59 var lge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 60 for (GraphicsDevice device : lge.getScreenDevices()) { 61 test(device); 62 } 63 } 64 65 private static void test(GraphicsDevice device) throws Exception { 66 Robot robot = new Robot(); 67 robot.setAutoDelay(50); 68 69 SwingUtilities.invokeAndWait(() -> createAndShowGUI(device)); 70 robot.waitForIdle(); 71 72 SwingUtilities.invokeAndWait(() -> { 73 srcPoint = getPoint(srcTextArea, 0.1); 74 dstPoint = getPoint(dstTextArea, 0.75); 75 }); 76 robot.waitForIdle(); 77 78 dragAndDrop(robot, srcPoint, dstPoint); 79 robot.waitForIdle(); 80 81 SwingUtilities.invokeAndWait(() -> { 82 passed = TEXT.equals(dstTextArea.getText()); 83 frame.dispose(); 84 }); 85 robot.waitForIdle(); 86 87 if (!passed) { 88 throw new RuntimeException("Text Drag and Drop failed!"); 89 } 90 } 91 92 private static void createAndShowGUI(GraphicsDevice device) { 93 frame = new JFrame(device.getDefaultConfiguration()); 94 Rectangle screen = device.getDefaultConfiguration().getBounds(); 95 int x = (int) (screen.getCenterX() - SIZE / 2); 96 int y = (int) (screen.getCenterY() - SIZE / 2); 97 frame.setBounds(x, y, SIZE, SIZE); 98 99 JPanel panel = new JPanel(new BorderLayout()); 100 101 srcTextArea = new JTextArea(TEXT); 102 srcTextArea.setDragEnabled(true); 103 srcTextArea.selectAll(); 104 dstTextArea = new JTextArea(); 105 106 panel.add(dstTextArea, BorderLayout.CENTER); 107 panel.add(srcTextArea, BorderLayout.SOUTH); 108 109 frame.getContentPane().add(panel); 110 frame.setVisible(true); 111 } 112 113 private static Point getPoint(Component component, double scale) { 114 Point point = component.getLocationOnScreen(); 115 Dimension bounds = component.getSize(); 116 point.translate((int) (bounds.width * scale), (int) (bounds.height * scale)); 117 return point; 118 } 119 120 public static void dragAndDrop(Robot robot, Point src, Point dst) throws Exception { 121 122 int x1 = src.x; 123 int y1 = src.y; 124 int x2 = dst.x; 125 int y2 = dst.y; 126 robot.mouseMove(x1, y1); 127 robot.mousePress(InputEvent.BUTTON1_MASK); 128 129 float dmax = (float) Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1)); 130 float dx = (x2 - x1) / dmax; 131 float dy = (y2 - y1) / dmax; 132 133 for (int i = 0; i <= dmax; i += 5) { 134 robot.mouseMove((int) (x1 + dx * i), (int) (y1 + dy * i)); 135 } 136 137 robot.mouseRelease(InputEvent.BUTTON1_MASK); 138 } 139 }