FileDocCategorySizeDatePackage
XPStyle.javaAPI DocJava SE 5 API29214Fri Aug 26 14:54:50 BST 2005com.sun.java.swing.plaf.windows

XPStyle

public class XPStyle extends Object
Implements Windows XP Styles for the Windows Look and Feel.
version
1.22 08/09/05
author
Leif Samuelsson

Fields Summary
private static XPStyle
xp
private static Boolean
themeActive
private HashMap
map
private String
styleFile
private String
themeFile
Constructors Summary
private XPStyle()

	map = new HashMap();

	Toolkit toolkit = Toolkit.getDefaultToolkit();
	styleFile = (String)toolkit.getDesktopProperty("win.xpstyle.dllName");
	if (styleFile != null) {
            themeFile = getString("themeFile");
	}
	// Note: All further access to the map must be synchronized
    
Methods Summary
private voidabandonXP()

	if (AccessController.doPrivileged(new GetPropertyAction("swing.debug")) != null) {
	    System.err.println("An error occured in XPStyle while reading resource "+themeFile + " in " + styleFile);
	    new Exception().printStackTrace();
	}
	xp = null;
    
private java.awt.image.BufferedImageconvertToOpaque(java.awt.image.BufferedImage src)

        GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().
            getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage dest = (BufferedImage)gc.
            createCompatibleImage(src.getWidth(), src.getHeight(), Transparency.OPAQUE);
        ColorConvertOp op = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
        op.filter(src, dest);
        return dest;
    
private java.awt.image.BufferedImageconvertToTransparent(java.awt.image.BufferedImage src, java.awt.Color transparentColor)

	int w = src.getWidth();
	int h = src.getHeight();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
        BufferedImage dest = gc.createCompatibleImage(w, h, Transparency.BITMASK);
	int trgb;
	if (transparentColor != null) {
	    trgb = transparentColor.getRGB() | 0xff000000;
	} else {
	    trgb = 0xffff00ff;
	}
	// Copy pixels a scan line at a time
	int buf[] = new int[w];
        for (int y = 0; y < h; y++) {
	    src.getRGB(0, y, w, 1, buf, 0, w);
            for (int x = 0; x < w; x++) {
		if (buf[x] == trgb) {
		    buf[x] = 0;
		}
            }
	    dest.setRGB(0, y, w, 1, buf, 0, w);
	}
	return dest;
    
static java.awt.image.BufferedImagegetBitmapResource(java.lang.String key)

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Map resources = (Map)toolkit.getDesktopProperty("win.xpstyle.resources.images");
	return (BufferedImage)resources.get(key);
    
private java.lang.BooleangetBoolean(java.lang.String key)

	String value = getString(key);
	return (value != null) ? Boolean.valueOf("true".equalsIgnoreCase(value)) : null;
    
synchronized javax.swing.border.BordergetBorder(java.lang.String category)
Get a named Border value from the current style

param
key a String
return
a Border or null if key is not found in the current style or if the style for the particular category is not defined as "borderfill".

	if (category == "menu") {
	    // Special case because XP has no skin for menus
	    if (getBoolean("sysmetrics.flatmenus") == Boolean.TRUE) {
		// TODO: The classic border uses this color, but we should create
		// a new UI property called "PopupMenu.borderColor" instead.
		return new XPFillBorder(UIManager.getColor("InternalFrame.borderShadow"), 1);
	    } else {
		return null;	// Will cause L&F to use classic border
	    }
	}
	Border border = (Border)map.get("Border "+category);
	if (border == null) {
	    String bgType = getString(category + ".bgtype");
	    if ("borderfill".equalsIgnoreCase(bgType)) {
		int thickness = getInt(category + ".bordersize", 1);
		Color color = getColor(category + ".bordercolor", Color.black);
		border = new XPFillBorder(color, thickness);
	    } else if ("imagefile".equalsIgnoreCase(bgType)) {
		Insets m = getMargin(category + ".sizingmargins");
		if (m != null) {
		    if (getBoolean(category + ".borderonly") == Boolean.TRUE) {
			border = new XPImageBorder(category);
		    } else {
			border = new XPEmptyBorder(m);
		    }
		}
	    }
	    if (border != null) {
		map.put("Border "+category, border);
	    }
	}
	return border;
    
synchronized java.awt.ColorgetColor(java.lang.String key, java.awt.Color fallback)
Get a named Color value from the current style

param
key a String
return
a Color or null if key is not found in the current style

	Color color = (Color)map.get("Color "+key);
	if (color == null) {
	    String str = getString(key);
	    if (str != null) {
		StringTokenizer tok = new StringTokenizer(str, " \t,");
		int r = parseInt(tok.nextToken(), 0);
		int g = parseInt(tok.nextToken(), 0);
		int b = parseInt(tok.nextToken(), 0);
		if (r >= 0 && g >=0 && b >= 0) {
		    color = new ColorUIResource(r, g, b);
		    map.put("Color "+key, color);
		}
	    }
	}
	return (color != null) ? color : fallback;
    
synchronized java.awt.DimensiongetDimension(java.lang.String key)
Get a named Dimension value from the current style

param
key a String
return
a Dimension or null if key is not found in the current style

	Dimension d = (Dimension)map.get("Dimension "+key);
	if (d == null) {
	    String str = getString(key);
	    if (str != null) {
		StringTokenizer tok = new StringTokenizer(str, " \t,");
		d = new Dimension(parseInt(tok.nextToken(), 0),
				  parseInt(tok.nextToken(), 0));
		map.put("Dimension "+key, d);
	    }
	}
	return d;
    
private synchronized java.awt.ImagegetImage(java.lang.String key, java.lang.Boolean useTransparency, java.awt.Color transparentColor)

	String imageName = getString(key);
	if (imageName == null) {
            return null;
        }

        String imageKey = "Image " + imageName;
	BufferedImage image = (BufferedImage)map.get(imageKey);
         
	if (image != null) {
            return image;
        }
	
	image = getBitmapResource(imageName);

	if (image == null) {
            return null;
        }

	int oldTransparency = image.getColorModel().getTransparency();

	// Transparency can be forced to false for 32-bit images and forced
	// to true for 8/24-bit images. Do nothing if useTransparency is null.
        if (useTransparency == Boolean.FALSE && oldTransparency != Transparency.OPAQUE) {
            image = convertToOpaque(image);
        } else if (useTransparency == Boolean.TRUE && oldTransparency == Transparency.OPAQUE) {
	    // Assume we only use images from 8/24-bit bitmaps here
	    image = convertToTransparent(image, transparentColor);
	}
        // We cache images separately because multiple keys/skins
        // can point to the same image
        map.put(imageKey, image);
	return image;
    
intgetInt(java.lang.String key, int fallback)
Get a named int value from the current style

param
key a String
return
an int or null if key is not found in the current style

	return parseInt(getString(key), fallback);
    
synchronized java.awt.InsetsgetMargin(java.lang.String key)
Get a named Insets value from the current style

param
key a String
return
an Insets object or null if key is not found in the current style

	Insets insets = (Insets)map.get("Margin "+key);
	if (insets == null) {
	    String str = getString(key);
	    if (str != null) {
		StringTokenizer tok = new StringTokenizer(str, " \t,");
		insets = new Insets(0, 0, 0, 0);
		insets.left   = parseInt(tok.nextToken(), 0);
		insets.right  = parseInt(tok.nextToken(), 0);
		insets.top    = parseInt(tok.nextToken(), 0);
		insets.bottom = parseInt(tok.nextToken(), 0);
		map.put("Margin "+key, insets);
	    }
	}
	return insets;
    
synchronized java.awt.PointgetPoint(java.lang.String key)
Get a named Point (e.g. a location or an offset) value from the current style

param
key a String
return
a Point or null if key is not found in the current style

	Point p = (Point)map.get("Point "+key);
	if (p == null) {
	    String str = getString(key);
	    if (str != null) {
		StringTokenizer tok = new StringTokenizer(str, " \t,");
		p = new Point(parseInt(tok.nextToken(), 0),
			      parseInt(tok.nextToken(), 0));
		map.put("Point "+key, p);
	    }
	}
	return p;
    
synchronized com.sun.java.swing.plaf.windows.XPStyle$SkingetSkin(java.lang.String category)
Get an XPStyle.Skin object from the current style for a named category (component type)

param
category a String
return
an XPStyle.Skin object or null if the category is not found in the current style

	Skin skin = (Skin)map.get("Skin "+category);
	if (skin == null) {
	    skin = new Skin(category);
	    map.put("Skin "+category, skin);
	}
	return skin;
    
private static java.lang.StringgetString(java.lang.String key)
Get a named String value from the current style

param
key a String
return
a String or null if key is not found in the current style

	// Example: getString("a.b(c).d") => return getString("a.b", "c", "d");

	String category = "";
	String state = "";
	String attributeKey = key;

	int i = key.lastIndexOf('.");
	if (i > 0 && key.length() > i+1) {
	    category = key.substring(0, i);
	    attributeKey = key.substring(i+1);

	    i = category.lastIndexOf('(");
	    if (i > 0) {
		int i2 = category.indexOf(')", i);
		if (i2 == category.length() - 1) {
		    state = category.substring(i+1, i2);
		    category = category.substring(0, i);
		}
	    }
	}
	return getString(category, state, attributeKey);
    
static java.lang.StringgetString(java.lang.String category, java.lang.String state, java.lang.String attributeKey)
Get a named String value from the current style

param
category a String
param
state a String
param
attributeKey a String
return
a String or null if key is not found in the current style

        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Map resources = (Map)toolkit.getDesktopProperty("win.xpstyle.resources.strings");

	String value;
	if (category == null || category.length() == 0) {
	    value = (String)resources.get(attributeKey);
	} else {
	    if (state == null || state.length() == 0) {
		value = (String)resources.get(category + "." + attributeKey);
	    } else {
		value = (String)resources.get(category + "(" + state + ")." + attributeKey);
	    }
	}
	if (value == null) {
	    // Look for inheritance. For example, the attributeKey "transparent" in
	    // category "window.closebutton" can be inherited from category "window".
	    int i = category.lastIndexOf('.");
	    if (i > 0) {
		value = getString(category.substring(0, i), state, attributeKey);
	    }
	}
	if (value == null && state != null && state.length() > 0) {
	    // Try again without a state specified
	    value = getString(category, "", attributeKey);
	}
	return value;
    
static synchronized com.sun.java.swing.plaf.windows.XPStylegetXP()
Get the singleton instance of this class

return
the signleton instance of this class or null if XP styles are not active or if this is not Windows XP

	if (themeActive == null) {
            Toolkit toolkit = Toolkit.getDefaultToolkit();
	    themeActive = (Boolean)toolkit.getDesktopProperty("win.xpstyle.themeActive");
	    if (themeActive == null) {
		themeActive = Boolean.FALSE;
	    }
   	    if (themeActive.booleanValue() &&
		AccessController.doPrivileged(new GetPropertyAction("swing.noxp")) == null &&
		!(UIManager.getLookAndFeel() instanceof WindowsClassicLookAndFeel)) {
		xp = new XPStyle();
	    }
	}
	return xp;
    
static synchronized voidinvalidateStyle()
Static method for clearing the hashmap and loading the current XP style and theme


     
	invalidateStyle();
    
	xp = null;
	themeActive = null;
    
private intparseInt(java.lang.String str, int fallback)

	if (str != null) {
	    try {
		return Integer.parseInt(str);
	    } catch (NumberFormatException nfe) {
		abandonXP();
	    }
	}
	return fallback;