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
|
/*
* Copyright (c) 2016, 2017, 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.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
/**
* @test
* @key headful
* @bug 8158566 8160879 8160977 8158566
* @summary Provide a Swing property which modifies MenuItemUI behaviour
*/
public class CloseOnMouseClickPropertyTest {
private static final String CHECK_BOX_PROP = "CheckBoxMenuItem."
+ "doNotCloseOnMouseClick";
private static final String RADIO_BUTTON_PROP = "RadioButtonMenuItem"
+ ".doNotCloseOnMouseClick";
private static JFrame frame;
private static JMenu menu;
private static TestItem[] TEST_ITEMS = {
new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, true),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, false),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, true),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, false),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, null, true),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, null, false),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, true, null),
new TestItem(TestType.CHECK_BOX_MENU_ITEM, false, null),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, true),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, false),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, true),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, false),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, true, null),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, false, null),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, null, true),
new TestItem(TestType.RADIO_BUTTON_MENU_ITEM, null, false),
new TestItem(TestType.MENU_ITEM, true, true),
new TestItem(TestType.MENU_ITEM, true, false),
new TestItem(TestType.MENU_ITEM, false, true),
new TestItem(TestType.MENU_ITEM, false, false),
new TestItem(TestType.MENU_ITEM, true, null),
new TestItem(TestType.MENU_ITEM, false, null),
new TestItem(TestType.MENU_ITEM, null, true),
new TestItem(TestType.MENU_ITEM, null, false),
};
public static void main(String[] args) throws Exception {
for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
UIManager.setLookAndFeel(info.getClassName());
for (TestItem testItem : TEST_ITEMS) {
test(testItem);
}
}
}
private static void test(TestItem item) throws Exception {
Robot robot = new Robot();
robot.setAutoDelay(50);
SwingUtilities.invokeAndWait(() -> createAndShowGUI(item));
robot.waitForIdle();
Point point = getClickPoint(true);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
point = getClickPoint(false);
robot.mouseMove(point.x, point.y);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
robot.waitForIdle();
SwingUtilities.invokeAndWait(() -> {
JMenuItem menuItem = menu.getItem(0);
boolean isShowing = menuItem.isShowing();
frame.dispose();
if (isShowing ^ item.doNotCloseOnMouseClick()) {
throw new RuntimeException("Property is not taken into account!");
}
});
}
private static void createAndShowGUI(TestItem testItem) {
frame = new JFrame();
frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menuBar = new JMenuBar();
menu = new JMenu("Menu");
JMenuItem menuItem = testItem.getMenuItem();
testItem.setProperties(menuItem);
menu.add(menuItem);
menuBar.add(menu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
}
private static Point getClickPoint(boolean parent) throws Exception {
Point points[] = new Point[1];
SwingUtilities.invokeAndWait(() -> {
JComponent comp = parent ? menu : menu.getItem(0);
Point point = comp.getLocationOnScreen();
Rectangle bounds = comp.getBounds();
point.x += bounds.getWidth() / 2;
point.y += bounds.getHeight() / 2;
points[0] = point;
});
return points[0];
}
enum TestType {
MENU_ITEM,
CHECK_BOX_MENU_ITEM,
RADIO_BUTTON_MENU_ITEM
}
static class TestItem {
TestType type;
Boolean compDoNotCloseOnMouseClick;
Boolean lafDoNotCloseOnMouseClick;
public TestItem(TestType type,
Boolean compDoNotCloseOnMouseClick,
Boolean lafDoNotCloseOnMouseClick)
{
this.type = type;
this.compDoNotCloseOnMouseClick = compDoNotCloseOnMouseClick;
this.lafDoNotCloseOnMouseClick = lafDoNotCloseOnMouseClick;
}
boolean doNotCloseOnMouseClick() {
switch (type) {
case MENU_ITEM:
return false;
default:
return compDoNotCloseOnMouseClick != null
? compDoNotCloseOnMouseClick
: lafDoNotCloseOnMouseClick;
}
}
void setProperties(JMenuItem menuItem) {
switch (type) {
case CHECK_BOX_MENU_ITEM:
menuItem.putClientProperty(CHECK_BOX_PROP, compDoNotCloseOnMouseClick);
UIManager.put(CHECK_BOX_PROP, lafDoNotCloseOnMouseClick);
break;
case RADIO_BUTTON_MENU_ITEM:
menuItem.putClientProperty(RADIO_BUTTON_PROP, compDoNotCloseOnMouseClick);
UIManager.put(RADIO_BUTTON_PROP, lafDoNotCloseOnMouseClick);
break;
default:
menuItem.putClientProperty(CHECK_BOX_PROP, compDoNotCloseOnMouseClick);
menuItem.putClientProperty(RADIO_BUTTON_PROP, compDoNotCloseOnMouseClick);
UIManager.put(CHECK_BOX_PROP, lafDoNotCloseOnMouseClick);
UIManager.put(RADIO_BUTTON_PROP, lafDoNotCloseOnMouseClick);
}
}
JMenuItem getMenuItem() {
switch (type) {
case CHECK_BOX_MENU_ITEM:
return new JCheckBoxMenuItem("Check Box");
case RADIO_BUTTON_MENU_ITEM:
return new JRadioButtonMenuItem("Radio Button");
default:
return new JMenuItem("Menu Item");
}
}
}
}
|