Skip to content

Commit 82a63a0

Browse files
author
Abhishek Kumar
committed
8258979: The image didn't show correctly with GTK LAF
Reviewed-by: psadhukhan, tr
1 parent 8d9814a commit 82a63a0

File tree

4 files changed

+144
-137
lines changed

4 files changed

+144
-137
lines changed

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

+24-19
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ public class SynthTreeUI extends BasicTreeUI
7373

7474
private boolean useTreeColors;
7575

76-
private Icon expandedIconWrapper = new ExpandedIconWrapper();
76+
private Icon expandedIconWrapper;
77+
78+
private Icon collapsedIconWrapper;
7779

7880
/**
7981
*
@@ -99,12 +101,19 @@ public Icon getExpandedIcon() {
99101
return expandedIconWrapper;
100102
}
101103

104+
@Override
105+
public Icon getCollapsedIcon() {
106+
return collapsedIconWrapper;
107+
}
108+
102109
/**
103110
* {@inheritDoc}
104111
*/
105112
@Override
106113
protected void installDefaults() {
107114
updateStyle(tree);
115+
expandedIconWrapper = new IconWrapper(expandedIcon);
116+
collapsedIconWrapper = new IconWrapper(collapsedIcon);
108117
}
109118

110119
private void updateStyle(JTree tree) {
@@ -778,44 +787,40 @@ public String getName() {
778787
}
779788

780789
//
781-
// BasicTreeUI directly uses expandIcon outside of the Synth methods.
790+
// BasicTreeUI directly uses expandIcon and collapsedIcon outside of the
791+
// Synth methods.
782792
// To get the correct context we return an instance of this that fetches
783793
// the SynthContext as needed.
784794
//
785-
private class ExpandedIconWrapper implements SynthIcon {
795+
796+
private class IconWrapper implements SynthIcon {
797+
Icon iconType;
798+
799+
public IconWrapper(Icon type) {
800+
super();
801+
iconType = type;
802+
}
803+
786804
public void paintIcon(SynthContext context, Graphics g, int x,
787805
int y, int w, int h) {
788806
if (context == null) {
789807
context = getContext(tree);
790-
SynthGraphicsUtils.paintIcon(expandedIcon, context, g, x, y, w, h);
791-
}
792-
else {
793-
SynthGraphicsUtils.paintIcon(expandedIcon, context, g, x, y, w, h);
794808
}
809+
SynthGraphicsUtils.paintIcon(iconType, context, g, x, y, w, h);
795810
}
796811

797812
public int getIconWidth(SynthContext context) {
798-
int width;
799813
if (context == null) {
800814
context = getContext(tree);
801-
width = SynthGraphicsUtils.getIconWidth(expandedIcon, context);
802815
}
803-
else {
804-
width = SynthGraphicsUtils.getIconWidth(expandedIcon, context);
805-
}
806-
return width;
816+
return SynthGraphicsUtils.getIconWidth(iconType, context);
807817
}
808818

809819
public int getIconHeight(SynthContext context) {
810-
int height;
811820
if (context == null) {
812821
context = getContext(tree);
813-
height = SynthGraphicsUtils.getIconHeight(expandedIcon, context);
814-
}
815-
else {
816-
height = SynthGraphicsUtils.getIconHeight(expandedIcon, context);
817822
}
818-
return height;
823+
return SynthGraphicsUtils.getIconHeight(iconType, context);
819824
}
820825
}
821826
}

test/jdk/javax/swing/JTree/8038113/bug8038113.html

-36
This file was deleted.

test/jdk/javax/swing/JTree/8038113/bug8038113.java

-82
This file was deleted.
+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
* Copyright (c) 2014, 2024, 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.io.File;
25+
import java.io.IOException;
26+
import java.awt.Color;
27+
import java.awt.Graphics2D;
28+
import java.awt.image.BufferedImage;
29+
import javax.imageio.ImageIO;
30+
import javax.swing.Icon;
31+
import javax.swing.JLabel;
32+
import javax.swing.JTree;
33+
import javax.swing.SwingUtilities;
34+
import javax.swing.plaf.basic.BasicTreeUI;
35+
import javax.swing.UIManager;
36+
import javax.swing.UnsupportedLookAndFeelException;
37+
38+
import static java.awt.image.BufferedImage.TYPE_INT_RGB;
39+
40+
/* @test
41+
* @key headful
42+
* @bug 8038113 8258979
43+
* @summary [macosx] JTree icon is not rendered in high resolution on Retina,
44+
* collapsed icon is not rendered for GTK LAF as well.
45+
* @run main bug8038113
46+
*/
47+
48+
public class bug8038113 {
49+
public static void main(String[] args) throws Exception {
50+
for (UIManager.LookAndFeelInfo laf :
51+
UIManager.getInstalledLookAndFeels()) {
52+
if (!laf.getName().contains("Motif")) {
53+
System.out.println("Testing LAF: " + laf.getName());
54+
SwingUtilities.invokeAndWait(() -> test(laf));
55+
}
56+
}
57+
}
58+
59+
public static void test(UIManager.LookAndFeelInfo laf) {
60+
setLookAndFeel(laf);
61+
final JTree tree = new JTree();
62+
final BasicTreeUI treeUI = (BasicTreeUI) tree.getUI();
63+
64+
Icon collapsedIcon = treeUI.getCollapsedIcon();
65+
Icon expandedIcon = treeUI.getExpandedIcon();
66+
BufferedImage img1 = paintToImage(expandedIcon);
67+
BufferedImage img2 = paintToImage(collapsedIcon);
68+
69+
if (!isImgRendered(img1)) {
70+
try {
71+
ImageIO.write(img1, "png", new File("Expanded_Icon_" + laf.getName() + ".png"));
72+
} catch (IOException ignored) {
73+
}
74+
throw new RuntimeException("Test Failed, Expanded not rendered for: "+laf.getName());
75+
}
76+
77+
if (!isImgRendered(img2)) {
78+
try {
79+
ImageIO.write(img2, "png", new File("Collapsed_Icon_" + laf.getName() + ".png"));
80+
} catch (IOException ignored) {
81+
}
82+
throw new RuntimeException("Test Failed, Collapsed Icon not rendered for: "+laf.getName());
83+
}
84+
System.out.println("Test Passed");
85+
}
86+
87+
private static void setLookAndFeel(UIManager.LookAndFeelInfo laf) {
88+
try {
89+
UIManager.setLookAndFeel(laf.getClassName());
90+
} catch (UnsupportedLookAndFeelException ignored) {
91+
System.out.println("Unsupported LAF: " + laf.getClassName());
92+
} catch (ClassNotFoundException | InstantiationException
93+
| IllegalAccessException e) {
94+
throw new RuntimeException(e);
95+
}
96+
}
97+
98+
private static BufferedImage paintToImage(Icon content) {
99+
BufferedImage im = new BufferedImage(content.getIconWidth(),
100+
content.getIconHeight(), TYPE_INT_RGB);
101+
Graphics2D g = (Graphics2D) im.getGraphics();
102+
g.setBackground(Color.WHITE);
103+
g.clearRect(0, 0, content.getIconWidth(), content.getIconHeight());
104+
content.paintIcon(new JLabel(), g, 0, 0);
105+
g.dispose();
106+
return im;
107+
}
108+
109+
private static boolean isImgRendered(BufferedImage img) {
110+
Color white = new Color(255, 255, 255);
111+
for (int x = 0; x < img.getWidth(); ++x) {
112+
for (int y = 0; y < img.getHeight(); ++y) {
113+
if (img.getRGB(x, y) != white.getRGB()) {
114+
return true;
115+
}
116+
}
117+
}
118+
return false;
119+
}
120+
}

0 commit comments

Comments
 (0)