Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/swing/JTextArea/8149849/DNDTextToScaledArea.java
41153 views
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(150);
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
// check the destination
78
robot.mouseMove(dstPoint.x, dstPoint.y);
79
robot.mousePress(InputEvent.BUTTON1_MASK);
80
robot.mouseRelease(InputEvent.BUTTON1_MASK);
81
robot.waitForIdle();
82
83
dragAndDrop(robot, srcPoint, dstPoint);
84
robot.waitForIdle();
85
86
SwingUtilities.invokeAndWait(() -> {
87
passed = TEXT.equals(dstTextArea.getText());
88
frame.dispose();
89
});
90
robot.waitForIdle();
91
92
if (!passed) {
93
throw new RuntimeException("Text Drag and Drop failed!");
94
}
95
}
96
97
private static void createAndShowGUI(GraphicsDevice device) {
98
frame = new JFrame(device.getDefaultConfiguration());
99
Rectangle screen = device.getDefaultConfiguration().getBounds();
100
int x = (int) (screen.getCenterX() - SIZE / 2);
101
int y = (int) (screen.getCenterY() - SIZE / 2);
102
frame.setBounds(x, y, SIZE, SIZE);
103
104
JPanel panel = new JPanel(new BorderLayout());
105
106
srcTextArea = new JTextArea(TEXT);
107
srcTextArea.setDragEnabled(true);
108
srcTextArea.selectAll();
109
dstTextArea = new JTextArea();
110
111
panel.add(dstTextArea, BorderLayout.CENTER);
112
panel.add(srcTextArea, BorderLayout.SOUTH);
113
114
frame.getContentPane().add(panel);
115
frame.setVisible(true);
116
}
117
118
private static Point getPoint(Component component, double scale) {
119
Point point = component.getLocationOnScreen();
120
Dimension bounds = component.getSize();
121
point.translate((int) (bounds.width * scale), (int) (bounds.height * scale));
122
return point;
123
}
124
125
public static void dragAndDrop(Robot robot, Point src, Point dst) throws Exception {
126
127
int x1 = src.x;
128
int y1 = src.y;
129
int x2 = dst.x;
130
int y2 = dst.y;
131
robot.mouseMove(x1, y1);
132
robot.mousePress(InputEvent.BUTTON1_MASK);
133
134
float dmax = (float) Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
135
float dx = (x2 - x1) / dmax;
136
float dy = (y2 - y1) / dmax;
137
138
for (int i = 0; i <= dmax; i += 5) {
139
robot.mouseMove((int) (x1 + dx * i), (int) (y1 + dy * i));
140
}
141
142
robot.mouseRelease(InputEvent.BUTTON1_MASK);
143
}
144
}
145
146