| 1 |
/** |
| 2 |
* Copied from JDK source code and modified to provide additional integration between JIDE components and native windows L&F. |
| 3 |
*/ |
| 4 |
package com.jidesoft.plaf.windows; |
| 5 |
|
| 6 |
import com.jidesoft.plaf.windows.TMSchema.Part; |
| 7 |
import com.jidesoft.plaf.windows.TMSchema.Prop; |
| 8 |
import com.jidesoft.plaf.windows.TMSchema.State; |
| 9 |
import com.jidesoft.plaf.windows.TMSchema.TypeEnum; |
| 10 |
import com.jidesoft.utils.ReflectionUtils; |
| 11 |
import com.jidesoft.utils.SystemInfo; |
| 12 |
import com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel; |
| 13 |
import com.sun.java.swing.plaf.windows.WindowsComboBoxUI; |
| 14 |
import sun.awt.image.SunWritableRaster; |
| 15 |
import sun.awt.image.SurfaceManager; |
| 16 |
import sun.awt.windows.ThemeReader; |
| 17 |
import sun.security.action.GetPropertyAction; |
| 18 |
import sun.swing.CachedPainter; |
| 19 |
|
| 20 |
import javax.swing.*; |
| 21 |
import javax.swing.border.AbstractBorder; |
| 22 |
import javax.swing.border.Border; |
| 23 |
import javax.swing.border.EmptyBorder; |
| 24 |
import javax.swing.border.LineBorder; |
| 25 |
import javax.swing.plaf.ColorUIResource; |
| 26 |
import javax.swing.plaf.InsetsUIResource; |
| 27 |
import javax.swing.plaf.UIResource; |
| 28 |
import javax.swing.text.JTextComponent; |
| 29 |
import java.awt.*; |
| 30 |
import java.awt.image.BufferedImage; |
| 31 |
import java.awt.image.DataBuffer; |
| 32 |
import java.awt.image.DataBufferInt; |
| 33 |
import java.awt.image.WritableRaster; |
| 34 |
import java.beans.PropertyChangeEvent; |
| 35 |
import java.beans.PropertyChangeListener; |
| 36 |
import java.security.AccessController; |
| 37 |
import java.util.HashMap; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Implements Windows XP Styles for the Windows Look and Feel. |
| 42 |
* |
| 43 |
* @author Leif Samuelsson |
| 44 |
*/ |
| 45 |
public class XPStyle { |
| 46 |
// Singleton instance of this class |
| 47 |
private static XPStyle xp; |
| 48 |
|
| 49 |
// Singleton instance of SkinPainter |
| 50 |
private static SkinPainter skinPainter = new SkinPainter(); |
| 51 |
|
| 52 |
private static Boolean themeActive = null; |
| 53 |
|
| 54 |
private HashMap<String, Border> borderMap; |
| 55 |
private HashMap<String, Color> colorMap; |
| 56 |
|
| 57 |
private boolean flatMenus; |
| 58 |
|
| 59 |
static { |
| 60 |
invalidateStyle(); |
| 61 |
|
| 62 |
// make sure whenever L&F changes, we clear the xpstyle. |
| 63 |
UIManager.addPropertyChangeListener(new PropertyChangeListener() { |
| 64 |
public void propertyChange(PropertyChangeEvent evt) { |
| 65 |
if ("lookAndFeel".equals(evt.getPropertyName())) |
| 66 |
invalidateStyle(); |
| 67 |
} |
| 68 |
}); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Static method for clearing the hashmap and loading the current XP style and theme |
| 73 |
*/ |
| 74 |
public static synchronized void invalidateStyle() { |
| 75 |
xp = null; |
| 76 |
themeActive = null; |
| 77 |
skinPainter.flush(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get the singleton instance of this class |
| 82 |
* |
| 83 |
* @return the singleton instance of this class or null if XP styles are not active or if this is not Windows XP |
| 84 |
*/ |
| 85 |
public static synchronized XPStyle getXP() { |
| 86 |
if (themeActive == null) { |
| 87 |
Toolkit toolkit = Toolkit.getDefaultToolkit(); |
| 88 |
themeActive = |
| 89 |
(Boolean) toolkit.getDesktopProperty("win.xpstyle.themeActive"); |
| 90 |
if (themeActive == null) { |
| 91 |
themeActive = Boolean.FALSE; |
| 92 |
} |
| 93 |
if (themeActive.booleanValue()) { |
| 94 |
GetPropertyAction propertyAction = |
| 95 |
new GetPropertyAction("swing.noxp"); |
| 96 |
if (AccessController.doPrivileged(propertyAction) == null && |
| 97 |
ThemeReader.isThemed() && |
| 98 |
!(UIManager.getLookAndFeel() |
| 99 |
instanceof WindowsClassicLookAndFeel)) { |
| 100 |
|
| 101 |
xp = new XPStyle(); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
return xp; |
| 106 |
} |
| 107 |
|
| 108 |
public static boolean isVista() { |
| 109 |
XPStyle xp = XPStyle.getXP(); |
| 110 |
return (xp != null && xp.isSkinDefined(null, Part.CP_DROPDOWNBUTTONRIGHT)); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get a named <code>String</code> value from the current style |
| 115 |
* |
| 116 |
* @param part a <code>Part</code> |
| 117 |
* @param state a <code>String</code> |
| 118 |
* @param prop a <code>String</code> |
| 119 |
* @return a <code>String</code> or null if key is not found in the current style |
| 120 |
* <p/> |
| 121 |
* This is currently only used by WindowsInternalFrameTitlePane for painting title foreground and can be removed |
| 122 |
* when no longer needed |
| 123 |
*/ |
| 124 |
public String getString(Component c, Part part, State state, Prop prop) { |
| 125 |
return getTypeEnumName(c, part, state, prop); |
| 126 |
} |
| 127 |
|
| 128 |
public TypeEnum getTypeEnum(Component c, Part part, State state, Prop prop) { |
| 129 |
int enumValue = ThemeReader.getEnum(part.getControlName(c), part.getValue(), |
| 130 |
State.getValue(part, state), |
| 131 |
prop.getValue()); |
| 132 |
return TypeEnum.getTypeEnum(prop, enumValue); |
| 133 |
} |
| 134 |
|
| 135 |
private static String getTypeEnumName(Component c, Part part, State state, Prop prop) { |
| 136 |
int enumValue = ThemeReader.getEnum(part.getControlName(c), part.getValue(), |
| 137 |
State.getValue(part, state), |
| 138 |
prop.getValue()); |
| 139 |
if (enumValue == -1) { |
| 140 |
return null; |
| 141 |
} |
| 142 |
return TypeEnum.getTypeEnum(prop, enumValue).getName(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Get a named <code>int</code> value from the current style |
| 148 |
* |
| 149 |
* @param part a <code>Part</code> |
| 150 |
* @return an <code>int</code> or null if key is not found in the current style |
| 151 |
*/ |
| 152 |
public int getInt(Component c, Part part, State state, Prop prop, int fallback) { |
| 153 |
return ThemeReader.getInt(part.getControlName(c), part.getValue(), |
| 154 |
State.getValue(part, state), |
| 155 |
prop.getValue()); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Get a named <code>Dimension</code> value from the current style |
| 160 |
* |
| 161 |
* @return a <code>Dimension</code> or null if key is not found in the current style |
| 162 |
* <p/> |
| 163 |
* This is currently only used by WindowsProgressBarUI and the value should probably be cached there instead of |
| 164 |
* here. |
| 165 |
*/ |
| 166 |
public Dimension getDimension(Component c, Part part, State state, Prop prop) { |
| 167 |
return ThemeReader.getPosition(part.getControlName(c), part.getValue(), |
| 168 |
State.getValue(part, state), |
| 169 |
prop.getValue()); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Get a named <code>Point</code> (e.g. a location or an offset) value from the current style |
| 174 |
* |
| 175 |
* @return a <code>Point</code> or null if key is not found in the current style |
| 176 |
* <p/> |
| 177 |
* This is currently only used by WindowsInternalFrameTitlePane for painting title foregound and can be removed when |
| 178 |
* no longer needed |
| 179 |
*/ |
| 180 |
public Point getPoint(Component c, Part part, State state, Prop prop) { |
| 181 |
Dimension d = ThemeReader.getPosition(part.getControlName(c), part.getValue(), |
| 182 |
State.getValue(part, state), |
| 183 |
prop.getValue()); |
| 184 |
if (d != null) { |
| 185 |
return new Point(d.width, d.height); |
| 186 |
} |
| 187 |
else { |
| 188 |
return null; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Get a named <code>Insets</code> value from the current style |
| 194 |
* |
| 195 |
* @return an <code>Insets</code> object or null if key is not found in the current style |
| 196 |
* <p/> |
| 197 |
* This is currently only used to create borders and by WindowsInternalFrameTitlePane for painting title foregound. |
| 198 |
* The return value is already cached in those places. |
| 199 |
*/ |
| 200 |
public Insets getMargin(Component c, Part part, State state, Prop prop) { |
| 201 |
return ThemeReader.getThemeMargins(part.getControlName(c), part.getValue(), |
| 202 |
State.getValue(part, state), |
| 203 |
prop.getValue()); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Get a named <code>Color</code> value from the current style |
| 209 |
* |
| 210 |
* @return a <code>Color</code> or null if key is not found in the current style |
| 211 |
*/ |
| 212 |
public synchronized Color getColor(Skin skin, Prop prop, Color fallback) { |
| 213 |
String key = skin.toString() + "." + prop.name(); |
| 214 |
Part part = skin.part; |
| 215 |
Color color = colorMap.get(key); |
| 216 |
if (color == null) { |
| 217 |
color = ThemeReader.getColor(part.getControlName(null), part.getValue(), |
| 218 |
State.getValue(part, skin.state), |
| 219 |
prop.getValue()); |
| 220 |
if (color != null) { |
| 221 |
color = new ColorUIResource(color); |
| 222 |
colorMap.put(key, color); |
| 223 |
} |
| 224 |
} |
| 225 |
return (color != null) ? color : fallback; |
| 226 |
} |
| 227 |
|
| 228 |
Color getColor(Component c, Part part, State state, Prop prop, Color fallback) { |
| 229 |
return getColor(new Skin(c, part, state), prop, fallback); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
/** |
| 234 |
* Get a named <code>Border</code> value from the current style |
| 235 |
* |
| 236 |
* @param part a <code>Part</code> |
| 237 |
* @return a <code>Border</code> or null if key is not found in the current style or if the style for the particular |
| 238 |
* part is not defined as "borderfill". |
| 239 |
*/ |
| 240 |
public synchronized Border getBorder(Component c, Part part) { |
| 241 |
if (part == Part.MENU) { |
| 242 |
// Special case because XP has no skin for menus |
| 243 |
if (flatMenus) { |
| 244 |
// TODO: The classic border uses this color, but we should |
| 245 |
// create a new UI property called "PopupMenu.borderColor" |
| 246 |
// instead. |
| 247 |
return new XPFillBorder(UIManager.getColor("InternalFrame.borderShadow"), |
| 248 |
1); |
| 249 |
} |
| 250 |
else { |
| 251 |
return null; // Will cause L&F to use classic border |
| 252 |
} |
| 253 |
} |
| 254 |
Skin skin = new Skin(c, part, null); |
| 255 |
Border border = borderMap.get(skin.string); |
| 256 |
if (border == null) { |
| 257 |
String bgType = getTypeEnumName(c, part, null, Prop.BGTYPE); |
| 258 |
if ("borderfill".equalsIgnoreCase(bgType)) { |
| 259 |
int thickness = getInt(c, part, null, Prop.BORDERSIZE, 1); |
| 260 |
Color color = getColor(skin, Prop.BORDERCOLOR, Color.black); |
| 261 |
border = new XPFillBorder(color, thickness); |
| 262 |
if (part == Part.CP_COMBOBOX) { |
| 263 |
border = new XPStatefulFillBorder(color, thickness, part, Prop.BORDERCOLOR); |
| 264 |
} |
| 265 |
} |
| 266 |
else if ("imagefile".equalsIgnoreCase(bgType)) { |
| 267 |
Insets m = getMargin(c, part, null, Prop.SIZINGMARGINS); |
| 268 |
if (m != null) { |
| 269 |
if (getBoolean(c, part, null, Prop.BORDERONLY)) { |
| 270 |
border = new XPImageBorder(c, part); |
| 271 |
} |
| 272 |
else if (part == Part.CP_COMBOBOX) { |
| 273 |
border = new EmptyBorder(1, 1, 1, 1); |
| 274 |
} |
| 275 |
else { |
| 276 |
if (part == Part.TP_BUTTON) { |
| 277 |
border = new XPEmptyBorder(new Insets(3, 3, 3, 3)); |
| 278 |
} |
| 279 |
else { |
| 280 |
border = new XPEmptyBorder(m); |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
} |
| 285 |
if (border != null) { |
| 286 |
borderMap.put(skin.string, border); |
| 287 |
} |
| 288 |
} |
| 289 |
return border; |
| 290 |
} |
| 291 |
|
| 292 |
private class XPFillBorder extends LineBorder implements UIResource { |
| 293 |
XPFillBorder(Color color, int thickness) { |
| 294 |
super(color, thickness); |
| 295 |
} |
| 296 |
|
| 297 |
public Insets getBorderInsets(Component c, Insets insets) { |
| 298 |
Insets margin = null; |
| 299 |
// |
| 300 |
// Ideally we'd have an interface defined for classes which |
| 301 |
// support margins (to avoid this hackery), but we've |
| 302 |
// decided against it for simplicity |
| 303 |
// |
| 304 |
if (c instanceof AbstractButton) { |
| 305 |
margin = ((AbstractButton) c).getMargin(); |
| 306 |
} |
| 307 |
else if (c instanceof JToolBar) { |
| 308 |
margin = ((JToolBar) c).getMargin(); |
| 309 |
} |
| 310 |
else if (c instanceof JTextComponent) { |
| 311 |
margin = ((JTextComponent) c).getMargin(); |
| 312 |
} |
| 313 |
insets.top = (margin != null ? margin.top : 0) + thickness; |
| 314 |
insets.left = (margin != null ? margin.left : 0) + thickness; |
| 315 |
insets.bottom = (margin != null ? margin.bottom : 0) + thickness; |
| 316 |
insets.right = (margin != null ? margin.right : 0) + thickness; |
| 317 |
|
| 318 |
return insets; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
private class XPStatefulFillBorder extends XPFillBorder { |
| 323 |
private final Part part; |
| 324 |
private final Prop prop; |
| 325 |
|
| 326 |
XPStatefulFillBorder(Color color, int thickness, Part part, Prop prop) { |
| 327 |
super(color, thickness); |
| 328 |
this.part = part; |
| 329 |
this.prop = prop; |
| 330 |
} |
| 331 |
|
| 332 |
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) { |
| 333 |
State state = State.NORMAL; |
| 334 |
// special casing for comboboxes. |
| 335 |
// there may be more special cases in the future |
| 336 |
if (c instanceof JComboBox) { |
| 337 |
JComboBox cb = (JComboBox) c; |
| 338 |
// note. in the future this should be replaced with a call |
| 339 |
// to BasicLookAndFeel.getUIOfType() |
| 340 |
if (cb.getUI() instanceof WindowsComboBoxUI) { |
| 341 |
WindowsComboBoxUI wcb = (WindowsComboBoxUI) cb.getUI(); |
| 342 |
// state = wcb.getXPComboBoxState(cb); |
| 343 |
} |
| 344 |
} |
| 345 |
lineColor = getColor(c, part, state, prop, Color.black); |
| 346 |
super.paintBorder(c, g, x, y, width, height); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
private class XPImageBorder extends AbstractBorder implements UIResource { |
| 351 |
Skin skin; |
| 352 |
|
| 353 |
XPImageBorder(Component c, Part part) { |
| 354 |
this.skin = getSkin(c, part); |
| 355 |
} |
| 356 |
|
| 357 |
public void paintBorder(Component c, Graphics g, |
| 358 |
int x, int y, int width, int height) { |
| 359 |
skin.paintSkin(g, x, y, width, height, null); |
| 360 |
} |
| 361 |
|
| 362 |
// public Insets getBorderInsets(Component c, Insets insets) { |
| 363 |
// Insets margin = null; |
| 364 |
// Insets borderInsets = skin.getContentMargin(); |
| 365 |
// if (borderInsets == null) { |
| 366 |
// borderInsets = new Insets(0, 0, 0, 0); |
| 367 |
// } |
| 368 |
// // |
| 369 |
// // Ideally we'd have an interface defined for classes which |
| 370 |
// // support margins (to avoid this hackery), but we've |
| 371 |
// // decided against it for simplicity |
| 372 |
// // |
| 373 |
// if (c instanceof AbstractButton) { |
| 374 |
// margin = ((AbstractButton) c).getMargin(); |
| 375 |
// } |
| 376 |
// else if (c instanceof JToolBar) { |
| 377 |
// margin = ((JToolBar) c).getMargin(); |
| 378 |
// } |
| 379 |
// else if (c instanceof JTextComponent) { |
| 380 |
// margin = ((JTextComponent) c).getMargin(); |
| 381 |
// } |
| 382 |
// insets.top = (margin != null ? margin.top : 0) + borderInsets.top; |
| 383 |
// insets.left = (margin != null ? margin.left : 0) + borderInsets.left; |
| 384 |
// insets.bottom = (margin != null ? margin.bottom : 0) + borderInsets.bottom; |
| 385 |
// insets.right = (margin != null ? margin.right : 0) + borderInsets.right; |
| 386 |
// |
| 387 |
// return insets; |
| 388 |
// } |
| 389 |
} |
| 390 |
|
| 391 |
private class XPEmptyBorder extends EmptyBorder implements UIResource { |
| 392 |
XPEmptyBorder(Insets m) { |
| 393 |
super(m.top + 2, m.left + 2, m.bottom + 2, m.right + 2); |
| 394 |
} |
| 395 |
|
| 396 |
public Insets getBorderInsets(Component c, Insets insets) { |
| 397 |
insets = super.getBorderInsets(c, insets); |
| 398 |
|
| 399 |
Insets margin = null; |
| 400 |
if (c instanceof AbstractButton) { |
| 401 |
Insets m = ((AbstractButton) c).getMargin(); |
| 402 |
// if this is a toolbar button then ignore getMargin() |
| 403 |
// and subtract the padding added by the constructor |
| 404 |
if (c.getParent() instanceof JToolBar |
| 405 |
&& !(c instanceof JRadioButton) |
| 406 |
&& !(c instanceof JCheckBox) |
| 407 |
&& m instanceof InsetsUIResource) { |
| 408 |
insets.top -= 2; |
| 409 |
insets.left -= 2; |
| 410 |
insets.bottom -= 2; |
| 411 |
insets.right -= 2; |
| 412 |
} |
| 413 |
else { |
| 414 |
margin = m; |
| 415 |
} |
| 416 |
} |
| 417 |
else if (c instanceof JToolBar) { |
| 418 |
margin = ((JToolBar) c).getMargin(); |
| 419 |
} |
| 420 |
else if (c instanceof JTextComponent) { |
| 421 |
margin = ((JTextComponent) c).getMargin(); |
| 422 |
} |
| 423 |
if (margin != null) { |
| 424 |
insets.top = margin.top + 2; |
| 425 |
insets.left = margin.left + 2; |
| 426 |
insets.bottom = margin.bottom + 2; |
| 427 |
insets.right = margin.right + 2; |
| 428 |
} |
| 429 |
return insets; |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
public boolean isSkinDefined(Component c, Part part) { |
| 434 |
return (part.getValue() == 0) |
| 435 |
|| ThemeReader.isThemePartDefined( |
| 436 |
part.getControlName(c), part.getValue(), 0); |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
/** |
| 441 |
* Get a <code>Skin</code> object from the current style for a named part (component type) |
| 442 |
* |
| 443 |
* @param part a <code>Part</code> |
| 444 |
* @return a <code>Skin</code> object |
| 445 |
*/ |
| 446 |
public synchronized Skin getSkin(Component c, Part part) { |
| 447 |
assert isSkinDefined(c, part) : "part " + part + " is not defined"; |
| 448 |
return new Skin(c, part, null); |
| 449 |
} |
| 450 |
|
| 451 |
|
| 452 |
long getThemeTransitionDuration(Component c, Part part, State stateFrom, |
| 453 |
State stateTo, Prop prop) { |
| 454 |
return ThemeReader.getThemeTransitionDuration(part.getControlName(c), |
| 455 |
part.getValue(), |
| 456 |
State.getValue(part, stateFrom), |
| 457 |
State.getValue(part, stateTo), |
| 458 |
(prop != null) ? prop.getValue() : 0); |
| 459 |
} |
| 460 |
|
| 461 |
|
| 462 |
/** |
| 463 |
* A class which encapsulates attributes for a given part (component type) and which provides methods for painting |
| 464 |
* backgrounds and glyphs |
| 465 |
*/ |
| 466 |
public static class Skin { |
| 467 |
final Component component; |
| 468 |
final Part part; |
| 469 |
final State state; |
| 470 |
|
| 471 |
private final String string; |
| 472 |
private Dimension size = null; |
| 473 |
|
| 474 |
Skin(Component component, Part part) { |
| 475 |
this(component, part, null); |
| 476 |
} |
| 477 |
|
| 478 |
Skin(Part part, State state) { |
| 479 |
this(null, part, state); |
| 480 |
} |
| 481 |
|
| 482 |
Skin(Component component, Part part, State state) { |
| 483 |
this.component = component; |
| 484 |
this.part = part; |
| 485 |
this.state = state; |
| 486 |
|
| 487 |
String str = part.getControlName(component) + "." + part.name(); |
| 488 |
if (state != null) { |
| 489 |
str += "(" + state.name() + ")"; |
| 490 |
} |
| 491 |
string = str; |
| 492 |
} |
| 493 |
|
| 494 |
// Insets getContentMargin() { |
| 495 |
// /* idk: it seems margins are the same for all 'big enough' |
| 496 |
// * bounding rectangles. |
| 497 |
// */ |
| 498 |
// int boundingWidth = 100; |
| 499 |
// int boundingHeight = 100; |
| 500 |
// |
| 501 |
// return ThemeReader.getThemeBackgroundContentMargins( |
| 502 |
// part.getControlName(null), part.getValue(), |
| 503 |
// 0, boundingWidth, boundingHeight); |
| 504 |
// } |
| 505 |
|
| 506 |
private int getWidth(State state) { |
| 507 |
if (size == null) { |
| 508 |
size = getPartSize(part, state); |
| 509 |
} |
| 510 |
return size.width; |
| 511 |
} |
| 512 |
|
| 513 |
int getWidth() { |
| 514 |
return getWidth((state != null) ? state : State.NORMAL); |
| 515 |
} |
| 516 |
|
| 517 |
private int getHeight(State state) { |
| 518 |
if (size == null) { |
| 519 |
size = getPartSize(part, state); |
| 520 |
} |
| 521 |
return size.height; |
| 522 |
} |
| 523 |
|
| 524 |
int getHeight() { |
| 525 |
return getHeight((state != null) ? state : State.NORMAL); |
| 526 |
} |
| 527 |
|
| 528 |
public String toString() { |
| 529 |
return string; |
| 530 |
} |
| 531 |
|
| 532 |
public boolean equals(Object obj) { |
| 533 |
return (obj instanceof Skin && ((Skin) obj).string.equals(string)); |
| 534 |
} |
| 535 |
|
| 536 |
public int hashCode() { |
| 537 |
return string.hashCode(); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* Paint a skin at x, y. |
| 542 |
* |
| 543 |
* @param g the graphics context to use for painting |
| 544 |
* @param dx the destination <i>x</i> coordinate |
| 545 |
* @param dy the destination <i>y</i> coordinate |
| 546 |
* @param state which state to paint |
| 547 |
*/ |
| 548 |
public void paintSkin(Graphics g, int dx, int dy, State state) { |
| 549 |
if (state == null) { |
| 550 |
state = this.state; |
| 551 |
} |
| 552 |
paintSkin(g, dx, dy, getWidth(state), getHeight(state), state); |
| 553 |
} |
| 554 |
|
| 555 |
/** |
| 556 |
* Paint a skin in an area defined by a rectangle. |
| 557 |
* |
| 558 |
* @param g the graphics context to use for painting |
| 559 |
* @param r a <code>Rectangle</code> defining the area to fill, may cause the image to be stretched or |
| 560 |
* tiled |
| 561 |
* @param state which state to paint |
| 562 |
*/ |
| 563 |
public void paintSkin(Graphics g, Rectangle r, State state) { |
| 564 |
paintSkin(g, r.x, r.y, r.width, r.height, state); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Paint a skin at a defined position and size This method supports animation. |
| 569 |
* |
| 570 |
* @param g the graphics context to use for painting |
| 571 |
* @param dx the destination <i>x</i> coordinate |
| 572 |
* @param dy the destination <i>y</i> coordinate |
| 573 |
* @param dw the width of the area to fill, may cause the image to be stretched or tiled |
| 574 |
* @param dh the height of the area to fill, may cause the image to be stretched or tiled |
| 575 |
* @param state which state to paint |
| 576 |
*/ |
| 577 |
public void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state) { |
| 578 |
if (SystemInfo.isJdk6Above() && ThemeReader.isGetThemeTransitionDurationDefined() |
| 579 |
&& component instanceof JComponent |
| 580 |
&& SwingUtilities.getAncestorOfClass(CellRendererPane.class, |
| 581 |
component) == null) { |
| 582 |
AnimationController.paintSkin((JComponent) component, this, |
| 583 |
g, dx, dy, dw, dh, state); |
| 584 |
} |
| 585 |
else { |
| 586 |
paintSkinRaw(g, dx, dy, dw, dh, state); |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
/** |
| 591 |
* Paint a skin at a defined position and size. This method does not trigger animation. It is needed for the |
| 592 |
* animation support. |
| 593 |
* |
| 594 |
* @param g the graphics context to use for painting |
| 595 |
* @param dx the destination <i>x</i> coordinate. |
| 596 |
* @param dy the destination <i>y</i> coordinate. |
| 597 |
* @param dw the width of the area to fill, may cause the image to be stretched or tiled |
| 598 |
* @param dh the height of the area to fill, may cause the image to be stretched or tiled |
| 599 |
* @param state which state to paint |
| 600 |
*/ |
| 601 |
public void paintSkinRaw(Graphics g, int dx, int dy, int dw, int dh, State state) { |
| 602 |
skinPainter.paint(null, g, dx, dy, dw, dh, this, state); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Paint a skin at a defined position and size |
| 607 |
* |
| 608 |
* @param g the graphics context to use for painting |
| 609 |
* @param dx the destination <i>x</i> coordinate |
| 610 |
* @param dy the destination <i>y</i> coordinate |
| 611 |
* @param dw the width of the area to fill, may cause the image to be stretched or tiled |
| 612 |
* @param dh the height of the area to fill, may cause the image to be stretched or tiled |
| 613 |
* @param state which state to paint |
| 614 |
* @param borderFill should test if the component uses a border fill and skip painting if it is |
| 615 |
*/ |
| 616 |
public void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state, |
| 617 |
boolean borderFill) { |
| 618 |
if (borderFill && "borderfill".equals(getTypeEnumName(component, part, |
| 619 |
state, Prop.BGTYPE))) { |
| 620 |
return; |
| 621 |
} |
| 622 |
skinPainter.paint(null, g, dx, dy, dw, dh, this, state); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
private static class SkinPainter extends CachedPainter { |
| 627 |
SkinPainter() { |
| 628 |
super(30); |
| 629 |
flush(); |
| 630 |
} |
| 631 |
|
| 632 |
public void flush() { |
| 633 |
super.flush(); |
| 634 |
} |
| 635 |
|
| 636 |
protected void paintToImage(Component c, Image image, Graphics g, |
| 637 |
int w, int h, Object[] args) { |
| 638 |
if (!SystemInfo.isJdk7Above()) { |
| 639 |
boolean accEnabled = false; |
| 640 |
Skin skin = (Skin) args[0]; |
| 641 |
TMSchema.Part part = skin.part; |
| 642 |
TMSchema.State state = (TMSchema.State) args[1]; |
| 643 |
if (state == null) { |
| 644 |
state = skin.state; |
| 645 |
} |
| 646 |
if (c == null) { |
| 647 |
c = skin.component; |
| 648 |
} |
| 649 |
BufferedImage bi = (BufferedImage) image; |
| 650 |
|
| 651 |
// Getting the DataBuffer for an image (as it's done below) defeats |
| 652 |
// possible future acceleration. |
| 653 |
// Calling setLocalAccelerationEnabled on that image's surface |
| 654 |
// manager re-enables it. |
| 655 |
SurfaceManager sm = SurfaceManager.getManager(bi); |
| 656 |
if (sm.getClass().getName().equals("sun.awt.image.CachingSurfaceManager")) { |
| 657 |
try { |
| 658 |
accEnabled = (Boolean) ReflectionUtils.callGet(sm, "isLocalAccelerationEnabled"); |
| 659 |
} |
| 660 |
catch (Exception e) { |
| 661 |
// ignore |
| 662 |
} |
| 663 |
} |
| 664 |
|
| 665 |
WritableRaster raster = bi.getRaster(); |
| 666 |
DataBufferInt buffer = (DataBufferInt) raster.getDataBuffer(); |
| 667 |
ThemeReader.paintBackground(buffer.getData(), |
| 668 |
part.getControlName(c), part.getValue(), |
| 669 |
TMSchema.State.getValue(part, state), |
| 670 |
0, 0, w, h, w); |
| 671 |
|
| 672 |
if (sm.getClass().getName().equals("sun.awt.image.CachingSurfaceManager")) { |
| 673 |
try { |
| 674 |
boolean oldAccEnabled = (Boolean) ReflectionUtils.callGet(sm, "isLocalAccelerationEnabled"); |
| 675 |
if (accEnabled != oldAccEnabled) { |
| 676 |
ReflectionUtils.callSet(sm, "setLocalAccelerationEnabled", accEnabled); |
| 677 |
ReflectionUtils.call(sm, "rasterChanged"); |
| 678 |
} |
| 679 |
} |
| 680 |
catch (Exception e) { |
| 681 |
// ignore |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
else { // copied from JDK7 XPStyle. To make the code compilable under JDk6, we use RefectionUtils |
| 686 |
boolean accEnabled = false; |
| 687 |
Skin skin = (Skin) args[0]; |
| 688 |
Part part = skin.part; |
| 689 |
State state = (State) args[1]; |
| 690 |
if (state == null) { |
| 691 |
state = skin.state; |
| 692 |
} |
| 693 |
if (c == null) { |
| 694 |
c = skin.component; |
| 695 |
} |
| 696 |
BufferedImage bi = (BufferedImage) image; |
| 697 |
|
| 698 |
WritableRaster raster = bi.getRaster(); |
| 699 |
DataBufferInt dbi = (DataBufferInt) raster.getDataBuffer(); |
| 700 |
// Note that stealData() requires a markDirty() afterwards |
| 701 |
// since we modify the data in it. |
| 702 |
try { |
| 703 |
ThemeReader.paintBackground( |
| 704 |
(int[]) ReflectionUtils.callStatic(SunWritableRaster.class, "stealData", new Class[]{DataBufferInt.class, int.class}, new Object[]{dbi, 0}), |
| 705 |
/*SunWritableRaster.stealData(dbi, 0),*/ |
| 706 |
part.getControlName(c), part.getValue(), |
| 707 |
State.getValue(part, state), |
| 708 |
0, 0, w, h, w); |
| 709 |
ReflectionUtils.callStatic(SunWritableRaster.class, "markDirty", new Class[]{DataBuffer.class}, new Object[]{dbi}); |
| 710 |
// SunWritableRaster.markDirty(dbi); |
| 711 |
} |
| 712 |
catch (Exception e) { |
| 713 |
e.printStackTrace(); |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
protected Image createImage(Component c, int w, int h, |
| 719 |
GraphicsConfiguration config, Object[] args) { |
| 720 |
return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
static class GlyphButton extends JButton { |
| 725 |
private Skin skin; |
| 726 |
|
| 727 |
public GlyphButton(Component parent, Part part) { |
| 728 |
XPStyle xp = getXP(); |
| 729 |
skin = xp.getSkin(parent, part); |
| 730 |
setBorder(null); |
| 731 |
setContentAreaFilled(false); |
| 732 |
setMinimumSize(new Dimension(5, 5)); |
| 733 |
setPreferredSize(new Dimension(16, 16)); |
| 734 |
setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); |
| 735 |
} |
| 736 |
|
| 737 |
public boolean isFocusTraversable() { |
| 738 |
return false; |
| 739 |
} |
| 740 |
|
| 741 |
protected State getState() { |
| 742 |
State state = State.NORMAL; |
| 743 |
if (!isEnabled()) { |
| 744 |
state = State.DISABLED; |
| 745 |
} |
| 746 |
else if (getModel().isPressed()) { |
| 747 |
state = State.PRESSED; |
| 748 |
} |
| 749 |
else if (getModel().isRollover()) { |
| 750 |
state = State.HOT; |
| 751 |
} |
| 752 |
return state; |
| 753 |
} |
| 754 |
|
| 755 |
public void paintComponent(Graphics g) { |
| 756 |
Dimension d = getSize(); |
| 757 |
skin.paintSkin(g, 0, 0, d.width, d.height, getState()); |
| 758 |
} |
| 759 |
|
| 760 |
public void setPart(Component parent, Part part) { |
| 761 |
XPStyle xp = getXP(); |
| 762 |
skin = xp.getSkin(parent, part); |
| 763 |
revalidate(); |
| 764 |
repaint(); |
| 765 |
} |
| 766 |
|
| 767 |
protected void paintBorder(Graphics g) { |
| 768 |
} |
| 769 |
|
| 770 |
|
| 771 |
} |
| 772 |
|
| 773 |
// Private constructor |
| 774 |
private XPStyle() { |
| 775 |
flatMenus = getSysBoolean(Prop.FLATMENUS); |
| 776 |
|
| 777 |
colorMap = new HashMap<String, Color>(); |
| 778 |
borderMap = new HashMap<String, Border>(); |
| 779 |
// Note: All further access to the maps must be synchronized |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
private boolean getBoolean(Component c, Part part, State state, Prop prop) { |
| 784 |
return ThemeReader.getBoolean(part.getControlName(c), part.getValue(), |
| 785 |
State.getValue(part, state), |
| 786 |
prop.getValue()); |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
static Dimension getPartSize(Part part, State state) { |
| 791 |
return ThemeReader.getPartSize(part.getControlName(null), part.getValue(), |
| 792 |
State.getValue(part, state)); |
| 793 |
} |
| 794 |
|
| 795 |
private static boolean getSysBoolean(Prop prop) { |
| 796 |
// We can use any widget name here, I guess. |
| 797 |
return ThemeReader.getSysBoolean("window", prop.getValue()); |
| 798 |
} |
| 799 |
} |