|
| 1 | +/* |
| 2 | + * Copyright (c) 2015, 2022, 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.Component; |
| 25 | +import java.awt.Dimension; |
| 26 | +import java.awt.HeadlessException; |
| 27 | +import java.awt.Point; |
| 28 | +import java.awt.Robot; |
| 29 | +import java.awt.event.ActionListener; |
| 30 | +import java.awt.event.InputEvent; |
| 31 | +import java.awt.event.KeyEvent; |
| 32 | +import java.util.Arrays; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | +import java.util.stream.Collectors; |
| 37 | +import javax.swing.JButton; |
| 38 | +import javax.swing.JDialog; |
| 39 | +import javax.swing.JFileChooser; |
| 40 | +import javax.swing.JFrame; |
| 41 | +import javax.swing.JPanel; |
| 42 | +import javax.swing.SwingUtilities; |
| 43 | +import javax.swing.UIManager; |
| 44 | +import javax.swing.UIManager.LookAndFeelInfo; |
| 45 | +import javax.swing.UnsupportedLookAndFeelException; |
| 46 | + |
| 47 | +import static javax.swing.UIManager.getInstalledLookAndFeels; |
| 48 | + |
| 49 | +/* |
| 50 | + * @test |
| 51 | + * @key headful |
| 52 | + * @bug 4390885 |
| 53 | + * @summary This test checks CCC #4390885, which verifies that it should be |
| 54 | + * possible to set the location of the JFileChooser. |
| 55 | + * @run main JFileChooserSetLocationTest |
| 56 | + */ |
| 57 | +public class JFileChooserSetLocationTest { |
| 58 | + |
| 59 | + public static final String SHOW_DIALOG_OUTSIDE_THE_PANEL = |
| 60 | + "ShowFileChooser OUTSIDE the Panel"; |
| 61 | + public static final String SHOW_DIALOG_OVER_THE_PANEL = |
| 62 | + "ShowFileChooser OVER the Panel"; |
| 63 | + public static final String SHOW_SAVE_DIALOG_OVER_THE_PANEL = |
| 64 | + "ShowSaveDialog"; |
| 65 | + private static final int TOLERANCE_LEVEL = 6; |
| 66 | + private static final int xOut = 75; |
| 67 | + private static final int yOut = 75; |
| 68 | + private static Robot robot; |
| 69 | + private static JPanel panel; |
| 70 | + private static MyFileChooser fileChooser; |
| 71 | + private static int xIn; |
| 72 | + private static int yIn; |
| 73 | + private static JButton btn; |
| 74 | + private static JButton btn1; |
| 75 | + private static JButton btn2; |
| 76 | + private static JFrame frame; |
| 77 | + |
| 78 | + public static void main(String[] s) throws Exception { |
| 79 | + robot = new Robot(); |
| 80 | + robot.setAutoWaitForIdle(true); |
| 81 | + robot.setAutoDelay(200); |
| 82 | + |
| 83 | + List<String> lafs = Arrays.stream(getInstalledLookAndFeels()) |
| 84 | + .map(LookAndFeelInfo::getClassName) |
| 85 | + .collect(Collectors.toList()); |
| 86 | + for (final String laf : lafs) { |
| 87 | + try { |
| 88 | + AtomicBoolean lafSetSuccess = new AtomicBoolean(false); |
| 89 | + SwingUtilities.invokeAndWait(() -> { |
| 90 | + lafSetSuccess.set(setLookAndFeel(laf)); |
| 91 | + if (lafSetSuccess.get()) { |
| 92 | + createUI(); |
| 93 | + } |
| 94 | + }); |
| 95 | + if (!lafSetSuccess.get()) { |
| 96 | + continue; |
| 97 | + } |
| 98 | + robot.waitForIdle(); |
| 99 | + |
| 100 | + AtomicReference<Point> pt = new AtomicReference<>(); |
| 101 | + AtomicReference<Dimension> dim = new AtomicReference<>(); |
| 102 | + SwingUtilities.invokeAndWait(() -> { |
| 103 | + pt.set(panel.getLocationOnScreen()); |
| 104 | + dim.set(panel.getSize()); |
| 105 | + }); |
| 106 | + Point panelLoc = pt.get(); |
| 107 | + Dimension panelDim = dim.get(); |
| 108 | + xIn = (panelLoc.x + panelDim.width) / 2; |
| 109 | + yIn = (panelLoc.y + panelDim.height) / 2; |
| 110 | + |
| 111 | + Point dest = getCenterPointOf(btn); |
| 112 | + |
| 113 | + robot.mouseMove(dest.x, dest.y); |
| 114 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 115 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 116 | + |
| 117 | + robot.waitForIdle(); |
| 118 | + |
| 119 | + Point actualPos = getActualLocation(fileChooser); |
| 120 | + |
| 121 | + // Case 1 : Verifying that the location of JFileChooser |
| 122 | + // 'Show Dialog' is correctly set outside the frame at (25,25) |
| 123 | + verify(xOut, actualPos.x, yOut, actualPos.y); |
| 124 | + |
| 125 | + hitKeys(KeyEvent.VK_ESCAPE); |
| 126 | + |
| 127 | + dest = getCenterPointOf(btn1); |
| 128 | + robot.mouseMove(dest.x, dest.y); |
| 129 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 130 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 131 | + |
| 132 | + actualPos = getActualLocation(fileChooser); |
| 133 | + |
| 134 | + // Case 2 : Verifying that the location of JFileChooser |
| 135 | + // 'Show Dialog' is correctly set inside the test frame |
| 136 | + verify(xIn, actualPos.x, yIn, actualPos.y); |
| 137 | + |
| 138 | + hitKeys(KeyEvent.VK_ESCAPE); |
| 139 | + |
| 140 | + dest = getCenterPointOf(btn2); |
| 141 | + robot.mouseMove(dest.x, dest.y); |
| 142 | + robot.mousePress(InputEvent.BUTTON1_DOWN_MASK); |
| 143 | + robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK); |
| 144 | + |
| 145 | + actualPos = getActualLocation(fileChooser); |
| 146 | + |
| 147 | + // Case 3 : Verifying that the location of JFileChooser |
| 148 | + // 'Save Dialog' is correctly set inside the test frame |
| 149 | + verify(xIn, actualPos.x, yIn, actualPos.y); |
| 150 | + |
| 151 | + hitKeys(KeyEvent.VK_ESCAPE); |
| 152 | + |
| 153 | + System.out.println("Test Passed, All cases passed for " + laf); |
| 154 | + } finally { |
| 155 | + SwingUtilities.invokeAndWait( |
| 156 | + JFileChooserSetLocationTest::disposeFrame); |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private static Point getCenterPointOf(final Component comp) |
| 162 | + throws Exception { |
| 163 | + |
| 164 | + AtomicReference<Point> pt = new AtomicReference<>(); |
| 165 | + SwingUtilities.invokeAndWait(() -> pt.set(comp.getLocationOnScreen())); |
| 166 | + Point loc = pt.get(); |
| 167 | + loc.translate(comp.getWidth() / 2, comp.getHeight() / 2); |
| 168 | + return loc; |
| 169 | + } |
| 170 | + |
| 171 | + private static Point getActualLocation(final MyFileChooser fcoo) |
| 172 | + throws Exception { |
| 173 | + AtomicReference<Point> pt = new AtomicReference<>(); |
| 174 | + SwingUtilities.invokeAndWait(() -> pt.set(fcoo.getDialogLocation())); |
| 175 | + return pt.get(); |
| 176 | + } |
| 177 | + |
| 178 | + public static void verify(int x1, int x2, int y1, int y2) { |
| 179 | + System.out.println("verify " + x1 + "==" + x2 + "; " + y1 + "==" + y2); |
| 180 | + if ((Math.abs(x1 - x2) < TOLERANCE_LEVEL) && |
| 181 | + (Math.abs(y1 - y2) < TOLERANCE_LEVEL)) { |
| 182 | + System.out.println("Test passed"); |
| 183 | + } else { |
| 184 | + throw new RuntimeException( |
| 185 | + "Test Failed, setLocation() is not working properly"); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + private static void hitKeys(int... keys) { |
| 190 | + for (int key : keys) { |
| 191 | + robot.keyPress(key); |
| 192 | + } |
| 193 | + |
| 194 | + for (int i = keys.length - 1; i >= 0; i--) { |
| 195 | + robot.keyRelease(keys[i]); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + public static void createUI() { |
| 200 | + frame = new JFrame(); |
| 201 | + panel = new JPanel(); |
| 202 | + btn = new JButton(SHOW_DIALOG_OUTSIDE_THE_PANEL); |
| 203 | + btn1 = new JButton(SHOW_DIALOG_OVER_THE_PANEL); |
| 204 | + btn2 = new JButton(SHOW_SAVE_DIALOG_OVER_THE_PANEL); |
| 205 | + ActionListener actionListener = actionEvent -> { |
| 206 | + String btnAction = actionEvent.getActionCommand(); |
| 207 | + if (btnAction.equals(SHOW_DIALOG_OUTSIDE_THE_PANEL)) { |
| 208 | + fileChooser = new MyFileChooser(xOut, yOut); |
| 209 | + fileChooser.showOpenDialog(panel); |
| 210 | + } else if (btnAction.equals(SHOW_DIALOG_OVER_THE_PANEL)) { |
| 211 | + fileChooser = new MyFileChooser(xIn, yIn); |
| 212 | + fileChooser.showOpenDialog(panel); |
| 213 | + } else if (btnAction.equals(SHOW_SAVE_DIALOG_OVER_THE_PANEL)) { |
| 214 | + fileChooser = new MyFileChooser(xIn, yIn); |
| 215 | + fileChooser.showSaveDialog(panel); |
| 216 | + } |
| 217 | + }; |
| 218 | + btn.addActionListener(actionListener); |
| 219 | + btn1.addActionListener(actionListener); |
| 220 | + btn2.addActionListener(actionListener); |
| 221 | + panel.add(btn); |
| 222 | + panel.add(btn1); |
| 223 | + panel.add(btn2); |
| 224 | + |
| 225 | + frame.add(panel); |
| 226 | + frame.setLocationRelativeTo(null); |
| 227 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 228 | + frame.pack(); |
| 229 | + frame.setVisible(true); |
| 230 | + } |
| 231 | + |
| 232 | + private static boolean setLookAndFeel(String lafName) { |
| 233 | + try { |
| 234 | + System.out.println("Testing " + lafName); |
| 235 | + UIManager.setLookAndFeel(lafName); |
| 236 | + } catch (UnsupportedLookAndFeelException ignored) { |
| 237 | + System.out.println("Ignoring Unsupported laf : " + lafName); |
| 238 | + return false; |
| 239 | + } catch (ClassNotFoundException | InstantiationException |
| 240 | + | IllegalAccessException e) { |
| 241 | + throw new RuntimeException(e); |
| 242 | + } |
| 243 | + return true; |
| 244 | + } |
| 245 | + |
| 246 | + private static void disposeFrame() { |
| 247 | + if (frame != null) { |
| 248 | + frame.dispose(); |
| 249 | + frame = null; |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + private static class MyFileChooser extends JFileChooser { |
| 254 | + JDialog dialog; |
| 255 | + int x, y; |
| 256 | + |
| 257 | + public MyFileChooser(int x, int y) { |
| 258 | + super(); |
| 259 | + this.x = x; |
| 260 | + this.y = y; |
| 261 | + } |
| 262 | + |
| 263 | + protected JDialog createDialog(Component parent) |
| 264 | + throws HeadlessException { |
| 265 | + |
| 266 | + dialog = super.createDialog(parent); |
| 267 | + |
| 268 | + System.out.println( |
| 269 | + "createDialog and set location to (" + x + ", " + y + ")"); |
| 270 | + dialog.setLocation(x, y); |
| 271 | + |
| 272 | + return dialog; |
| 273 | + } |
| 274 | + |
| 275 | + public Point getDialogLocation() { |
| 276 | + return dialog.getLocation(); |
| 277 | + } |
| 278 | + |
| 279 | + } |
| 280 | + |
| 281 | +} |
0 commit comments