1 /*
   2  * Copyright (c) 2007, 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 /* @test
  25    @bug 4783068
  26    @summary Disabled components should render grayed-out HTML
  27    @author Peter Zhelezniakov
  28    @run main Test4783068
  29 */
  30 
  31 import java.awt.*;
  32 import java.awt.image.BufferedImage;
  33 import javax.swing.*;
  34 import javax.swing.plaf.metal.MetalLookAndFeel;
  35 
  36 public class Test4783068 {
  37     final static Color TEST_COLOR = Color.WHITE;
  38 
  39     final static String html = "<html>" +
  40                   "This is a <font color='red'>colored</font> <b>text</b>" +
  41                   "<p>with a <a href='http://ru.sun.com'>link</a>" +
  42                   "<ul><li>an unordered<li>list</ul>" +
  43                   "<ol><li>and an ordered<li>list</ol>" +
  44                   "</html>";
  45 
  46 
  47     void test() {
  48         try {
  49             UIManager.setLookAndFeel(new MetalLookAndFeel());
  50         } catch (UnsupportedLookAndFeelException e) {
  51             throw new Error("Cannot set Metal LAF");
  52         }
  53         // Render text using background color
  54         UIManager.put("textInactiveText", TEST_COLOR);
  55 
  56         test(new JLabel(html));
  57         test(new JButton(html));
  58 
  59         JEditorPane pane = new JEditorPane("text/html", html);
  60         pane.setDisabledTextColor(TEST_COLOR);
  61         test(pane);
  62     }
  63 
  64     void test(JComponent c) {
  65         c.setEnabled(false);
  66         c.setOpaque(true);
  67         c.setBackground(TEST_COLOR);
  68         c.setBorder(null);
  69         Dimension size = c.getPreferredSize();
  70         c.setBounds(0, 0, size.width, size.height);
  71 
  72         BufferedImage image = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_ARGB);
  73         c.paint(image.getGraphics());
  74 
  75         int rgb = TEST_COLOR.getRGB();
  76         for (int i = 0; i < size.height; i++) {
  77             for (int j = 0; j < size.width; j++) {
  78                 if (image.getRGB(j, i) != rgb) {
  79                     throw new RuntimeException(
  80                             String.format("Color mismatch at [%d, %d]", j, i));
  81                 }
  82             }
  83         }
  84     }
  85 
  86     public static void main(String[] args) throws Exception {
  87         SwingUtilities.invokeAndWait(new Runnable() {
  88             @Override public void run() {
  89                 new Test4783068().test();
  90             }
  91         });
  92     }
  93 }