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