|
| 1 | +/* |
| 2 | + * Copyright (c) 1999, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 4211052 |
| 27 | + * @requires (os.family == "windows") |
| 28 | + * @summary |
| 29 | + * This test checks if menu items lay out correctly when their |
| 30 | + * ComponentOrientation property is set to RIGHT_TO_LEFT. |
| 31 | + * The tester is asked to compare left-to-right and |
| 32 | + * right-to-left menus and judge whether they are mirror images of each |
| 33 | + * other. |
| 34 | + * @library /java/awt/regtesthelpers |
| 35 | + * @build PassFailJFrame |
| 36 | + * @run main/manual RightLeftOrientation |
| 37 | + */ |
| 38 | + |
| 39 | +import java.awt.Color; |
| 40 | +import java.awt.Component; |
| 41 | +import java.awt.ComponentOrientation; |
| 42 | +import java.awt.Font; |
| 43 | +import java.awt.Graphics; |
| 44 | +import java.awt.event.ActionEvent; |
| 45 | +import java.awt.event.KeyEvent; |
| 46 | + |
| 47 | +import javax.swing.Icon; |
| 48 | +import javax.swing.JCheckBoxMenuItem; |
| 49 | +import javax.swing.JFrame; |
| 50 | +import javax.swing.JMenu; |
| 51 | +import javax.swing.JMenuBar; |
| 52 | +import javax.swing.JMenuItem; |
| 53 | +import javax.swing.JRadioButtonMenuItem; |
| 54 | +import javax.swing.KeyStroke; |
| 55 | +import javax.swing.LookAndFeel; |
| 56 | +import javax.swing.SwingConstants; |
| 57 | +import javax.swing.UIManager; |
| 58 | + |
| 59 | +public class RightLeftOrientation { |
| 60 | + |
| 61 | + private static final String INSTRUCTIONS = """ |
| 62 | + A menu bar is shown containing a menu for each look and feel. |
| 63 | + A disabled menu means that the look and feel is not available for |
| 64 | + testing in this environment. |
| 65 | + Every effort should be made to run this test |
| 66 | + in an environment that covers all look and feels. |
| 67 | +
|
| 68 | + Each menu is divided into two halves. The upper half is oriented |
| 69 | + left-to-right and the lower half is oriented right-to-left. |
| 70 | + For each menu, ensure that the lower half mirrors the upper half. |
| 71 | +
|
| 72 | + Note that when checking the positioning of the sub-menus, it |
| 73 | + helps to position the frame away from the screen edges."""; |
| 74 | + |
| 75 | + public static void main(String[] args) throws Exception { |
| 76 | + PassFailJFrame.builder() |
| 77 | + .title("RightLeftOrientation Instructions") |
| 78 | + .instructions(INSTRUCTIONS) |
| 79 | + .columns(35) |
| 80 | + .testUI(RightLeftOrientation::createTestUI) |
| 81 | + .build() |
| 82 | + .awaitAndCheck(); |
| 83 | + } |
| 84 | + |
| 85 | + private static JFrame createTestUI() { |
| 86 | + JFrame frame = new JFrame("RightLeftOrientation"); |
| 87 | + JMenuBar menuBar = new JMenuBar(); |
| 88 | + |
| 89 | + menuBar.add(createMenu("javax.swing.plaf.metal.MetalLookAndFeel", |
| 90 | + "Metal")); |
| 91 | + menuBar.add(createMenu("com.sun.java.swing.plaf.motif.MotifLookAndFeel", |
| 92 | + "Motif")); |
| 93 | + menuBar.add(createMenu("com.sun.java.swing.plaf.windows.WindowsLookAndFeel", |
| 94 | + "Windows")); |
| 95 | + |
| 96 | + frame.setJMenuBar(menuBar); |
| 97 | + frame.pack(); |
| 98 | + return frame; |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | + static JMenu createMenu(String laf, String name) { |
| 103 | + JMenu menu = new JMenu(name); |
| 104 | + try { |
| 105 | + LookAndFeel save = UIManager.getLookAndFeel(); |
| 106 | + UIManager.setLookAndFeel(laf); |
| 107 | + addMenuItems(menu, ComponentOrientation.LEFT_TO_RIGHT); |
| 108 | + menu.addSeparator(); |
| 109 | + addMenuItems(menu, ComponentOrientation.RIGHT_TO_LEFT); |
| 110 | + UIManager.setLookAndFeel(save); |
| 111 | + } catch (Exception e) { |
| 112 | + menu = new JMenu(name); |
| 113 | + menu.setEnabled(false); |
| 114 | + } |
| 115 | + return menu; |
| 116 | + } |
| 117 | + |
| 118 | + static void addMenuItems(JMenu menu, ComponentOrientation o) { |
| 119 | + |
| 120 | + JMenuItem menuItem; |
| 121 | + |
| 122 | + menuItem = new JMenuItem("Menu Item"); |
| 123 | + menuItem.setComponentOrientation(o); |
| 124 | + menu.add(menuItem); |
| 125 | + |
| 126 | + menuItem = new JMenuItem("Text trails icon", new MyMenuItemIcon()); |
| 127 | + menuItem.setComponentOrientation(o); |
| 128 | + menu.add(menuItem); |
| 129 | + |
| 130 | + menuItem = new JMenuItem("Text leads icon", new MyMenuItemIcon()); |
| 131 | + menuItem.setComponentOrientation(o); |
| 132 | + menuItem.setHorizontalTextPosition(SwingConstants.LEADING); |
| 133 | + menu.add(menuItem); |
| 134 | + |
| 135 | + menuItem = new JRadioButtonMenuItem("Radio Button Menu Item"); |
| 136 | + menuItem.setComponentOrientation(o); |
| 137 | + menuItem.setSelected(true); |
| 138 | + menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK)); |
| 139 | + menu.add(menuItem); |
| 140 | + |
| 141 | + menuItem = new JCheckBoxMenuItem("Check box Menu Item"); |
| 142 | + menuItem.setComponentOrientation(o); |
| 143 | + menuItem.setSelected(true); |
| 144 | + menuItem.setFont( new Font("Serif",Font.PLAIN, 24) ); |
| 145 | + menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK)); |
| 146 | + menu.add(menuItem); |
| 147 | + |
| 148 | + menuItem = new JMenu("Sub Menu"); |
| 149 | + menuItem.setComponentOrientation(o); |
| 150 | + menuItem.add(new JMenuItem("Sub Menu One")).setComponentOrientation(o); |
| 151 | + menuItem.add(new JMenuItem("Sub Menu Two")).setComponentOrientation(o); |
| 152 | + menu.add(menuItem); |
| 153 | + } |
| 154 | + |
| 155 | + |
| 156 | + private static class MyMenuItemIcon implements Icon { |
| 157 | + public void paintIcon(Component c, Graphics g, int x, int y) { |
| 158 | + Color oldColor = g.getColor(); |
| 159 | + g.setColor(Color.orange); |
| 160 | + g.fill3DRect(x, y, getIconWidth(), getIconHeight(), true); |
| 161 | + g.setColor(oldColor); |
| 162 | + } |
| 163 | + public int getIconWidth() { return 15; } |
| 164 | + public int getIconHeight() { return 15; } |
| 165 | + } |
| 166 | +} |
0 commit comments