< prev index next >

test/jdk/javax/swing/JTextArea/8149849/DNDTextToScaledArea.java

Print this page
rev 60071 : 8211999: Window positioning bugs due to overlapping GraphicsDevice bounds (Windows/HiDPI)
Reviewed-by: XXX
   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;


   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;


< prev index next >