Skip to content

Commit e43a907

Browse files
committed
8271315: Redo: Nimbus JTree renderer properties persist across L&F changes
Reviewed-by: aivanov
1 parent 11c9fd8 commit e43a907

File tree

5 files changed

+279
-8
lines changed

5 files changed

+279
-8
lines changed

src/java.desktop/share/classes/javax/swing/plaf/nimbus/NimbusIcon.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* An icon that delegates to a painter.
4444
* @author rbair
4545
*/
46-
class NimbusIcon implements SynthIcon {
46+
class NimbusIcon implements SynthIcon, UIResource {
4747
private int width;
4848
private int height;
4949
private String prefix;

src/java.desktop/share/classes/javax/swing/plaf/nimbus/skin.laf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27352,10 +27352,10 @@
2735227352
<contentMargins top="0" bottom="0" left="0" right="0"/>
2735327353
<style>
2735427354
<textForeground>
27355-
<matte red="0" green="0" blue="0" alpha="0" uiDefaultParentName="text" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="false"/>
27355+
<matte red="0" green="0" blue="0" alpha="0" uiDefaultParentName="text" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="true"/>
2735627356
</textForeground>
2735727357
<textBackground>
27358-
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusLightBackground" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="false"/>
27358+
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusLightBackground" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="true"/>
2735927359
</textBackground>
2736027360
<background>
2736127361
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusLightBackground" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0"/>
@@ -27379,10 +27379,10 @@
2737927379
<insets top="2" left="0" bottom="1" right="5"/>
2738027380
</uiProperty>
2738127381
<uiProperty name="selectionForeground" type="COLOR">
27382-
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusSelectedText" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="false"/>
27382+
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusSelectedText" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="true"/>
2738327383
</uiProperty>
2738427384
<uiProperty name="selectionBackground" type="COLOR">
27385-
<matte red="57" green="105" blue="138" alpha="255" uiDefaultParentName="nimbusSelectionBackground" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="false"/>
27385+
<matte red="57" green="105" blue="138" alpha="255" uiDefaultParentName="nimbusSelectionBackground" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="true"/>
2738627386
</uiProperty>
2738727387
<uiProperty name="dropLineColor" type="COLOR">
2738827388
<matte red="242" green="242" blue="242" alpha="255" uiDefaultParentName="nimbusFocus" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0" alphaOffset="0"/>
@@ -27621,7 +27621,9 @@
2762127621
<stateTypes/>
2762227622
<contentMargins top="0" bottom="0" left="0" right="0"/>
2762327623
<style>
27624-
<textForeground/>
27624+
<textForeground>
27625+
<matte red="255" green="255" blue="255" alpha="255" uiDefaultParentName="nimbusSelectedText" hueOffset="0.0" saturationOffset="0.0" brightnessOffset="0.0" alphaOffset="0" uiResource="true"/>
27626+
</textForeground>
2762527627
<textBackground/>
2762627628
<background/>
2762727629
<uiproperties/>

src/java.desktop/share/classes/javax/swing/plaf/synth/SynthLabelUI.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import javax.swing.plaf.*;
3030
import javax.swing.plaf.basic.*;
3131
import javax.swing.text.View;
32+
import javax.swing.tree.DefaultTreeCellRenderer;
3233
import java.awt.Dimension;
3334
import java.awt.Rectangle;
3435
import java.awt.Insets;
@@ -207,8 +208,14 @@ protected void paint(SynthContext context, Graphics g) {
207208
Icon icon = (label.isEnabled()) ? label.getIcon() :
208209
label.getDisabledIcon();
209210

210-
g.setColor(context.getStyle().getColor(context,
211-
ColorType.TEXT_FOREGROUND));
211+
if (label instanceof DefaultTreeCellRenderer &&
212+
label.getForeground() instanceof UIResource) {
213+
g.setColor(label.getForeground());
214+
} else {
215+
g.setColor(context.getStyle().getColor(context,
216+
ColorType.TEXT_FOREGROUND));
217+
}
218+
212219
g.setFont(style.getFont(context));
213220
context.getStyle().getGraphicsUtils(context).paintText(
214221
context, g, label.getText(), icon,
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright (c) 2021, 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 8266510 8271315
26+
* @summary Verifies Nimbus JTree default tree cell renderer use selected text color
27+
* @run main/manual NimbusJTreeSelTextColor
28+
*/
29+
import java.util.concurrent.CountDownLatch;
30+
import java.util.concurrent.TimeUnit;
31+
import java.awt.Color;
32+
import java.awt.GridBagLayout;
33+
import java.awt.Insets;
34+
import java.awt.GridBagConstraints;
35+
import java.awt.event.ActionEvent;
36+
import java.awt.event.ActionListener;
37+
import java.awt.event.WindowAdapter;
38+
import java.awt.event.WindowEvent;
39+
import javax.swing.JButton;
40+
import javax.swing.JComponent;
41+
import javax.swing.JFrame;
42+
import javax.swing.JPanel;
43+
import javax.swing.JTextArea;
44+
import javax.swing.JTree;
45+
import javax.swing.SwingUtilities;
46+
import javax.swing.tree.DefaultTreeCellRenderer;
47+
import javax.swing.UIManager;
48+
49+
public class NimbusJTreeSelTextColor {
50+
51+
private static JFrame frame;
52+
private static JTree tree;
53+
private static DefaultTreeCellRenderer treeCellRenderer;
54+
private static volatile CountDownLatch countDownLatch;
55+
private static volatile boolean testResult;
56+
57+
private static final String INSTRUCTIONS = "INSTRUCTIONS:\n\n"
58+
+ "Verify selected text color is same as selected tree leaf icon color.\n "
59+
+ "If the color is same ie, white\n"
60+
+ "then press Pass otherwise press Fail.";
61+
62+
public static void main(String args[]) throws Exception{
63+
countDownLatch = new CountDownLatch(1);
64+
65+
SwingUtilities.invokeAndWait(NimbusJTreeSelTextColor::createUI);
66+
countDownLatch.await(5, TimeUnit.MINUTES);
67+
68+
if (!testResult) {
69+
throw new RuntimeException("Selected text color not same as selected tree leaf icon color!");
70+
}
71+
}
72+
73+
private static void createUI() {
74+
try {
75+
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
76+
} catch (Exception e) {
77+
throw new RuntimeException(e);
78+
}
79+
80+
JFrame mainFrame = new JFrame();
81+
GridBagLayout layout = new GridBagLayout();
82+
JPanel mainControlPanel = new JPanel(layout);
83+
JPanel resultButtonPanel = new JPanel(layout);
84+
85+
GridBagConstraints gbc = new GridBagConstraints();
86+
87+
gbc.gridx = 0;
88+
gbc.gridy = 0;
89+
gbc.insets = new Insets(5, 15, 5, 15);
90+
gbc.fill = GridBagConstraints.HORIZONTAL;
91+
mainControlPanel.add(createComponent(), gbc);
92+
93+
JTextArea instructionTextArea = new JTextArea();
94+
instructionTextArea.setText(INSTRUCTIONS);
95+
instructionTextArea.setEditable(false);
96+
instructionTextArea.setBackground(Color.white);
97+
98+
gbc.gridx = 0;
99+
gbc.gridy = 1;
100+
gbc.fill = GridBagConstraints.HORIZONTAL;
101+
mainControlPanel.add(instructionTextArea, gbc);
102+
103+
JButton passButton = new JButton("Pass");
104+
passButton.setActionCommand("Pass");
105+
passButton.addActionListener((ActionEvent e) -> {
106+
testResult = true;
107+
mainFrame.dispose();
108+
countDownLatch.countDown();
109+
110+
});
111+
112+
JButton failButton = new JButton("Fail");
113+
failButton.setActionCommand("Fail");
114+
failButton.addActionListener(new ActionListener() {
115+
@Override
116+
public void actionPerformed(ActionEvent e) {
117+
mainFrame.dispose();
118+
countDownLatch.countDown();
119+
}
120+
});
121+
122+
gbc.gridx = 0;
123+
gbc.gridy = 0;
124+
125+
resultButtonPanel.add(passButton, gbc);
126+
gbc.gridx = 1;
127+
gbc.gridy = 0;
128+
resultButtonPanel.add(failButton, gbc);
129+
130+
gbc.gridx = 0;
131+
gbc.gridy = 2;
132+
mainControlPanel.add(resultButtonPanel, gbc);
133+
134+
mainFrame.add(mainControlPanel);
135+
mainFrame.pack();
136+
137+
mainFrame.addWindowListener(new WindowAdapter() {
138+
139+
@Override
140+
public void windowClosing(WindowEvent e) {
141+
mainFrame.dispose();
142+
countDownLatch.countDown();
143+
}
144+
});
145+
mainFrame.setLocationRelativeTo(null);
146+
mainFrame.setVisible(true);
147+
}
148+
149+
private static JComponent createComponent() {
150+
tree = new JTree();
151+
152+
treeCellRenderer = new DefaultTreeCellRenderer();
153+
tree.setRootVisible(true);
154+
tree.setShowsRootHandles(true);
155+
156+
tree.setCellRenderer(treeCellRenderer);
157+
tree.setSelectionRow(1);
158+
return tree;
159+
}
160+
}
161+
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* Copyright (c) 2021, 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 javax.swing.UIManager;
25+
import javax.swing.UnsupportedLookAndFeelException;
26+
import javax.swing.plaf.UIResource;
27+
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
28+
import java.awt.Color;
29+
30+
/**
31+
* @test
32+
* @bug 8271315
33+
* @summary Nimbus JTree renderer properties persist across L&F changes
34+
* @key headful
35+
* @run main NimbusPropertiesDoNotImplUIResource
36+
*/
37+
38+
public class NimbusPropertiesDoNotImplUIResource {
39+
private static final String[] defPropertyKeys = new String[] {
40+
"Tree.leafIcon", "Tree.closedIcon",
41+
"Tree.openIcon", "Tree.selectionForeground",
42+
"Tree.textForeground", "Tree.selectionBackground",
43+
"Tree.textBackground", "Tree.selectionBorderColor"};
44+
45+
private static String failedKeys;
46+
47+
public static void main(String[] args) throws Exception {
48+
UIManager.LookAndFeelInfo[] installedLookAndFeels;
49+
installedLookAndFeels = UIManager.getInstalledLookAndFeels();
50+
51+
for (UIManager.LookAndFeelInfo LF : installedLookAndFeels) {
52+
try {
53+
UIManager.setLookAndFeel(LF.getClassName());
54+
failedKeys = null;
55+
for (String propertyKey : defPropertyKeys) {
56+
verifyProperty(propertyKey);
57+
}
58+
if (failedKeys != null) {
59+
throw new RuntimeException("JTree renderer Properties " +
60+
failedKeys + " are not instance of UIResource for "
61+
+ LF.getClassName());
62+
}
63+
} catch (UnsupportedLookAndFeelException e) {
64+
System.out.println("Note: LookAndFeel " + LF.getClassName()
65+
+ " is not supported on this configuration");
66+
}
67+
}
68+
69+
// Check that both uiResource option true and false work for
70+
// getDerivedColor method of NimbusLookAndFeel
71+
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
72+
Color color1 = ((NimbusLookAndFeel)UIManager.getLookAndFeel())
73+
.getDerivedColor("text", 0, 0, 0, 0, false);
74+
if (color1 instanceof UIResource) {
75+
throw new RuntimeException("color1 should not be instance of " +
76+
"UIResource");
77+
}
78+
79+
Color color2 = ((NimbusLookAndFeel)UIManager.getLookAndFeel())
80+
.getDerivedColor("text", 0, 0, 0, 0, true);
81+
if (!(color2 instanceof UIResource)) {
82+
throw new RuntimeException("color2 should be instance of " +
83+
"UIResource");
84+
}
85+
86+
}
87+
88+
private static void verifyProperty(String propertyKey) {
89+
Object property = UIManager.get(propertyKey);
90+
if (property == null) {
91+
return;
92+
}
93+
if (!(property instanceof UIResource)) {
94+
if (failedKeys == null) {
95+
failedKeys = ":" + propertyKey;
96+
} else {
97+
failedKeys += "," + propertyKey;
98+
}
99+
}
100+
}
101+
}

0 commit comments

Comments
 (0)