author | rupashka |
Sat, 28 May 2011 11:55:32 +0400 | |
changeset 9769 | 7af3ada3cf8e |
parent 5506 | 202f599c92aa |
child 25565 | ce603b34c98d |
permissions | -rw-r--r-- |
2658 | 1 |
/* |
5506 | 2 |
* Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. |
2658 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2658 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2658 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2658 | 24 |
*/ |
25 |
package javax.swing.plaf.nimbus; |
|
26 |
||
27 |
import javax.swing.Painter; |
|
28 |
import sun.swing.plaf.synth.SynthIcon; |
|
29 |
||
30 |
import javax.swing.plaf.synth.SynthContext; |
|
31 |
import javax.swing.*; |
|
32 |
import java.awt.*; |
|
33 |
import java.awt.image.BufferedImage; |
|
34 |
import javax.swing.plaf.UIResource; |
|
35 |
||
36 |
/** |
|
37 |
* An icon that delegates to a painter. |
|
38 |
* @author rbair |
|
39 |
*/ |
|
40 |
class NimbusIcon extends SynthIcon { |
|
41 |
private int width; |
|
42 |
private int height; |
|
43 |
private String prefix; |
|
44 |
private String key; |
|
45 |
||
46 |
NimbusIcon(String prefix, String key, int w, int h) { |
|
47 |
this.width = w; |
|
48 |
this.height = h; |
|
49 |
this.prefix = prefix; |
|
50 |
this.key = key; |
|
51 |
} |
|
52 |
||
53 |
@Override |
|
54 |
public void paintIcon(SynthContext context, Graphics g, int x, int y, |
|
55 |
int w, int h) { |
|
56 |
Painter painter = null; |
|
57 |
if (context != null) { |
|
58 |
painter = (Painter)context.getStyle().get(context, key); |
|
59 |
} |
|
60 |
if (painter == null){ |
|
61 |
painter = (Painter) UIManager.get(prefix + "[Enabled]." + key); |
|
62 |
} |
|
63 |
||
64 |
if (painter != null && context != null) { |
|
65 |
JComponent c = context.getComponent(); |
|
66 |
boolean rotate = false; |
|
67 |
boolean flip = false; |
|
68 |
//translatex and translatey are additional translations that |
|
69 |
//must occur on the graphics context when rendering a toolbar |
|
70 |
//icon |
|
71 |
int translatex = 0; |
|
72 |
int translatey = 0; |
|
73 |
if (c instanceof JToolBar) { |
|
74 |
JToolBar toolbar = (JToolBar)c; |
|
75 |
rotate = toolbar.getOrientation() == JToolBar.VERTICAL; |
|
76 |
flip = !toolbar.getComponentOrientation().isLeftToRight(); |
|
77 |
Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar); |
|
78 |
//we only do the +1 hack for UIResource borders, assuming |
|
79 |
//that the border is probably going to be our border |
|
80 |
if (toolbar.getBorder() instanceof UIResource) { |
|
81 |
if (o == BorderLayout.SOUTH) { |
|
82 |
translatey = 1; |
|
83 |
} else if (o == BorderLayout.EAST) { |
|
84 |
translatex = 1; |
|
85 |
} |
|
86 |
} |
|
3343
54d3dddd301f
6857360: NimbusLAF: Menu indicator looks ugly with RTL orientation.
peterz
parents:
2658
diff
changeset
|
87 |
} else if (c instanceof JMenu) { |
54d3dddd301f
6857360: NimbusLAF: Menu indicator looks ugly with RTL orientation.
peterz
parents:
2658
diff
changeset
|
88 |
flip = ! c.getComponentOrientation().isLeftToRight(); |
2658 | 89 |
} |
90 |
if (g instanceof Graphics2D){ |
|
91 |
Graphics2D gfx = (Graphics2D)g; |
|
92 |
gfx.translate(x, y); |
|
93 |
gfx.translate(translatex, translatey); |
|
94 |
if (rotate) { |
|
95 |
gfx.rotate(Math.toRadians(90)); |
|
96 |
gfx.translate(0, -w); |
|
97 |
painter.paint(gfx, context.getComponent(), h, w); |
|
98 |
gfx.translate(0, w); |
|
99 |
gfx.rotate(Math.toRadians(-90)); |
|
100 |
} else if (flip){ |
|
101 |
gfx.scale(-1, 1); |
|
102 |
gfx.translate(-w,0); |
|
103 |
painter.paint(gfx, context.getComponent(), w, h); |
|
104 |
gfx.translate(w,0); |
|
105 |
gfx.scale(-1, 1); |
|
106 |
} else { |
|
107 |
painter.paint(gfx, context.getComponent(), w, h); |
|
108 |
} |
|
109 |
gfx.translate(-translatex, -translatey); |
|
110 |
gfx.translate(-x, -y); |
|
111 |
} else { |
|
112 |
// use image if we are printing to a Java 1.1 PrintGraphics as |
|
113 |
// it is not a instance of Graphics2D |
|
114 |
BufferedImage img = new BufferedImage(w,h, |
|
115 |
BufferedImage.TYPE_INT_ARGB); |
|
116 |
Graphics2D gfx = img.createGraphics(); |
|
117 |
if (rotate) { |
|
118 |
gfx.rotate(Math.toRadians(90)); |
|
119 |
gfx.translate(0, -w); |
|
120 |
painter.paint(gfx, context.getComponent(), h, w); |
|
121 |
} else if (flip){ |
|
122 |
gfx.scale(-1, 1); |
|
123 |
gfx.translate(-w,0); |
|
124 |
painter.paint(gfx, context.getComponent(), w, h); |
|
125 |
} else { |
|
126 |
painter.paint(gfx, context.getComponent(), w, h); |
|
127 |
} |
|
128 |
gfx.dispose(); |
|
129 |
g.drawImage(img,x,y,null); |
|
130 |
img = null; |
|
131 |
} |
|
132 |
} |
|
133 |
} |
|
134 |
||
135 |
/** |
|
136 |
* Implements the standard Icon interface's paintIcon method as the standard |
|
137 |
* synth stub passes null for the context and this will cause us to not |
|
138 |
* paint any thing, so we override here so that we can paint the enabled |
|
139 |
* state if no synth context is available |
|
140 |
*/ |
|
141 |
@Override |
|
142 |
public void paintIcon(Component c, Graphics g, int x, int y) { |
|
143 |
Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key); |
|
144 |
if (painter != null){ |
|
145 |
JComponent jc = (c instanceof JComponent) ? (JComponent)c : null; |
|
146 |
Graphics2D gfx = (Graphics2D)g; |
|
147 |
gfx.translate(x, y); |
|
148 |
painter.paint(gfx, jc , width, height); |
|
149 |
gfx.translate(-x, -y); |
|
150 |
} |
|
151 |
} |
|
152 |
||
153 |
@Override |
|
154 |
public int getIconWidth(SynthContext context) { |
|
155 |
if (context == null) { |
|
156 |
return width; |
|
157 |
} |
|
158 |
JComponent c = context.getComponent(); |
|
159 |
if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) { |
|
160 |
//we only do the -1 hack for UIResource borders, assuming |
|
161 |
//that the border is probably going to be our border |
|
162 |
if (c.getBorder() instanceof UIResource) { |
|
163 |
return c.getWidth() - 1; |
|
164 |
} else { |
|
165 |
return c.getWidth(); |
|
166 |
} |
|
167 |
} else { |
|
168 |
return scale(context, width); |
|
169 |
} |
|
170 |
} |
|
171 |
||
172 |
@Override |
|
173 |
public int getIconHeight(SynthContext context) { |
|
174 |
if (context == null) { |
|
175 |
return height; |
|
176 |
} |
|
177 |
JComponent c = context.getComponent(); |
|
178 |
if (c instanceof JToolBar){ |
|
179 |
JToolBar toolbar = (JToolBar)c; |
|
180 |
if (toolbar.getOrientation() == JToolBar.HORIZONTAL) { |
|
181 |
//we only do the -1 hack for UIResource borders, assuming |
|
182 |
//that the border is probably going to be our border |
|
183 |
if (toolbar.getBorder() instanceof UIResource) { |
|
184 |
return c.getHeight() - 1; |
|
185 |
} else { |
|
186 |
return c.getHeight(); |
|
187 |
} |
|
188 |
} else { |
|
189 |
return scale(context, width); |
|
190 |
} |
|
191 |
} else { |
|
192 |
return scale(context, height); |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
/** |
|
197 |
* Scale a size based on the "JComponent.sizeVariant" client property of the |
|
198 |
* component that is using this icon |
|
199 |
* |
|
200 |
* @param context The synthContext to get the component from |
|
201 |
* @param size The size to scale |
|
202 |
* @return The scaled size or original if "JComponent.sizeVariant" is not |
|
203 |
* set |
|
204 |
*/ |
|
205 |
private int scale(SynthContext context, int size) { |
|
206 |
if (context == null || context.getComponent() == null){ |
|
207 |
return size; |
|
208 |
} |
|
209 |
// The key "JComponent.sizeVariant" is used to match Apple's LAF |
|
210 |
String scaleKey = (String) context.getComponent().getClientProperty( |
|
211 |
"JComponent.sizeVariant"); |
|
212 |
if (scaleKey != null) { |
|
213 |
if (NimbusStyle.LARGE_KEY.equals(scaleKey)) { |
|
214 |
size *= NimbusStyle.LARGE_SCALE; |
|
215 |
} else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) { |
|
216 |
size *= NimbusStyle.SMALL_SCALE; |
|
217 |
} else if (NimbusStyle.MINI_KEY.equals(scaleKey)) { |
|
218 |
// mini is not quite as small for icons as full mini is |
|
219 |
// just too tiny |
|
220 |
size *= NimbusStyle.MINI_SCALE + 0.07; |
|
221 |
} |
|
222 |
} |
|
223 |
return size; |
|
224 |
} |
|
225 |
} |