1 /*
   2  * Copyright (c) 2012, 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  * @key headful
  27  * @bug 6209975
  28  * @summary regression: JMenuItem icons overimposed on JMenuItem labels under Metal LAF
  29  * @author Alexander Zuev
  30  * @run main bug6209975
  31  */
  32 import javax.swing.*;
  33 import java.awt.*;
  34 import java.awt.event.InputEvent;
  35 
  36 public class bug6209975 {
  37 
  38     private static final ReturnObject RO1 = new ReturnObject();
  39     private static final ReturnObject RO2 = new ReturnObject();
  40 
  41     private static JMenu menu;
  42     private static JButton button;
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         Robot robot = new Robot();
  47         robot.setAutoDelay(500);
  48 
  49 
  50         SwingUtilities.invokeAndWait(new Runnable() {
  51 
  52             @Override
  53             public void run() {
  54                 createAndShowGUI();
  55             }
  56         });
  57 
  58         robot.waitForIdle();
  59 
  60         Point clickPoint = getButtonClickPoint();
  61         robot.mouseMove(clickPoint.x, clickPoint.y);
  62         robot.mousePress(InputEvent.BUTTON1_MASK);
  63         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  64         robot.waitForIdle();
  65 
  66         clickPoint = getMenuClickPoint();
  67         robot.mouseMove(clickPoint.x, clickPoint.y);
  68         robot.mousePress(InputEvent.BUTTON1_MASK);
  69         robot.mouseRelease(InputEvent.BUTTON1_MASK);
  70         robot.waitForIdle();
  71 
  72         if (RO1.itsValue <= RO2.itsValue) {
  73             throw new RuntimeException("Offset if the second icon is invalid.");
  74         }
  75     }
  76 
  77     private static Point getButtonClickPoint() throws Exception {
  78         final Point[] result = new Point[1];
  79 
  80         SwingUtilities.invokeAndWait(new Runnable() {
  81 
  82             @Override
  83             public void run() {
  84                 Point p = button.getLocationOnScreen();
  85                 Dimension size = button.getSize();
  86                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
  87             }
  88         });
  89         return result[0];
  90     }
  91 
  92     private static Point getMenuClickPoint() throws Exception {
  93         final Point[] result = new Point[1];
  94 
  95         SwingUtilities.invokeAndWait(new Runnable() {
  96 
  97             @Override
  98             public void run() {
  99                 Point p = menu.getLocationOnScreen();
 100                 Dimension size = menu.getSize();
 101                 result[0] = new Point(p.x + size.width / 2, p.y + size.height / 2);
 102             }
 103         });
 104         return result[0];
 105     }
 106 
 107     private static void createAndShowGUI() {
 108         JFrame frame = new JFrame("Test6209975");
 109         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 110         frame.applyComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
 111         frame.setLayout(new BorderLayout());
 112         button = new JButton("Focus holder");
 113         frame.add(button);
 114 
 115         JMenuBar mb = new JMenuBar();
 116         menu = new JMenu("File");
 117 
 118         JMenuItem item;
 119 
 120         item = new JMenuItem("Just a menu item");
 121         item.setIcon(new MyIcon(RO1));
 122         item.setHorizontalTextPosition(SwingConstants.LEADING);
 123         menu.add(item);
 124 
 125         item = new JMenuItem("Menu Item with another icon");
 126         item.setIcon(new MyIcon(RO2));
 127         item.setHorizontalTextPosition(SwingConstants.TRAILING);
 128         menu.add(item);
 129 
 130         mb.add(menu);
 131 
 132         frame.setJMenuBar(mb);
 133         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 134         frame.pack();
 135         frame.setLocation(400, 300);
 136         frame.setVisible(true);
 137     }
 138 
 139     public static class ReturnObject {
 140 
 141         public volatile int itsValue;
 142     }
 143 
 144     public static class MyIcon implements Icon {
 145 
 146         ReturnObject thisObject = null;
 147 
 148         public MyIcon(ReturnObject ro) {
 149             super();
 150             thisObject = ro;
 151         }
 152 
 153         public void paintIcon(Component c, Graphics g, int x, int y) {
 154             Color color = g.getColor();
 155             g.setColor(Color.BLACK);
 156             g.fillRect(x, y, 10, 10);
 157             g.setColor(color);
 158             thisObject.itsValue = x;
 159         }
 160 
 161         public int getIconWidth() {
 162             return 10;
 163         }
 164 
 165         public int getIconHeight() {
 166             return 10;
 167         }
 168     }
 169 }