1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
|
/*
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import static javax.swing.UIManager.getInstalledLookAndFeels;
/*
* @test
* @key headful
* @bug 4390885
* @summary This test checks CCC #4390885, which verifies that it should be
* possible to set the location of the JFileChooser.
* @run main JFileChooserSetLocationTest
*/
public class JFileChooserSetLocationTest {
public static final String SHOW_DIALOG_OUTSIDE_THE_PANEL =
"ShowFileChooser OUTSIDE the Panel";
public static final String SHOW_DIALOG_OVER_THE_PANEL =
"ShowFileChooser OVER the Panel";
public static final String SHOW_SAVE_DIALOG_OVER_THE_PANEL =
"ShowSaveDialog";
private static final int TOLERANCE_LEVEL = 6;
private static final int xOut = 75;
private static final int yOut = 75;
private static Robot robot;
private static JPanel panel;
private static MyFileChooser fileChooser;
private static int xIn;
private static int yIn;
private static JButton btn;
private static JButton btn1;
private static JButton btn2;
private static JFrame frame;
public static void main(String[] s) throws Exception {
robot = new Robot();
robot.setAutoWaitForIdle(true);
robot.setAutoDelay(200);
List<String> lafs = Arrays.stream(getInstalledLookAndFeels())
.map(LookAndFeelInfo::getClassName)
.collect(Collectors.toList());
for (final String laf : lafs) {
try {
AtomicBoolean lafSetSuccess = new AtomicBoolean(false);
SwingUtilities.invokeAndWait(() -> {
lafSetSuccess.set(setLookAndFeel(laf));
if (lafSetSuccess.get()) {
createUI();
}
});
if (!lafSetSuccess.get()) {
continue;
}
robot.waitForIdle();
AtomicReference<Point> pt = new AtomicReference<>();
AtomicReference<Dimension> dim = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> {
pt.set(panel.getLocationOnScreen());
dim.set(panel.getSize());
});
Point panelLoc = pt.get();
Dimension panelDim = dim.get();
xIn = (panelLoc.x + panelDim.width) / 2;
yIn = (panelLoc.y + panelDim.height) / 2;
Point dest = getCenterPointOf(btn);
robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
robot.waitForIdle();
Point actualPos = getActualLocation(fileChooser);
// Case 1 : Verifying that the location of JFileChooser
// 'Show Dialog' is correctly set outside the frame at (25,25)
verify(xOut, actualPos.x, yOut, actualPos.y);
hitKeys(KeyEvent.VK_ESCAPE);
dest = getCenterPointOf(btn1);
robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
actualPos = getActualLocation(fileChooser);
// Case 2 : Verifying that the location of JFileChooser
// 'Show Dialog' is correctly set inside the test frame
verify(xIn, actualPos.x, yIn, actualPos.y);
hitKeys(KeyEvent.VK_ESCAPE);
dest = getCenterPointOf(btn2);
robot.mouseMove(dest.x, dest.y);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
actualPos = getActualLocation(fileChooser);
// Case 3 : Verifying that the location of JFileChooser
// 'Save Dialog' is correctly set inside the test frame
verify(xIn, actualPos.x, yIn, actualPos.y);
hitKeys(KeyEvent.VK_ESCAPE);
System.out.println("Test Passed, All cases passed for " + laf);
} finally {
SwingUtilities.invokeAndWait(
JFileChooserSetLocationTest::disposeFrame);
}
}
}
private static Point getCenterPointOf(final Component comp)
throws Exception {
AtomicReference<Point> pt = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> pt.set(comp.getLocationOnScreen()));
Point loc = pt.get();
loc.translate(comp.getWidth() / 2, comp.getHeight() / 2);
return loc;
}
private static Point getActualLocation(final MyFileChooser fcoo)
throws Exception {
AtomicReference<Point> pt = new AtomicReference<>();
SwingUtilities.invokeAndWait(() -> pt.set(fcoo.getDialogLocation()));
return pt.get();
}
public static void verify(int x1, int x2, int y1, int y2) throws Exception {
System.out.println("verify " + x1 + "==" + x2 + "; " + y1 + "==" + y2);
if ((Math.abs(x1 - x2) < TOLERANCE_LEVEL) &&
(Math.abs(y1 - y2) < TOLERANCE_LEVEL)) {
System.out.println("Test passed");
} else {
GraphicsConfiguration gc = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
Rectangle gcBounds = gc.getBounds();
BufferedImage bufferedImage = robot.createScreenCapture(
new Rectangle(gcBounds));
ImageIO.write(bufferedImage, "png",new File("FailureImage.png"));
throw new RuntimeException(
"Test Failed, setLocation() is not working properly");
}
}
private static void hitKeys(int... keys) {
for (int key : keys) {
robot.keyPress(key);
}
for (int i = keys.length - 1; i >= 0; i--) {
robot.keyRelease(keys[i]);
}
}
public static void createUI() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int xPos = (int) screenSize.getWidth() / 2;
int yPos = (int) screenSize.getHeight() / 2;
frame = new JFrame("FileChooser set location test");
panel = new JPanel();
btn = new JButton(SHOW_DIALOG_OUTSIDE_THE_PANEL);
btn1 = new JButton(SHOW_DIALOG_OVER_THE_PANEL);
btn2 = new JButton(SHOW_SAVE_DIALOG_OVER_THE_PANEL);
ActionListener actionListener = actionEvent -> {
String btnAction = actionEvent.getActionCommand();
if (btnAction.equals(SHOW_DIALOG_OUTSIDE_THE_PANEL)) {
fileChooser = new MyFileChooser(xOut, yOut);
fileChooser.showOpenDialog(panel);
} else if (btnAction.equals(SHOW_DIALOG_OVER_THE_PANEL)) {
fileChooser = new MyFileChooser(xIn, yIn);
fileChooser.showOpenDialog(panel);
} else if (btnAction.equals(SHOW_SAVE_DIALOG_OVER_THE_PANEL)) {
fileChooser = new MyFileChooser(xIn, yIn);
fileChooser.showSaveDialog(panel);
}
};
btn.addActionListener(actionListener);
btn1.addActionListener(actionListener);
btn2.addActionListener(actionListener);
panel.add(btn);
panel.add(btn1);
panel.add(btn2);
frame.add(panel);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.pack();
frame.setLocation(xPos, yPos - 200);
frame.setVisible(true);
}
private static boolean setLookAndFeel(String lafName) {
try {
System.out.println("Testing " + lafName);
UIManager.setLookAndFeel(lafName);
} catch (UnsupportedLookAndFeelException ignored) {
System.out.println("Ignoring Unsupported laf : " + lafName);
return false;
} catch (ClassNotFoundException | InstantiationException
| IllegalAccessException e) {
throw new RuntimeException(e);
}
return true;
}
private static void disposeFrame() {
if (frame != null) {
frame.dispose();
frame = null;
}
}
private static class MyFileChooser extends JFileChooser {
JDialog dialog;
int x, y;
public MyFileChooser(int x, int y) {
super();
this.x = x;
this.y = y;
}
protected JDialog createDialog(Component parent)
throws HeadlessException {
dialog = super.createDialog(parent);
System.out.println(
"createDialog and set location to (" + x + ", " + y + ")");
dialog.setLocation(x, y);
return dialog;
}
public Point getDialogLocation() {
return dialog.getLocation();
}
}
}
|