1 /*
   2  * Copyright (c) 2016, 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 6897701
  27  * @summary Verify JMenu and JMenuItem Disabled state for Nimbus LAF
  28  * @run main JMenuItemsTest
  29  */
  30 import java.awt.Color;
  31 import java.awt.Rectangle;
  32 import java.awt.image.BufferedImage;
  33 import javax.swing.JFrame;
  34 import javax.swing.JMenu;
  35 import javax.swing.JMenuBar;
  36 import javax.swing.JMenuItem;
  37 import javax.swing.SwingUtilities;
  38 import javax.swing.UIManager;
  39 
  40 public class JMenuItemsTest {
  41 
  42     private static JFrame mainFrame;
  43     private static JMenu disabledMenu;
  44     private static JMenuItem disabledMenuItem;
  45 
  46     public JMenuItemsTest() {
  47         createUI();
  48     }
  49 
  50     private void createUI() {
  51 
  52         mainFrame = new JFrame("Test");
  53 
  54         disabledMenu = new JMenu("Disabled Menu");
  55         disabledMenu.setForeground(Color.BLUE);
  56         disabledMenu.setEnabled(false);
  57 
  58         disabledMenuItem = new JMenuItem("Disabled MenuItem");
  59         disabledMenuItem.setForeground(Color.BLUE);
  60         disabledMenuItem.setEnabled(false);
  61 
  62         JMenuBar menuBar = new JMenuBar();
  63         menuBar = new JMenuBar();
  64         menuBar.add(disabledMenu);
  65         menuBar.add(disabledMenuItem);
  66 
  67         mainFrame.add(menuBar);
  68         mainFrame.pack();
  69         mainFrame.setVisible(true);
  70     }
  71 
  72     private void dispose() {
  73         mainFrame.dispose();
  74     }
  75 
  76     private void testDisabledStateOfJMenu() {
  77 
  78         // Test disabled JMenu state
  79         Rectangle rect = disabledMenu.getBounds();
  80         BufferedImage image = new BufferedImage(rect.width, rect.height,
  81                 BufferedImage.TYPE_INT_ARGB);
  82         disabledMenu.paint(image.getGraphics());
  83         int y = image.getHeight() / 2;
  84         for (int x = 0; x < image.getWidth(); x++) {
  85             Color c = new Color(image.getRGB(x, y));
  86             if (c.equals(Color.BLUE)) {
  87                 dispose();
  88                 throw new RuntimeException("JMenu Disabled"
  89                         + " State not Valid.");
  90             }
  91         }
  92 
  93     }
  94 
  95     private void testDisabledStateOfJMenuItem() {
  96 
  97         // Test disabled JMenuItem state
  98         Rectangle rect = disabledMenuItem.getBounds();
  99         BufferedImage image = new BufferedImage(rect.width, rect.height,
 100                 BufferedImage.TYPE_INT_ARGB);
 101         disabledMenuItem.paint(image.getGraphics());
 102         int y = image.getHeight() / 2;
 103         for (int x = 0; x < image.getWidth(); x++) {
 104             Color c = new Color(image.getRGB(x, y));
 105             if (c.equals(Color.BLUE)) {
 106                 dispose();
 107                 throw new RuntimeException("JMenuItem Disabled"
 108                         + " State not Valid.");
 109             }
 110         }
 111 
 112     }
 113 
 114     public static void main(String[] args) throws Exception {
 115         UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
 116         SwingUtilities.invokeAndWait(() -> {
 117 
 118             try {
 119                 JMenuItemsTest obj = new JMenuItemsTest();
 120                 obj.testDisabledStateOfJMenu();
 121                 obj.testDisabledStateOfJMenuItem();
 122                 obj.dispose();
 123 
 124             } catch (Exception ex) {
 125                 throw ex;
 126             }
 127 
 128         });
 129     }
 130 }