Path: blob/master/test/jdk/javax/swing/JTextArea/8149849/DNDTextToScaledArea.java
41153 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.awt.BorderLayout;24import java.awt.Component;25import java.awt.Dimension;26import java.awt.GraphicsDevice;27import java.awt.GraphicsEnvironment;28import java.awt.Point;29import java.awt.Rectangle;30import java.awt.Robot;31import java.awt.event.InputEvent;32import javax.swing.JFrame;33import javax.swing.JPanel;34import javax.swing.JTextArea;35import javax.swing.SwingUtilities;3637/**38* @test39* @key headful40* @bug 8149849 821199941* @summary [hidpi] DnD issues (cannot DnD from JFileChooser to JEditorPane or42* other text component) when scale > 143* @run main/othervm DNDTextToScaledArea44* @run main/othervm -Dsun.java2d.uiScale=2 DNDTextToScaledArea45*/46public class DNDTextToScaledArea {4748private static final int SIZE = 300;49private static final String TEXT = "ABCDEFGH";50private static JFrame frame;51private static JTextArea srcTextArea;52private static JTextArea dstTextArea;53private static volatile Point srcPoint;54private static volatile Point dstPoint;55private static volatile boolean passed = false;5657public static void main(String[] args) throws Exception {58var lge = GraphicsEnvironment.getLocalGraphicsEnvironment();59for (GraphicsDevice device : lge.getScreenDevices()) {60test(device);61}62}6364private static void test(GraphicsDevice device) throws Exception {65Robot robot = new Robot();66robot.setAutoDelay(150);6768SwingUtilities.invokeAndWait(() -> createAndShowGUI(device));69robot.waitForIdle();7071SwingUtilities.invokeAndWait(() -> {72srcPoint = getPoint(srcTextArea, 0.1);73dstPoint = getPoint(dstTextArea, 0.75);74});75robot.waitForIdle();76// check the destination77robot.mouseMove(dstPoint.x, dstPoint.y);78robot.mousePress(InputEvent.BUTTON1_MASK);79robot.mouseRelease(InputEvent.BUTTON1_MASK);80robot.waitForIdle();8182dragAndDrop(robot, srcPoint, dstPoint);83robot.waitForIdle();8485SwingUtilities.invokeAndWait(() -> {86passed = TEXT.equals(dstTextArea.getText());87frame.dispose();88});89robot.waitForIdle();9091if (!passed) {92throw new RuntimeException("Text Drag and Drop failed!");93}94}9596private static void createAndShowGUI(GraphicsDevice device) {97frame = new JFrame(device.getDefaultConfiguration());98Rectangle screen = device.getDefaultConfiguration().getBounds();99int x = (int) (screen.getCenterX() - SIZE / 2);100int y = (int) (screen.getCenterY() - SIZE / 2);101frame.setBounds(x, y, SIZE, SIZE);102103JPanel panel = new JPanel(new BorderLayout());104105srcTextArea = new JTextArea(TEXT);106srcTextArea.setDragEnabled(true);107srcTextArea.selectAll();108dstTextArea = new JTextArea();109110panel.add(dstTextArea, BorderLayout.CENTER);111panel.add(srcTextArea, BorderLayout.SOUTH);112113frame.getContentPane().add(panel);114frame.setVisible(true);115}116117private static Point getPoint(Component component, double scale) {118Point point = component.getLocationOnScreen();119Dimension bounds = component.getSize();120point.translate((int) (bounds.width * scale), (int) (bounds.height * scale));121return point;122}123124public static void dragAndDrop(Robot robot, Point src, Point dst) throws Exception {125126int x1 = src.x;127int y1 = src.y;128int x2 = dst.x;129int y2 = dst.y;130robot.mouseMove(x1, y1);131robot.mousePress(InputEvent.BUTTON1_MASK);132133float dmax = (float) Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));134float dx = (x2 - x1) / dmax;135float dy = (y2 - y1) / dmax;136137for (int i = 0; i <= dmax; i += 5) {138robot.mouseMove((int) (x1 + dx * i), (int) (y1 + dy * i));139}140141robot.mouseRelease(InputEvent.BUTTON1_MASK);142}143}144145146