|
| 1 | +/* |
| 2 | + * Copyright (c) 2005, 2025, 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.BorderLayout; |
| 25 | +import java.awt.Color; |
| 26 | +import java.awt.Component; |
| 27 | +import java.awt.ComponentOrientation; |
| 28 | +import java.awt.Graphics; |
| 29 | +import java.awt.event.KeyEvent; |
| 30 | +import javax.swing.Icon; |
| 31 | +import javax.swing.JCheckBoxMenuItem; |
| 32 | +import javax.swing.JFrame; |
| 33 | +import javax.swing.JLabel; |
| 34 | +import javax.swing.JMenu; |
| 35 | +import javax.swing.JMenuBar; |
| 36 | +import javax.swing.JMenuItem; |
| 37 | +import javax.swing.JRadioButtonMenuItem; |
| 38 | +import javax.swing.KeyStroke; |
| 39 | +import javax.swing.UIManager; |
| 40 | + |
| 41 | +final class MenuItemTestHelper { |
| 42 | + |
| 43 | + public static JFrame getMenuItemTestFrame(boolean isLeft, String lafName) { |
| 44 | + boolean applyLookAndFeel = lafName != null; |
| 45 | + if (applyLookAndFeel) { |
| 46 | + try { |
| 47 | + UIManager.setLookAndFeel(lafName); |
| 48 | + } catch (Exception e) { |
| 49 | + throw new RuntimeException(e); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + Icon myIcon = new ColoredIcon(Color.RED, 10, 10); |
| 54 | + Icon myIcon2 = new ColoredIcon(Color.GREEN, 15, 10); |
| 55 | + |
| 56 | + JMenuBar menuBar = new JMenuBar(); |
| 57 | + menuBar.add(createViewMenu(myIcon, myIcon2)); |
| 58 | + menuBar.add(createNoNothingMenu()); |
| 59 | + menuBar.add(createSomeIconsMenu(myIcon, myIcon2)); |
| 60 | + |
| 61 | + String title = (isLeft ? "(Left-to-right)" : "(Right-to-left)") + " - Menu Item Test"; |
| 62 | + JFrame frame = new JFrame(title); |
| 63 | + frame.setJMenuBar(menuBar); |
| 64 | + frame.applyComponentOrientation(isLeft |
| 65 | + ? ComponentOrientation.LEFT_TO_RIGHT |
| 66 | + : ComponentOrientation.RIGHT_TO_LEFT); |
| 67 | + |
| 68 | + if (applyLookAndFeel) { |
| 69 | + String shortName = lafName.substring(lafName.lastIndexOf('.') + 1); |
| 70 | + JLabel label = new JLabel("<HTML><H2>" + shortName + "</H2></HTML>"); |
| 71 | + frame.setLayout(new BorderLayout()); |
| 72 | + frame.add(label, BorderLayout.CENTER); |
| 73 | + } |
| 74 | + |
| 75 | + frame.setSize(300, 300); |
| 76 | + return frame; |
| 77 | + } |
| 78 | + |
| 79 | + public static JFrame getMenuItemTestFrame(boolean isLeft) { |
| 80 | + return getMenuItemTestFrame(isLeft, null); |
| 81 | + } |
| 82 | + |
| 83 | + private static JMenu createViewMenu(Icon myIcon, Icon myIcon2) { |
| 84 | + JMenu menu = new JMenu("View"); |
| 85 | + menu.setMnemonic('V'); |
| 86 | + menu.add(new JMenuItem("Refresh")); |
| 87 | + menu.add(new JMenuItem("Customize...")); |
| 88 | + menu.add(new JCheckBoxMenuItem("Show Toolbar")); |
| 89 | + menu.addSeparator(); |
| 90 | + menu.add(new JRadioButtonMenuItem("List")); |
| 91 | + menu.add(new JRadioButtonMenuItem("Icons")); |
| 92 | + |
| 93 | + JRadioButtonMenuItem rm2 = new JRadioButtonMenuItem("And icon."); |
| 94 | + rm2.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F1, KeyEvent.SHIFT_DOWN_MASK)); |
| 95 | + rm2.setIcon(myIcon2); |
| 96 | + menu.add(rm2); |
| 97 | + |
| 98 | + JRadioButtonMenuItem mi3 = new JRadioButtonMenuItem("Radio w/icon"); |
| 99 | + mi3.setIcon(myIcon); |
| 100 | + menu.add(mi3); |
| 101 | + |
| 102 | + menu.add(new JMenuItem(myIcon2)); |
| 103 | + |
| 104 | + JMenuItem mi4 = new JMenuItem("Item with icon"); |
| 105 | + mi4.setIcon(myIcon); |
| 106 | + menu.addSeparator(); |
| 107 | + menu.add(mi4); |
| 108 | + |
| 109 | + return menu; |
| 110 | + } |
| 111 | + |
| 112 | + private static JMenu createNoNothingMenu() { |
| 113 | + final JMenu noMenu = new JMenu("No nothing"); |
| 114 | + |
| 115 | + for (String label : new String[]{"One", "Two", "Threeee"}) { |
| 116 | + JMenuItem item = new JMenuItem(label); |
| 117 | + item.addActionListener((e) -> |
| 118 | + PassFailJFrame.log("menu.width = " |
| 119 | + + noMenu.getPopupMenu().getWidth())); |
| 120 | + noMenu.add(item); |
| 121 | + } |
| 122 | + |
| 123 | + return noMenu; |
| 124 | + } |
| 125 | + |
| 126 | + private static JMenu createSomeIconsMenu(Icon myIcon, Icon myIcon2) { |
| 127 | + JMenu someIcons = new JMenu("Some icons"); |
| 128 | + |
| 129 | + JMenuItem imi1 = new JMenuItem("Icon!"); |
| 130 | + imi1.setIcon(myIcon); |
| 131 | + someIcons.add(imi1); |
| 132 | + |
| 133 | + JMenuItem imi2 = new JMenuItem("Wide icon!"); |
| 134 | + imi2.setIcon(myIcon2); |
| 135 | + someIcons.add(imi2); |
| 136 | + |
| 137 | + someIcons.add(new JCheckBoxMenuItem("CheckBox")); |
| 138 | + someIcons.add(new JRadioButtonMenuItem("RadioButton")); |
| 139 | + |
| 140 | + return someIcons; |
| 141 | + } |
| 142 | + |
| 143 | + private record ColoredIcon(Color color, int width, int height) |
| 144 | + implements Icon { |
| 145 | + @Override |
| 146 | + public void paintIcon(Component c, Graphics g, int x, int y) { |
| 147 | + Color oldColor = g.getColor(); |
| 148 | + g.setColor(color); |
| 149 | + g.fillRect(x, y, width, height); |
| 150 | + g.setColor(oldColor); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public int getIconWidth() { |
| 155 | + return width; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public int getIconHeight() { |
| 160 | + return height; |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments