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