Skip to content

Commit ddc27a6

Browse files
committed
8353942: Open source Swing Tests - Set 5
Backport-of: 9a0cff692d6f96b8c89b1510cd2b4b1a8e318b6e
1 parent 994b89d commit ddc27a6

File tree

5 files changed

+495
-0
lines changed

5 files changed

+495
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2001, 2025, 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+
/*
25+
* @test
26+
* @bug 4514071
27+
* @summary Tests that JTable, JList and JTree provide drag-over feedback.
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual DragOverFeedbackTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.GridLayout;
37+
import java.awt.datatransfer.DataFlavor;
38+
import java.awt.event.MouseAdapter;
39+
import java.awt.event.MouseEvent;
40+
import javax.swing.BorderFactory;
41+
import javax.swing.JComponent;
42+
import javax.swing.JFrame;
43+
import javax.swing.JLabel;
44+
import javax.swing.JList;
45+
import javax.swing.JPanel;
46+
import javax.swing.JTable;
47+
import javax.swing.JTree;
48+
import javax.swing.TransferHandler;
49+
50+
public class DragOverFeedbackTest {
51+
private static final String INSTRUCTIONS = """
52+
This test is designed to make sure that JTable, JTree, and JList
53+
provide visual feedback when a DnD drag operation occurs over them.
54+
55+
Click on the label where it says "DRAG FROM HERE" and begin dragging.
56+
Drag over each of the three components (JTable, JTree, JList).
57+
While you're dragging over them, they should visually indicate the
58+
location where a drop would occur. This visual indication may use the
59+
selection but could be some other visual.
60+
61+
If above is true press PASS else press FAIL.
62+
""";
63+
64+
public static void main(String[] args) throws Exception {
65+
PassFailJFrame.builder()
66+
.instructions(INSTRUCTIONS)
67+
.columns(50)
68+
.testUI(DragOverFeedbackTest::createTestUI)
69+
.build()
70+
.awaitAndCheck();
71+
}
72+
73+
private static final TransferHandler handler = new TransferHandler() {
74+
public boolean canImport(JComponent comp, DataFlavor[] flavors) {
75+
return true;
76+
}
77+
};
78+
79+
private static JFrame createTestUI() {
80+
JFrame frame = new JFrame("DragOverFeedbackTest");
81+
final JLabel label = new JLabel("DRAG FROM HERE");
82+
label.setPreferredSize(new Dimension(400, 25));
83+
label.setTransferHandler(new TransferHandler("text"));
84+
label.addMouseListener(new MouseAdapter() {
85+
public void mousePressed(MouseEvent me) {
86+
label.getTransferHandler().exportAsDrag(label, me, TransferHandler.COPY);
87+
}
88+
});
89+
JTable table = new JTable(
90+
new String[][] {{"one"}, {"two"}, {"three"}, {"four"}},
91+
new String[] {"1"});
92+
table.setRowSelectionInterval(1, 1);
93+
table.setTransferHandler(handler);
94+
95+
JList list = new JList(new String[] {"one", "two", "three", "four"});
96+
list.setSelectedIndex(1);
97+
list.setTransferHandler(handler);
98+
99+
JTree tree = new JTree();
100+
tree.setSelectionRow(1);
101+
tree.setTransferHandler(handler);
102+
103+
frame.add(label, BorderLayout.NORTH);
104+
105+
JPanel wrapper = new JPanel();
106+
wrapper.setLayout(new GridLayout(3, 1));
107+
table.setBorder(BorderFactory.createLineBorder(Color.BLACK));
108+
wrapper.add(table);
109+
list.setBorder(BorderFactory.createLineBorder(Color.BLACK));
110+
wrapper.add(list);
111+
tree.setBorder(BorderFactory.createLineBorder(Color.BLACK));
112+
wrapper.add(tree);
113+
frame.add(wrapper);
114+
frame.setSize(500, 500);
115+
return frame;
116+
}
117+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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+
/*
25+
* @test
26+
* @bug 4546134
27+
* @summary Tests that JList shows the right drop location when it has multiple columns.
28+
* @library /java/awt/regtesthelpers
29+
* @build PassFailJFrame
30+
* @run main/manual ListDragOverFeedbackTest
31+
*/
32+
33+
import java.awt.BorderLayout;
34+
import java.awt.Color;
35+
import java.awt.Dimension;
36+
import java.awt.GridLayout;
37+
import java.awt.datatransfer.DataFlavor;
38+
import java.awt.event.MouseAdapter;
39+
import java.awt.event.MouseEvent;
40+
import javax.swing.BorderFactory;
41+
import javax.swing.JComponent;
42+
import javax.swing.JFrame;
43+
import javax.swing.JLabel;
44+
import javax.swing.JList;
45+
import javax.swing.JPanel;
46+
import javax.swing.TransferHandler;
47+
48+
public class ListDragOverFeedbackTest {
49+
private static final String INSTRUCTIONS = """
50+
JList should provide visual feedback when a DnD drag operation is
51+
occurring over it. This test is to check that it provides the
52+
feedback about the drop location correctly.
53+
54+
Click on the label where it says "DRAG FROM HERE" and begin dragging.
55+
Drag over each column in each of the three JLists and make sure that
56+
the drop location indicated is appropriate for the mouse location. For
57+
instance, if the mouse is in the first column, the drop location should
58+
also be indicated in that first column.
59+
60+
If above is true press PASS else press FAIL.
61+
""";
62+
63+
public static void main(String[] args) throws Exception {
64+
PassFailJFrame.builder()
65+
.instructions(INSTRUCTIONS)
66+
.columns(50)
67+
.testUI(ListDragOverFeedbackTest::createTestUI)
68+
.build()
69+
.awaitAndCheck();
70+
}
71+
72+
private static final TransferHandler handler = new TransferHandler() {
73+
public boolean canImport(JComponent comp, DataFlavor[] flavors) {
74+
return true;
75+
}
76+
};
77+
78+
private static JFrame createTestUI() {
79+
String[] vals = new String[] {
80+
"one", "two", "three", "four", "five", "six", "seven", "eight",
81+
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen"};
82+
83+
JFrame frame = new JFrame("ListDragOverFeedbackTest");
84+
final JLabel label = new JLabel("DRAG FROM HERE");
85+
label.setPreferredSize(new Dimension(400, 25));
86+
label.setTransferHandler(new TransferHandler("text"));
87+
label.addMouseListener(new MouseAdapter() {
88+
public void mousePressed(MouseEvent me) {
89+
label.getTransferHandler().exportAsDrag(label, me,
90+
TransferHandler.COPY);
91+
}
92+
});
93+
94+
JList list1 = new JList(vals);
95+
list1.setTransferHandler(handler);
96+
list1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
97+
98+
JList list2 = new JList(vals);
99+
list2.setLayoutOrientation(JList.VERTICAL_WRAP);
100+
list2.setTransferHandler(handler);
101+
list2.setBorder(BorderFactory.createLineBorder(Color.BLACK));
102+
103+
JList list3 = new JList(vals);
104+
list3.setLayoutOrientation(JList.HORIZONTAL_WRAP);
105+
list3.setTransferHandler(handler);
106+
list3.setBorder(BorderFactory.createLineBorder(Color.BLACK));
107+
108+
JPanel wrapper = new JPanel();
109+
wrapper.setLayout(new GridLayout(3, 1));
110+
wrapper.add(list1);
111+
wrapper.add(list2);
112+
wrapper.add(list3);
113+
114+
frame.add(label, BorderLayout.NORTH);
115+
frame.add(wrapper);
116+
frame.setSize(400, 500);
117+
return frame;
118+
}
119+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2002, 2025, 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+
/*
25+
* @test
26+
* @key headful
27+
* @bug 4655513
28+
* @summary TransferHandler doesn't recognize ACTION_LINK
29+
as a valid drop action
30+
* @library /javax/swing/regtesthelpers
31+
* @build Util
32+
* @run main bug4655513
33+
*/
34+
35+
import java.awt.BorderLayout;
36+
import java.awt.Color;
37+
import java.awt.Dimension;
38+
import java.awt.Point;
39+
import java.awt.Robot;
40+
import java.awt.datatransfer.StringSelection;
41+
import java.awt.dnd.DnDConstants;
42+
import java.awt.dnd.DragGestureRecognizer;
43+
import java.awt.dnd.DragSource;
44+
import java.awt.event.InputEvent;
45+
import java.awt.event.MouseEvent;
46+
import javax.swing.JEditorPane;
47+
import javax.swing.JFrame;
48+
import javax.swing.JLabel;
49+
import javax.swing.JScrollPane;
50+
import javax.swing.SwingUtilities;
51+
52+
public class bug4655513 {
53+
private static final String LINK_URL = "http://www.example.com";
54+
private static volatile JEditorPane editor;
55+
private static volatile JLabel dragSource;
56+
private static JFrame frame;
57+
58+
public static void main(String[] args) throws Exception {
59+
try {
60+
Robot robot = new Robot();
61+
SwingUtilities.invokeAndWait(bug4655513::createAndShowGUI);
62+
robot.waitForIdle();
63+
robot.delay(1000);
64+
65+
Point dragStartLoc = Util.getCenterPoint(dragSource);
66+
Point dragEndLoc = Util.getCenterPoint(editor);
67+
robot.mouseMove(dragStartLoc.x, dragStartLoc.y);
68+
robot.mousePress(MouseEvent.BUTTON1_DOWN_MASK);
69+
for (int y = dragStartLoc.y; y < dragEndLoc.y; y += 3) {
70+
robot.mouseMove(dragStartLoc.x, y);
71+
robot.delay(50);
72+
}
73+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
74+
robot.delay(500);
75+
76+
SwingUtilities.invokeAndWait(() -> {
77+
if (!editor.getText().contains(LINK_URL)) {
78+
throw new RuntimeException("Test Failed! Drag & Drop did not work.");
79+
}
80+
});
81+
} finally {
82+
SwingUtilities.invokeAndWait(frame::dispose);
83+
}
84+
}
85+
86+
private static void createAndShowGUI() {
87+
frame = new JFrame("Bug4655513 - Data Transfer");
88+
dragSource = new JLabel("To Test DnD, drag this label.");
89+
dragSource.setForeground(Color.RED);
90+
dragSource.setPreferredSize(new Dimension(250, 50));
91+
frame.add(dragSource, BorderLayout.NORTH);
92+
93+
editor = new JEditorPane("text/plain", "Drop here.");
94+
editor.setPreferredSize(new Dimension(250, 50));
95+
frame.add(new JScrollPane(editor), BorderLayout.CENTER);
96+
97+
DragSource ds = new DragSource();
98+
DragGestureRecognizer rec =
99+
ds.createDefaultDragGestureRecognizer(dragSource,
100+
DnDConstants.ACTION_LINK,
101+
dge -> dge.startDrag(null, new StringSelection(LINK_URL)));
102+
frame.setSize(300, 150);
103+
frame.setLocationRelativeTo(null);
104+
frame.setVisible(true);
105+
}
106+
}

0 commit comments

Comments
 (0)