Skip to content

Commit eeb2132

Browse files
committed
Backport 8316149+8316627 from 21
1 parent 3f1ba56 commit eeb2132

File tree

5 files changed

+370
-0
lines changed

5 files changed

+370
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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+
/* @test
25+
* @bug 4696499
26+
* @summary new tree model asked about nodes of previous tree model
27+
* @run main bug4696499
28+
*/
29+
30+
import java.util.ArrayList;
31+
32+
import javax.swing.JTree;
33+
import javax.swing.event.TreeModelListener;
34+
import javax.swing.tree.DefaultMutableTreeNode;
35+
import javax.swing.tree.TreeModel;
36+
import javax.swing.tree.TreeNode;
37+
import javax.swing.tree.TreePath;
38+
39+
public class bug4696499 {
40+
public static void main(String[] args) throws Exception {
41+
JTree tree = new JTree();
42+
TreeModel model = new MyModel();
43+
tree.setModel(model);
44+
45+
tree.setSelectionRow(1);
46+
model = new MyModel();
47+
tree.setModel(model);
48+
}
49+
}
50+
51+
class MyModel implements TreeModel {
52+
private Object root = "Root";
53+
private ArrayList listeners = new ArrayList();
54+
private TreeNode ONE;
55+
static int next = 1;
56+
57+
MyModel() {
58+
ONE = new DefaultMutableTreeNode(next);
59+
next *= 2;
60+
}
61+
62+
public void addTreeModelListener(TreeModelListener l) {
63+
listeners.add(l);
64+
}
65+
66+
public void removeTreeModelListener(TreeModelListener l) {
67+
listeners.remove(l);
68+
}
69+
70+
public void valueForPathChanged(TreePath tp, Object newValue) {
71+
}
72+
73+
public Object getRoot() {
74+
return root;
75+
}
76+
77+
public boolean isLeaf(Object o) {
78+
return o == ONE;
79+
}
80+
81+
public int getIndexOfChild(Object parent, Object child) {
82+
if (parent != root || child != ONE) {
83+
throw new RuntimeException("This method is called with the child " +
84+
"of the previous tree model");
85+
}
86+
return 0;
87+
}
88+
89+
public int getChildCount(Object o) {
90+
if (o == root) {
91+
return 1;
92+
}
93+
if (o == ONE) {
94+
return 0;
95+
}
96+
throw new IllegalArgumentException(o.toString());
97+
}
98+
99+
public Object getChild(Object o, int index) {
100+
if (o != root || index != 0) {
101+
throw new IllegalArgumentException(o + ", " + index);
102+
}
103+
return ONE;
104+
}
105+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright (c) 2004, 2023, 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+
/* @test
25+
* @bug 5039542
26+
* @summary JTree's setToolTipText() doesn't work
27+
* @run main bug5039542
28+
*/
29+
30+
import javax.swing.JTree;
31+
32+
public class bug5039542 {
33+
public static void main(String[] args) throws Exception {
34+
final String exampleStr = "TEST";
35+
JTree tree = new JTree();
36+
tree.setToolTipText(exampleStr);
37+
if (tree.getToolTipText(null) != exampleStr) {
38+
throw new RuntimeException("The default JTree tooltip text " +
39+
"have to be used if renderer doesn't provide it.");
40+
}
41+
}
42+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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+
/* @test
25+
* @bug 4546474
26+
* @summary JScrollPane's always-visible scrollbars not updated when
27+
* viewport is replaced
28+
* @run main bug4546474
29+
*/
30+
31+
import javax.swing.JPanel;
32+
import javax.swing.JScrollBar;
33+
import javax.swing.JScrollPane;
34+
35+
public class bug4546474 {
36+
public static void main(String[] args) {
37+
JPanel panel = new JPanel();
38+
JScrollPane scrollpane = new JScrollPane(panel,
39+
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
40+
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
41+
JScrollBar sbar = scrollpane.getVerticalScrollBar();
42+
43+
scrollpane.setViewportView(null);
44+
45+
if (sbar.getVisibleAmount() > 0) {
46+
throw new RuntimeException("Vertical scrollbar is not " +
47+
"updated when viewport is replaced");
48+
}
49+
}
50+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2003, 2023, 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+
/* @test
25+
* @bug 4677611
26+
* @summary JViewport sets Opaque after UpdateUI (prevents UI delegates
27+
* to determine look)
28+
* @run main bug4677611
29+
*/
30+
31+
import java.awt.Color;
32+
33+
import javax.swing.JScrollPane;
34+
import javax.swing.JViewport;
35+
36+
public class bug4677611 {
37+
public static void main(String[] args) throws Exception {
38+
JScrollPane sp = new JScrollPane();
39+
JViewport vp = new MyViewport();
40+
vp.setBackground(Color.blue);
41+
sp.setViewport(vp);
42+
43+
if (vp.isOpaque()) {
44+
throw new RuntimeException("JViewport shouldn't set Opaque " +
45+
"after update the UI");
46+
}
47+
}
48+
49+
static class MyViewport extends JViewport {
50+
public void updateUI() {
51+
setOpaque(false);
52+
super.updateUI();
53+
}
54+
}
55+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* Copyright (c) 2000, 2023, 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+
/* @test
25+
* @bug 4345798
26+
* @summary Tests if Pressing enter to dismiss menu works when a JRootPane
27+
* has a default button.
28+
* @key headful
29+
* @run main bug4345798
30+
*/
31+
32+
import java.awt.Point;
33+
import java.awt.Robot;
34+
import java.awt.event.ActionEvent;
35+
import java.awt.event.ActionListener;
36+
import java.awt.event.InputEvent;
37+
import java.awt.event.KeyEvent;
38+
39+
import javax.swing.JButton;
40+
import javax.swing.JFrame;
41+
import javax.swing.JMenu;
42+
import javax.swing.JMenuBar;
43+
import javax.swing.JMenuItem;
44+
import javax.swing.JRootPane;
45+
import javax.swing.SwingUtilities;
46+
47+
public class bug4345798 {
48+
private static JFrame f;
49+
private static JButton b;
50+
private static JMenu menu;
51+
private static volatile boolean passed = true;
52+
private static volatile Point p;
53+
54+
public static void main(String[] args) throws Exception {
55+
try {
56+
SwingUtilities.invokeAndWait(() -> {
57+
f = new JFrame("bug4345798");
58+
JMenuBar mbar = new JMenuBar();
59+
JMenuItem item = new JMenuItem("Open...");
60+
menu = new JMenu("File");
61+
item.addActionListener(new TestActionListener());
62+
menu.add(item);
63+
mbar.add(menu);
64+
65+
f.setJMenuBar(mbar);
66+
67+
b = new JButton("Default");
68+
b.addActionListener(new TestActionListener());
69+
f.getContentPane().add(b);
70+
JRootPane rp = f.getRootPane();
71+
rp.setDefaultButton(b);
72+
73+
f.setSize(200, 200);
74+
f.setLocationRelativeTo(null);
75+
f.setVisible(true);
76+
b.requestFocus();
77+
});
78+
79+
Robot robot = new Robot();
80+
robot.setAutoDelay(100);
81+
robot.waitForIdle();
82+
robot.delay(1000);
83+
84+
SwingUtilities.invokeAndWait(() -> p = menu.getLocationOnScreen());
85+
robot.mouseMove(p.x, p.y);
86+
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
87+
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
88+
89+
robot.keyPress(KeyEvent.VK_F10);
90+
robot.keyRelease(KeyEvent.VK_F10);
91+
92+
robot.keyPress(KeyEvent.VK_DOWN);
93+
robot.keyRelease(KeyEvent.VK_DOWN);
94+
95+
robot.keyPress(KeyEvent.VK_ENTER);
96+
robot.keyRelease(KeyEvent.VK_ENTER);
97+
} finally {
98+
SwingUtilities.invokeAndWait(() -> {
99+
if (f != null) {
100+
f.dispose();
101+
}
102+
});
103+
}
104+
105+
if (!passed) {
106+
throw new RuntimeException("Test failed.");
107+
}
108+
}
109+
110+
static class TestActionListener implements ActionListener {
111+
@Override
112+
public void actionPerformed(ActionEvent e) {
113+
if (e.getSource() == b) {
114+
passed = false;
115+
}
116+
}
117+
}
118+
}

0 commit comments

Comments
 (0)