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 xp;
 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         return ThemeReader.getPosition(part.getControlName(c), part.getValue(),
 185                                        State.getValue(part, state),
 186                                        prop.getValue());
 187     }
 188 
 189     /** Get a named <code>Point</code> (e.g. a location or an offset) value
 190      *  from the current style
 191      *
 192      * @param key a <code>String</code>
 193      * @return a <code>Point</code> or null if key is not found
 194      *    in the current style
 195      *
 196      * This is currently only used by WindowsInternalFrameTitlePane for painting
 197      * title foregound and can be removed when no longer needed
 198      */
 199     Point getPoint(Component c, Part part, State state, Prop prop) {
 200         Dimension d = ThemeReader.getPosition(part.getControlName(c), part.getValue(),
 201                                               State.getValue(part, state),
 202                                               prop.getValue());
 203         if (d != null) {
 204             return new Point(d.width, d.height);
 205         } else {
 206             return null;
 207         }
 208     }
 209 
 210     /** Get a named <code>Insets</code> value from the current style
 211      *
 212      * @param key a <code>String</code>
 213      * @return an <code>Insets</code> object or null if key is not found
 214      *    in the current style
 215      *
 216      * This is currently only used to create borders and by
 217      * WindowsInternalFrameTitlePane for painting title foregound.
 218      * The return value is already cached in those places.
 219      */
 220     Insets getMargin(Component c, Part part, State state, Prop prop) {
 221         return ThemeReader.getThemeMargins(part.getControlName(c), part.getValue(),
 222                                            State.getValue(part, state),
 223                                            prop.getValue());
 224     }
 225 
 226 
 227     /** Get a named <code>Color</code> value from the current style
 228      *
 229      * @param part a <code>Part</code>
 230      * @return a <code>Color</code> or null if key is not found
 231      *    in the current style
 232      */
 233     synchronized Color getColor(Skin skin, Prop prop, Color fallback) {
 234         String key = skin.toString() + "." + prop.name();
 235         Part part = skin.part;
 236         Color color = colorMap.get(key);
 237         if (color == null) {
 238             color = ThemeReader.getColor(part.getControlName(null), part.getValue(),
 239                                          State.getValue(part, skin.state),
 240                                          prop.getValue());
 241             if (color != null) {
 242                 color = new ColorUIResource(color);
 243                 colorMap.put(key, color);
 244             }
 245         }
 246         return (color != null) ? color : fallback;
 247     }
 248 
 249     Color getColor(Component c, Part part, State state, Prop prop, Color fallback) {
 250         return getColor(new Skin(c, part, state), prop, fallback);
 251     }
 252 
 253 
 254 
 255     /** Get a named <code>Border</code> value from the current style
 256      *
 257      * @param part a <code>Part</code>
 258      * @return a <code>Border</code> or null if key is not found
 259      *    in the current style or if the style for the particular
 260      *    part is not defined as "borderfill".
 261      */
 262     synchronized Border getBorder(Component c, Part part) {
 263         if (part == Part.MENU) {
 264             // Special case because XP has no skin for menus
 265             if (flatMenus) {
 266                 // TODO: The classic border uses this color, but we should
 267                 // create a new UI property called "PopupMenu.borderColor"
 268                 // instead.
 269                 return new XPFillBorder(UIManager.getColor("InternalFrame.borderShadow"),
 270                                         1);
 271             } else {
 272                 return null;    // Will cause L&F to use classic border
 273             }
 274         }
 275         Skin skin = new Skin(c, part, null);
 276         Border border = borderMap.get(skin.string);
 277         if (border == null) {
 278             String bgType = getTypeEnumName(c, part, null, Prop.BGTYPE);
 279             if ("borderfill".equalsIgnoreCase(bgType)) {
 280                 int thickness = getInt(c, part, null, Prop.BORDERSIZE, 1);
 281                 Color color = getColor(skin, Prop.BORDERCOLOR, Color.black);
 282                 border = new XPFillBorder(color, thickness);
 283                 if (part == Part.CP_COMBOBOX) {
 284                     border = new XPStatefulFillBorder(color, thickness, part, Prop.BORDERCOLOR);
 285                 }
 286             } else if ("imagefile".equalsIgnoreCase(bgType)) {
 287                 Insets m = getMargin(c, part, null, Prop.SIZINGMARGINS);
 288                 if (m != null) {
 289                     if (getBoolean(c, part, null, Prop.BORDERONLY)) {
 290                         border = new XPImageBorder(c, part);
 291                     } else if (part == Part.CP_COMBOBOX) {
 292                         border = new EmptyBorder(1, 1, 1, 1);
 293                     } else {
 294                         if(part == Part.TP_BUTTON) {
 295                             border = new XPEmptyBorder(new Insets(3,3,3,3));
 296                         } else {
 297                             border = new XPEmptyBorder(m);
 298                         }
 299                     }
 300                 }
 301             }
 302             if (border != null) {
 303                 borderMap.put(skin.string, border);
 304             }
 305         }
 306         return border;
 307     }
 308 
 309     @SuppressWarnings("serial") // Superclass is not serializable across versions
 310     private class XPFillBorder extends LineBorder implements UIResource {
 311         XPFillBorder(Color color, int thickness) {
 312             super(color, thickness);
 313         }
 314 
 315         public Insets getBorderInsets(Component c, Insets insets)       {
 316             Insets margin = null;
 317             //
 318             // Ideally we'd have an interface defined for classes which
 319             // support margins (to avoid this hackery), but we've
 320             // decided against it for simplicity
 321             //
 322            if (c instanceof AbstractButton) {
 323                margin = ((AbstractButton)c).getMargin();
 324            } else if (c instanceof JToolBar) {
 325                margin = ((JToolBar)c).getMargin();
 326            } else if (c instanceof JTextComponent) {
 327                margin = ((JTextComponent)c).getMargin();
 328            }
 329            insets.top    = (margin != null? margin.top : 0)    + thickness;
 330            insets.left   = (margin != null? margin.left : 0)   + thickness;
 331            insets.bottom = (margin != null? margin.bottom : 0) + thickness;
 332            insets.right =  (margin != null? margin.right : 0)  + thickness;
 333 
 334            return insets;
 335         }
 336     }
 337 
 338     @SuppressWarnings("serial") // Superclass is not serializable across versions
 339     private class XPStatefulFillBorder extends XPFillBorder {
 340         private final Part part;
 341         private final Prop prop;
 342         XPStatefulFillBorder(Color color, int thickness, Part part, Prop prop) {
 343             super(color, thickness);
 344             this.part = part;
 345             this.prop = prop;
 346         }
 347 
 348         public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
 349             State state = State.NORMAL;
 350             // special casing for comboboxes.
 351             // there may be more special cases in the future
 352             if(c instanceof JComboBox) {
 353                 JComboBox cb = (JComboBox)c;
 354                 // note. in the future this should be replaced with a call
 355                 // to BasicLookAndFeel.getUIOfType()
 356                 if(cb.getUI() instanceof WindowsComboBoxUI) {
 357                     WindowsComboBoxUI wcb = (WindowsComboBoxUI)cb.getUI();
 358                     state = wcb.getXPComboBoxState(cb);
 359                 }
 360             }
 361             lineColor = getColor(c, part, state, prop, Color.black);
 362             super.paintBorder(c, g, x, y, width, height);
 363         }
 364     }
 365 
 366     @SuppressWarnings("serial") // Superclass is not serializable across versions
 367     private class XPImageBorder extends AbstractBorder implements UIResource {
 368         Skin skin;
 369 
 370         XPImageBorder(Component c, Part part) {
 371             this.skin = getSkin(c, part);
 372         }
 373 
 374         public void paintBorder(Component c, Graphics g,
 375                                 int x, int y, int width, int height) {
 376             skin.paintSkin(g, x, y, width, height, null);
 377         }
 378 
 379         public Insets getBorderInsets(Component c, Insets insets)       {
 380             Insets margin = null;
 381             Insets borderInsets = skin.getContentMargin();
 382             if(borderInsets == null) {
 383                 borderInsets = new Insets(0, 0, 0, 0);
 384             }
 385             //
 386             // Ideally we'd have an interface defined for classes which
 387             // support margins (to avoid this hackery), but we've
 388             // decided against it for simplicity
 389             //
 390            if (c instanceof AbstractButton) {
 391                margin = ((AbstractButton)c).getMargin();
 392            } else if (c instanceof JToolBar) {
 393                margin = ((JToolBar)c).getMargin();
 394            } else if (c instanceof JTextComponent) {
 395                margin = ((JTextComponent)c).getMargin();
 396            }
 397            insets.top    = (margin != null? margin.top : 0)    + borderInsets.top;
 398            insets.left   = (margin != null? margin.left : 0)   + borderInsets.left;
 399            insets.bottom = (margin != null? margin.bottom : 0) + borderInsets.bottom;
 400            insets.right  = (margin != null? margin.right : 0)  + borderInsets.right;
 401 
 402            return insets;
 403         }
 404     }
 405 
 406     @SuppressWarnings("serial") // Superclass is not serializable across versions
 407     private class XPEmptyBorder extends EmptyBorder implements UIResource {
 408         XPEmptyBorder(Insets m) {
 409             super(m.top+2, m.left+2, m.bottom+2, m.right+2);
 410         }
 411 
 412         public Insets getBorderInsets(Component c, Insets insets)       {
 413             insets = super.getBorderInsets(c, insets);
 414 
 415             Insets margin = null;
 416             if (c instanceof AbstractButton) {
 417                 Insets m = ((AbstractButton)c).getMargin();
 418                 // if this is a toolbar button then ignore getMargin()
 419                 // and subtract the padding added by the constructor
 420                 if(c.getParent() instanceof JToolBar
 421                    && ! (c instanceof JRadioButton)
 422                    && ! (c instanceof JCheckBox)
 423                    && m instanceof InsetsUIResource) {
 424                     insets.top -= 2;
 425                     insets.left -= 2;
 426                     insets.bottom -= 2;
 427                     insets.right -= 2;
 428                 } else {
 429                     margin = m;
 430                 }
 431             } else if (c instanceof JToolBar) {
 432                 margin = ((JToolBar)c).getMargin();
 433             } else if (c instanceof JTextComponent) {
 434                 margin = ((JTextComponent)c).getMargin();
 435             }
 436             if (margin != null) {
 437                 insets.top    = margin.top + 2;
 438                 insets.left   = margin.left + 2;
 439                 insets.bottom = margin.bottom + 2;
 440                 insets.right  = margin.right + 2;
 441             }
 442             return insets;
 443         }
 444     }
 445     boolean isSkinDefined(Component c, Part part) {
 446         return (part.getValue() == 0)
 447             || ThemeReader.isThemePartDefined(
 448                    part.getControlName(c), part.getValue(), 0);
 449     }
 450 
 451 
 452     /** Get a <code>Skin</code> object from the current style
 453      * for a named part (component type)
 454      *
 455      * @param part a <code>Part</code>
 456      * @return a <code>Skin</code> object
 457      */
 458     synchronized Skin getSkin(Component c, Part part) {
 459         assert isSkinDefined(c, part) : "part " + part + " is not defined";
 460         return new Skin(c, part, null);
 461     }
 462 
 463 
 464     long getThemeTransitionDuration(Component c, Part part, State stateFrom,
 465                                     State stateTo, Prop prop) {
 466          return ThemeReader.getThemeTransitionDuration(part.getControlName(c),
 467                                           part.getValue(),
 468                                           State.getValue(part, stateFrom),
 469                                           State.getValue(part, stateTo),
 470                                           (prop != null) ? prop.getValue() : 0);
 471     }
 472 
 473 
 474     /** A class which encapsulates attributes for a given part
 475      * (component type) and which provides methods for painting backgrounds
 476      * and glyphs
 477      */
 478     static class Skin {
 479         final Component component;
 480         final Part part;
 481         final State state;
 482 
 483         private final String string;
 484         private Dimension size = null;
 485 
 486         Skin(Component component, Part part) {
 487             this(component, part, null);
 488         }
 489 
 490         Skin(Part part, State state) {
 491             this(null, part, state);
 492         }
 493 
 494         Skin(Component component, Part part, State state) {
 495             this.component = component;
 496             this.part  = part;
 497             this.state = state;
 498 
 499             String str = part.getControlName(component) +"." + part.name();
 500             if (state != null) {
 501                 str += "("+state.name()+")";
 502             }
 503             string = str;
 504         }
 505 
 506         Insets getContentMargin() {
 507             /* idk: it seems margins are the same for all 'big enough'
 508              * bounding rectangles.
 509              */
 510             int boundingWidth = 100;
 511             int boundingHeight = 100;
 512 
 513             return ThemeReader.getThemeBackgroundContentMargins(
 514                 part.getControlName(null), part.getValue(),
 515                 0, boundingWidth, boundingHeight);
 516         }
 517 
 518         private int getWidth(State state) {
 519             if (size == null) {
 520                 size = getPartSize(part, state);
 521             }
 522             return size.width;
 523         }
 524 
 525         int getWidth() {
 526             return getWidth((state != null) ? state : State.NORMAL);
 527         }
 528 
 529         private int getHeight(State state) {
 530             if (size == null) {
 531                 size = getPartSize(part, state);
 532             }
 533             return size.height;
 534         }
 535 
 536         int getHeight() {
 537             return getHeight((state != null) ? state : State.NORMAL);
 538         }
 539 
 540         public String toString() {
 541             return string;
 542         }
 543 
 544         public boolean equals(Object obj) {
 545             return (obj instanceof Skin && ((Skin)obj).string.equals(string));
 546         }
 547 
 548         public int hashCode() {
 549             return string.hashCode();
 550         }
 551 
 552         /** Paint a skin at x, y.
 553          *
 554          * @param g   the graphics context to use for painting
 555          * @param dx  the destination <i>x</i> coordinate
 556          * @param dy  the destination <i>y</i> coordinate
 557          * @param state which state to paint
 558          */
 559         void paintSkin(Graphics g, int dx, int dy, State state) {
 560             if (state == null) {
 561                 state = this.state;
 562             }
 563             paintSkin(g, dx, dy, getWidth(state), getHeight(state), state);
 564         }
 565 
 566         /** Paint a skin in an area defined by a rectangle.
 567          *
 568          * @param g the graphics context to use for painting
 569          * @param r     a <code>Rectangle</code> defining the area to fill,
 570          *                     may cause the image to be stretched or tiled
 571          * @param state which state to paint
 572          */
 573         void paintSkin(Graphics g, Rectangle r, State state) {
 574             paintSkin(g, r.x, r.y, r.width, r.height, state);
 575         }
 576 
 577         /** Paint a skin at a defined position and size
 578          *  This method supports animation.
 579          *
 580          * @param g   the graphics context to use for painting
 581          * @param dx  the destination <i>x</i> coordinate
 582          * @param dy  the destination <i>y</i> coordinate
 583          * @param dw  the width of the area to fill, may cause
 584          *                  the image to be stretched or tiled
 585          * @param dh  the height of the area to fill, may cause
 586          *                  the image to be stretched or tiled
 587          * @param state which state to paint
 588          */
 589         void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state) {
 590             if (ThemeReader.isGetThemeTransitionDurationDefined()
 591                   && component instanceof JComponent
 592                   && SwingUtilities.getAncestorOfClass(CellRendererPane.class,
 593                                                        component) == null) {
 594                 AnimationController.paintSkin((JComponent) component, this,
 595                                               g, dx, dy, dw, dh, state);
 596             } else {
 597                 paintSkinRaw(g, dx, dy, dw, dh, state);
 598             }
 599         }
 600 
 601         /** Paint a skin at a defined position and size. This method
 602          *  does not trigger animation. It is needed for the animation
 603          *  support.
 604          *
 605          * @param g   the graphics context to use for painting
 606          * @param dx  the destination <i>x</i> coordinate.
 607          * @param dy  the destination <i>y</i> coordinate.
 608          * @param dw  the width of the area to fill, may cause
 609          *                  the image to be stretched or tiled
 610          * @param dh  the height of the area to fill, may cause
 611          *                  the image to be stretched or tiled
 612          * @param state which state to paint
 613          */
 614         void paintSkinRaw(Graphics g, int dx, int dy, int dw, int dh, State state) {
 615             skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
 616         }
 617 
 618         /** Paint a skin at a defined position and size
 619          *
 620          * @param g   the graphics context to use for painting
 621          * @param dx  the destination <i>x</i> coordinate
 622          * @param dy  the destination <i>y</i> coordinate
 623          * @param dw  the width of the area to fill, may cause
 624          *                  the image to be stretched or tiled
 625          * @param dh  the height of the area to fill, may cause
 626          *                  the image to be stretched or tiled
 627          * @param state which state to paint
 628          * @param borderFill should test if the component uses a border fill
 629                             and skip painting if it is
 630          */
 631         void paintSkin(Graphics g, int dx, int dy, int dw, int dh, State state,
 632                 boolean borderFill) {
 633             if(borderFill && "borderfill".equals(getTypeEnumName(component, part,
 634                     state, Prop.BGTYPE))) {
 635                 return;
 636             }
 637             skinPainter.paint(null, g, dx, dy, dw, dh, this, state);
 638         }
 639     }
 640 
 641     private static class SkinPainter extends CachedPainter {
 642         SkinPainter() {
 643             super(30);
 644             flush();
 645         }
 646 
 647         public void flush() {
 648             super.flush();
 649         }
 650 
 651         protected void paintToImage(Component c, Image image, Graphics g,
 652                                     int w, int h, Object[] args) {
 653             boolean accEnabled = false;
 654             Skin skin = (Skin)args[0];
 655             Part part = skin.part;
 656             State state = (State)args[1];
 657             if (state == null) {
 658                 state = skin.state;
 659             }
 660             if (c == null) {
 661                 c = skin.component;
 662             }
 663             BufferedImage bi = (BufferedImage)image;
 664 
 665             WritableRaster raster = bi.getRaster();
 666             DataBufferInt dbi = (DataBufferInt)raster.getDataBuffer();
 667             // Note that stealData() requires a markDirty() afterwards
 668             // since we modify the data in it.
 669             ThemeReader.paintBackground(SunWritableRaster.stealData(dbi, 0),
 670                                         part.getControlName(c), part.getValue(),
 671                                         State.getValue(part, state),
 672                                         0, 0, w, h, w);
 673             SunWritableRaster.markDirty(dbi);
 674         }
 675 
 676         protected Image createImage(Component c, int w, int h,
 677                                     GraphicsConfiguration config, Object[] args) {
 678             return new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
 679         }
 680     }
 681 
 682     @SuppressWarnings("serial") // Superclass is not serializable across versions
 683     static class GlyphButton extends JButton {
 684         private Skin skin;
 685 
 686         public GlyphButton(Component parent, Part part) {
 687             XPStyle xp = getXP();
 688             skin = xp.getSkin(parent, part);
 689             setBorder(null);
 690             setContentAreaFilled(false);
 691             setMinimumSize(new Dimension(5, 5));
 692             setPreferredSize(new Dimension(16, 16));
 693             setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE));
 694         }
 695 
 696         public boolean isFocusTraversable() {
 697             return false;
 698         }
 699 
 700         protected State getState() {
 701             State state = State.NORMAL;
 702             if (!isEnabled()) {
 703                 state = State.DISABLED;
 704             } else if (getModel().isPressed()) {
 705                 state = State.PRESSED;
 706             } else if (getModel().isRollover()) {
 707                 state = State.HOT;
 708             }
 709             return state;
 710         }
 711 
 712         public void paintComponent(Graphics g) {
 713             Dimension d = getSize();
 714             skin.paintSkin(g, 0, 0, d.width, d.height, getState());
 715         }
 716 
 717         public void setPart(Component parent, Part part) {
 718             XPStyle xp = getXP();
 719             skin = xp.getSkin(parent, part);
 720             revalidate();
 721             repaint();
 722         }
 723 
 724         protected void paintBorder(Graphics g) {
 725         }
 726 
 727 
 728     }
 729 
 730     // Private constructor
 731     private XPStyle() {
 732         flatMenus = getSysBoolean(Prop.FLATMENUS);
 733 
 734         colorMap  = new HashMap<String, Color>();
 735         borderMap = new HashMap<String, Border>();
 736         // Note: All further access to the maps must be synchronized
 737     }
 738 
 739 
 740     private boolean getBoolean(Component c, Part part, State state, Prop prop) {
 741         return ThemeReader.getBoolean(part.getControlName(c), part.getValue(),
 742                                       State.getValue(part, state),
 743                                       prop.getValue());
 744     }
 745 
 746 
 747 
 748     static Dimension getPartSize(Part part, State state) {
 749         return ThemeReader.getPartSize(part.getControlName(null), part.getValue(),
 750                                        State.getValue(part, state));
 751     }
 752 
 753     private static boolean getSysBoolean(Prop prop) {
 754         // We can use any widget name here, I guess.
 755         return ThemeReader.getSysBoolean("window", prop.getValue());
 756     }
 757 }