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