|
1 /* |
|
2 * Copyright 2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 6505027 |
|
27 * @summary Tests focus problem inside internal frame |
|
28 * @author Sergey Malenkov |
|
29 */ |
|
30 |
|
31 import java.awt.AWTException; |
|
32 import java.awt.BorderLayout; |
|
33 import java.awt.Component; |
|
34 import java.awt.Container; |
|
35 import java.awt.KeyboardFocusManager; |
|
36 import java.awt.Point; |
|
37 import java.awt.Robot; |
|
38 import java.awt.event.InputEvent; |
|
39 import javax.swing.DefaultCellEditor; |
|
40 import javax.swing.JComboBox; |
|
41 import javax.swing.JDesktopPane; |
|
42 import javax.swing.JFrame; |
|
43 import javax.swing.JInternalFrame; |
|
44 import javax.swing.JScrollPane; |
|
45 import javax.swing.JTable; |
|
46 import javax.swing.JTextField; |
|
47 import javax.swing.SwingUtilities; |
|
48 import javax.swing.WindowConstants; |
|
49 import javax.swing.table.DefaultTableModel; |
|
50 import javax.swing.table.TableColumn; |
|
51 |
|
52 public class Test6505027 implements Runnable { |
|
53 |
|
54 private static final boolean INTERNAL = true; |
|
55 private static final boolean TERMINATE = true; |
|
56 |
|
57 private static final int WIDTH = 450; |
|
58 private static final int HEIGHT = 200; |
|
59 private static final int OFFSET = 10; |
|
60 private static final long PAUSE = 2048L; |
|
61 |
|
62 private static final String[] COLUMNS = { "Size", "Shape" }; // NON-NLS |
|
63 private static final String[] ITEMS = { "a", "b", "c", "d" }; // NON-NLS |
|
64 private static final String KEY = "terminateEditOnFocusLost"; // NON-NLS |
|
65 |
|
66 public static void main(String[] args) { |
|
67 SwingUtilities.invokeLater(new Test6505027()); |
|
68 |
|
69 Component component = null; |
|
70 while (component == null) { |
|
71 try { |
|
72 Thread.sleep(PAUSE); |
|
73 } |
|
74 catch (InterruptedException exception) { |
|
75 // ignore interrupted exception |
|
76 } |
|
77 component = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner(); |
|
78 } |
|
79 if (!component.getClass().equals(JComboBox.class)) { |
|
80 throw new Error("unexpected focus owner: " + component); |
|
81 } |
|
82 SwingUtilities.getWindowAncestor(component).dispose(); |
|
83 } |
|
84 |
|
85 private JTable table; |
|
86 private Point point; |
|
87 |
|
88 public void run() { |
|
89 if (this.table == null) { |
|
90 JFrame main = new JFrame(); |
|
91 main.setSize(WIDTH + OFFSET * 3, HEIGHT + OFFSET * 5); |
|
92 main.setLocationRelativeTo(null); |
|
93 main.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); |
|
94 main.setVisible(true); |
|
95 |
|
96 Container container = main; |
|
97 if (INTERNAL) { |
|
98 JInternalFrame frame = new JInternalFrame(); |
|
99 frame.setBounds(OFFSET, OFFSET, WIDTH, HEIGHT); |
|
100 frame.setVisible(true); |
|
101 |
|
102 JDesktopPane desktop = new JDesktopPane(); |
|
103 desktop.add(frame, new Integer(1)); |
|
104 |
|
105 container.add(desktop); |
|
106 container = frame; |
|
107 } |
|
108 this.table = new JTable(new DefaultTableModel(COLUMNS, 2)); |
|
109 if (TERMINATE) { |
|
110 this.table.putClientProperty(KEY, Boolean.TRUE); |
|
111 } |
|
112 TableColumn column = this.table.getColumn(COLUMNS[1]); |
|
113 column.setCellEditor(new DefaultCellEditor(new JComboBox(ITEMS))); |
|
114 |
|
115 container.add(BorderLayout.NORTH, new JTextField()); |
|
116 container.add(BorderLayout.CENTER, new JScrollPane(this.table)); |
|
117 |
|
118 SwingUtilities.invokeLater(this); |
|
119 } |
|
120 else if (this.point == null) { |
|
121 this.point = this.table.getCellRect(1, 1, false).getLocation(); |
|
122 SwingUtilities.convertPointToScreen(this.point, this.table); |
|
123 SwingUtilities.invokeLater(this); |
|
124 } |
|
125 else { |
|
126 try { |
|
127 Robot robot = new Robot(); |
|
128 robot.mouseMove(this.point.x + 1, this.point.y + 1); |
|
129 robot.mousePress(InputEvent.BUTTON1_MASK); |
|
130 } |
|
131 catch (AWTException exception) { |
|
132 throw new Error("unexpected exception", exception); |
|
133 } |
|
134 } |
|
135 } |
|
136 } |