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 import java.awt.Graphics;
25 import java.awt.Graphics2D;
26 import java.awt.image.BufferedImage;
27 import javax.swing.JComponent;
28 import javax.swing.JLabel;
29 import javax.swing.SwingUtilities;
30 import javax.swing.UIDefaults;
31 import javax.swing.UIManager;
32 import javax.swing.border.TitledBorder;
33 import javax.swing.plaf.ComponentUI;
34 import javax.swing.plaf.UIResource;
35 import javax.swing.plaf.metal.MetalLabelUI;
36 import javax.swing.plaf.metal.MetalLookAndFeel;
37 import javax.swing.plaf.nimbus.NimbusLookAndFeel;
38 
39 /**
40  * @test
41  * @bug 8152159
42  * @summary LabelUI is not updated for TitledBorder
43  * @run main/othervm TitledBorderLabelUITest LAF
44  * @run main/othervm TitledBorderLabelUITest LabelUI
45  */
46 
47 public class TitledBorderLabelUITest {
48 
49     private static final int SIZE = 50;
50     private static boolean useLAF;
51 
main(String[] args)52     public static void main(String[] args) throws Exception {
53         useLAF = "LAF".equals(args[0]);
54         SwingUtilities.invokeAndWait(TitledBorderLabelUITest::createAndShowGUI);
55     }
56 
createAndShowGUI()57     private static void createAndShowGUI() {
58 
59         try {
60             UIManager.setLookAndFeel(new TestLookAndFeel());
61 
62             JLabel label = new JLabel("Test Label");
63             label.setSize(SIZE, SIZE);
64             TitledBorder border = new TitledBorder("ABCDEF");
65             label.setBorder(new TitledBorder(border));
66 
67             if (useLAF) {
68                 UIManager.setLookAndFeel(new NimbusLookAndFeel());
69             } else {
70                 UIManager.getDefaults().put("LabelUI", MetalLabelUI.class.getName());
71             }
72 
73             SwingUtilities.updateComponentTreeUI(label);
74 
75             paintToImage(label);
76 
77         } catch (Exception e) {
78             throw new RuntimeException(e);
79         }
80     }
81 
paintToImage(JComponent comp)82     private static void paintToImage(JComponent comp) {
83         BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
84         Graphics2D g = image.createGraphics();
85         comp.paint(g);
86         g.dispose();
87     }
88 
89     public static class TestLookAndFeel extends MetalLookAndFeel {
90 
91         @Override
initClassDefaults(UIDefaults table)92         protected void initClassDefaults(UIDefaults table) {
93             super.initClassDefaults(table);
94             table.put("LabelUI", TestLabelUI.class.getName());
95         }
96     }
97 
98     public static class TestLabelUI extends MetalLabelUI implements UIResource {
99 
createUI(JComponent c)100         public static ComponentUI createUI(JComponent c) {
101             return new TestLabelUI();
102         }
103 
104         @Override
paint(Graphics g, JComponent c)105         public void paint(Graphics g, JComponent c) {
106             super.paint(g, c);
107             throw new RuntimeException("New LabelUI is not installed!");
108         }
109     }
110 }