📄 xpstyle.java
字号:
/* * @(#)XPStyle.java 1.10 03/03/20 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. *//* * <p>These classes are designed to be used while the * corresponding <code>LookAndFeel</code> class has been installed * (<code>UIManager.setLookAndFeel(new <i>XXX</i>LookAndFeel())</code>). * Using them while a different <code>LookAndFeel</code> is installed * may produce unexpected results, including exceptions. * Additionally, changing the <code>LookAndFeel</code> * maintained by the <code>UIManager</code> without updating the * corresponding <code>ComponentUI</code> of any * <code>JComponent</code>s may also produce unexpected results, * such as the wrong colors showing up, and is generally not * encouraged. * */package com.sun.java.swing.plaf.windows;import java.awt.*;import java.awt.image.*;import java.io.*;import java.security.AccessController;import java.util.*;import java.util.prefs.*;import javax.swing.*;import javax.swing.border.*;import javax.swing.plaf.*;import javax.swing.text.JTextComponent;import sun.security.action.GetPropertyAction;/** * Implements Windows XP Styles for the Windows Look and Feel. * * @version 1.10 03/20/03 * @author Leif Samuelsson */class XPStyle { // Singleton instance of this class private static XPStyle xp; private static Boolean themeActive = null; private HashMap map; private String styleFile; private String themeFile; static { invalidateStyle(); } /** Static method for clearing the hashmap and loading the * current XP style and theme */ static synchronized void invalidateStyle() { xp = null; themeActive = null; } /** 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 */ static synchronized XPStyle getXP() { 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) { xp = new XPStyle(); } } return xp; } /** Get a named <code>String</code> value from the current style * * @param key a <code>String</code> * @return a <code>String</code> or null if key is not found * in the current style */ synchronized String getString(String key) { return (String)map.get(key); } /** Get a named <code>int</code> value from the current style * * @param key a <code>String</code> * @return an <code>int</code> or null if key is not found * in the current style */ int getInt(String key, int fallback) { return parseInt(getString(key), fallback); } private int parseInt(String str, int fallback) { if (str != null) { try { return Integer.parseInt(str); } catch (NumberFormatException nfe) { abandonXP(); } } return fallback; } /** Get a named <code>Dimension</code> value from the current style * * @param key a <code>String</code> * @return a <code>Dimension</code> or null if key is not found * in the current style */ synchronized Dimension getDimension(String key) { 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; } /** Get a named <code>Point</code> (e.g. a location or an offset) value * from the current style * * @param key a <code>String</code> * @return a <code>Point</code> or null if key is not found * in the current style */ synchronized Point getPoint(String key) { 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; } /** Get a named <code>Insets</code> value from the current style * * @param key a <code>String</code> * @return an <code>Insets</code> object or null if key is not found * in the current style */ synchronized Insets getMargin(String key) { 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; } /** Get a named <code>Color</code> value from the current style * * @param key a <code>String</code> * @return a <code>Color</code> or null if key is not found * in the current style */ synchronized Color getColor(String key, Color fallback) { 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 Color(r, g, b); map.put("Color "+key, color); } } } return (color != null) ? color : fallback; } /** Get a named <code>Border</code> value from the current style * * @param key a <code>String</code> * @return a <code>Border</code> or null if key is not found * in the current style or if the style for the particular * category is not defined as "borderfill". */ synchronized Border getBorder(String category) { 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) { border = new XPEmptyBorder(m); } } if (border != null) { map.put("Border "+category, border); } } return border; } private class XPFillBorder extends LineBorder implements UIResource { XPFillBorder(Color color, int thickness) { super(color, thickness); } public Insets getBorderInsets(Component c) { return getBorderInsets(c, new Insets(0,0,0,0)); } public Insets getBorderInsets(Component c, Insets insets) { Insets margin = null; // // Ideally we'd have an interface defined for classes which // support margins (to avoid this hackery), but we've // decided against it for simplicity // if (c instanceof AbstractButton) { margin = ((AbstractButton)c).getMargin(); } else if (c instanceof JToolBar) { margin = ((JToolBar)c).getMargin(); } else if (c instanceof JTextComponent) { margin = ((JTextComponent)c).getMargin(); } insets.top = (margin != null? margin.top : 0) + thickness; insets.left = (margin != null? margin.left : 0) + thickness; insets.bottom = (margin != null? margin.bottom : 0) + thickness; insets.right = (margin != null? margin.right : 0) + thickness; return insets; } } private class XPEmptyBorder extends EmptyBorder implements UIResource { XPEmptyBorder(Insets m) { super(m.top+2, m.left+2, m.bottom+2, m.right+2); } public Insets getBorderInsets(Component c) { return getBorderInsets(c, getBorderInsets()); } public Insets getBorderInsets(Component c, Insets insets) { Insets margin = null; if (c instanceof AbstractButton) { margin = ((AbstractButton)c).getMargin(); } else if (c instanceof JToolBar) { margin = ((JToolBar)c).getMargin(); } else if (c instanceof JTextComponent) { margin = ((JTextComponent)c).getMargin(); } if (margin != null) { insets.top = margin.top + 2; insets.left = margin.left + 2; insets.bottom = margin.bottom + 2; insets.right = margin.right + 2; } return insets; } } /** Get an <code>XPStyle.Skin</code> object from the current style * for a named category (component type) * * @param category a <code>String</code> * @return an <code>XPStyle.Skin</code> object or null if the category is * not found in the current style */ synchronized Skin getSkin(String category) { Skin skin = (Skin)map.get("Skin "+category); if (skin == null) { skin = new Skin(category); map.put("Skin "+category, skin); } return skin; } /** A class which encapsulates attributes for a given category * (component type) and which provides methods for painting backgrounds * and glyphs */ class Skin { private Image image; private Insets contentMargin; private int w, h; private Image scaledImage; private Image glyphImage; private int frameCount; private Insets paintMargin; private boolean tile; private boolean sourceShrink; private boolean verticalFrames; /** The background image for the skin. * If this is null then width and height are not valid. */ Image getImage() { if (image != null) { return image; } else if (scaledImage != null) { return scaledImage; } else { return null; } } /** The content margin for a component skin is useful in * determining the minimum and preferred sizes for a component. */ Insets getContentMargin() { return contentMargin; } /** The width of the source image for this skin. * Not valid if getImage() returns null */ int getWidth() { return w; } /** The height of the source image for this skin. * Not valid if getImage() returns null */ int getHeight() { return h; } private Skin(String category) { XPStyle xp = getXP(); // Load main image image = xp.getImage(category+".imagefile", xp.getBoolean(category+".transparent", true)); // Look for additional (prescaled) images int n = 0; while (true) { if (xp.getString(category+".imagefile"+(n+1)) == null) { break; } n++; } if (n > 0) { int index = (n / 2) + 1; if ("dpi".equalsIgnoreCase(getString(category+".imageselecttype"))) { int dpi = Toolkit.getDefaultToolkit().getScreenResolution(); index = 1; for (int i = n; i >= 1; i--) { int minDpi = xp.getInt(category+".mindpi"+i, -1); if (minDpi > 0 && dpi >= minDpi) { index = i; break; } } } scaledImage = xp.getImage(category+".imagefile"+index, xp.getBoolean(category+".transparent", false) || xp.getBoolean(category+".glyphtransparent", false)); } frameCount = getInt(category+".imagecount", 1); paintMargin = getMargin(category+".sizingmargins"); contentMargin = getMargin(category+".contentmargins"); tile = "tile".equalsIgnoreCase(getString(category+".sizingtype")); sourceShrink = getBoolean(category+".sourceshrink", false); verticalFrames = "vertical".equalsIgnoreCase(getString(category+".imagelayout"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -