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