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