gtkstyle.java
来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,779 行 · 第 1/5 页
JAVA
1,779 行
/* * @(#)GTKStyle.java 1.116 04/06/24 * * Copyright 2004 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import com.sun.java.swing.SwingUtilities2;import javax.swing.plaf.synth.*;import java.awt.*;import java.awt.image.BufferedImage;import java.awt.image.RescaleOp;import java.lang.reflect.*;import javax.swing.*;import javax.swing.plaf.*;import java.util.*;import java.security.*;import sun.swing.plaf.synth.*;import sun.awt.AppContext;/** * SynthStyle implementation used in GTK. All painting is mapped to * a <code>GTKEngine</code>. * * @version 1.116, 06/24/04 * @author Scott Violet */public class GTKStyle extends DefaultSynthStyle implements GTKConstants { private static final String ICON_PROPERTY_PREFIX = "gtk.icon."; static final Color BLACK_COLOR = new ColorUIResource(Color.BLACK); static final Color WHITE_COLOR = new ColorUIResource(Color.WHITE); private static final Color[][] DEFAULT_COLORS; /** * State the color array at an particular index in DEFAULT_COLORS * represents. */ private static final int[] DEFAULT_COLOR_MAP; // NOTE: This will only be used if you weren't using GTKLookAndFeel // to create a GTKStyle, otherwise the default comes from GTKLookAndFeel. private static final Font DEFAULT_FONT = new FontUIResource( "sansserif", Font.PLAIN, 10); /** * Backing style properties that are used if the style does not * defined the property. */ // PENDING: This needs to be massaged so that it does not need to // be AppContext specific. In particular some of the Map values are // exposed and mutable and could effect other applets. private static final HashMap DATA = new HashMap(); /** * Maps from a key that is passed to Style.get to the equivalent class * specific key. */ private static final HashMap CLASS_SPECIFIC_MAP; private static final GTKGraphicsUtils GTK_GRAPHICS =new GTKGraphicsUtils(); /** * Indicates the thickness has not been set. */ static final int UNDEFINED_THICKNESS = -1; // NOTE: If you add a new field to this class you will need to update // addto, and possibly the clone and GTKStyle(GTKStyle) constructors. private int xThickness = 2; private int yThickness = 2; /** * Represents the values that are specific to a particular class. * This is a CircularIdentityList of CircularIdentityLists, where the * first level entries are gtk class names, and the second * CircularIdentityLists contains the actual key/value pairs. */ private CircularIdentityList classSpecificValues; /** * Icons. */ private GTKStockIconInfo[] icons; /** * Calculates the LIGHT color from the background color. */ static Color calculateLightColor(Color bg) { return GTKColorType.adjustColor(bg, 1.0f, 1.3f, 1.3f); } /** * Calculates the DARK color from the background color. */ static Color calculateDarkColor(Color bg) { return GTKColorType.adjustColor(bg, 1.0f, .7f, .7f); } /** * Calculates the MID color from the light and dark colors. */ static Color calculateMidColor(Color lightColor, Color darkColor) { int light = lightColor.getRGB(); int dark = darkColor.getRGB(); int rLight = (light & 0xFF0000) >> 16; int rDark = (dark & 0xFF0000) >> 16; int gLight = (light & 0x00FF00) >> 8; int gDark = (dark & 0x00FF00) >> 8; int bLight = (light & 0xFF); int bDark = (dark & 0xFF); return new ColorUIResource((((rLight + rDark) / 2) << 16) | (((gLight + gDark) / 2) << 8) | ((bLight + bDark) / 2)); } /** * Calculates the MID color from the background color. */ static Color calculateMidColor(Color bg) { return calculateMidColor(calculateLightColor(bg), calculateDarkColor(bg)); } /** * Creates an array of colors populated based on the passed in * the background color. Specifically this sets the * BACKGROUND, LIGHT, DARK, MID, BLACK, WHITE and FOCUS colors * from that of color, which is assumed to be the background. */ static Color[] getColorsFrom(Color bg, Color fg) { Color lightColor = calculateLightColor(bg); Color darkColor = calculateDarkColor(bg); Color midColor = calculateMidColor(lightColor, darkColor); Color[] colors = new Color[GTKColorType.MAX_COUNT]; colors[GTKColorType.BACKGROUND.getID()] = bg; colors[GTKColorType.LIGHT.getID()] = lightColor; colors[GTKColorType.DARK.getID()] = darkColor; colors[GTKColorType.MID.getID()] = midColor; colors[GTKColorType.BLACK.getID()] = BLACK_COLOR; colors[GTKColorType.WHITE.getID()] = WHITE_COLOR; colors[GTKColorType.FOCUS.getID()] = BLACK_COLOR; colors[GTKColorType.FOREGROUND.getID()] = fg; colors[GTKColorType.TEXT_FOREGROUND.getID()] = fg; colors[GTKColorType.TEXT_BACKGROUND.getID()] = WHITE_COLOR; return colors; } /** * Creates a new GTKStyle that is a copy of the passed in style. */ public GTKStyle(DefaultSynthStyle style) { super(style); if (style instanceof GTKStyle) { GTKStyle gStyle = (GTKStyle)style; xThickness = gStyle.xThickness; yThickness = gStyle.yThickness; icons = gStyle.icons; classSpecificValues = cloneClassSpecificValues( gStyle.classSpecificValues); } } /** * Creates an empty GTKStyle. */ public GTKStyle() { super(new Insets(-1, -1, -1, -1), true, null, null); } /** * Creates a GTKStyle with the specified font. * * @param font Font to use in GTK. */ public GTKStyle(Font font) { this(); setFont(font); } /** * Creates a GTKStyle with the specified parameters. * * @param states StateInfo specifying the colors and fonts to use for * a particular state. * @param classSpecificValues Values that are specific to a particular * class * @param font to use. * @param xThickness X thickness * @param yThickness Y thickness * @param GTKStockIconInfo stock icons for this style. */ GTKStyle(StateInfo[] states, CircularIdentityList classSpecificValues, Font font, int xThickness, int yThickness, GTKStockIconInfo[] icons) { super(new Insets(-1, -1, -1, -1), true, states, null); setFont(font); this.xThickness = xThickness; this.yThickness = yThickness; this.icons = icons; this.classSpecificValues = classSpecificValues; } /** * {@inheritDoc} */ public void installDefaults(SynthContext context) { super.installDefaults(context); if (!context.getRegion().isSubregion()) { context.getComponent().putClientProperty( SwingUtilities2.AA_TEXT_PROPERTY_KEY, GTKLookAndFeel.aaText); } } public SynthGraphicsUtils getGraphicsUtils(SynthContext context) { return GTK_GRAPHICS; } /** * Returns the object used to renderer the look. * * @param context SynthContext indentifying requestor * @return GTKEngine used to provide the look */ public GTKEngine getEngine(SynthContext context) { GTKEngine engine = (GTKEngine)get(context, "engine"); if (engine == null) { return GTKEngine.INSTANCE; } return engine; } /** * Returns a <code>SynthPainter</code> that will route the appropriate * calls to a <code>GTKEngine</code>. * * @param state SynthContext indentifying requestor * @return SynthPainter */ public SynthPainter getPainter(SynthContext state) { return GTKPainter.INSTANCE; } /** * Returns the Insets. If <code>to</code> is non-null the resulting * insets will be placed in it, otherwise a new Insets object will be * created and returned. * * @param context SynthContext indentifying requestor * @param to Where to place Insets * @return Insets. */ public Insets getInsets(SynthContext state, Insets insets) { insets = super.getInsets(state, insets); if (insets.top == -1) { insets.left = insets.right = insets.top = insets.bottom = 0; insets = GTKPainter.INSTANCE.getInsets(state, insets); } return insets; } /** * Returns the value for a class specific property. A class specific value * is a value that will be picked up based on class hierarchy. * For example, a value specified for JComponent would be inherited on * JButtons and JTrees, but not Button. * <p> * Note, the key used here should only contain the letters A-Z, a-z, the * digits 0-9, and the '-' character. If you need to request a value for * a key having characters outside this list, replace any other characters * with '-'. (ie. "default_border" should be "default-border"). * * @param region Region requesting class specific value * @param key Key identifying class specific value * @return Value, or null if one has not been defined. */ public Object getClassSpecificValue(Region region, String key) { if (classSpecificValues != null) { String gtkClass = GTKStyleFactory.gtkClassFor(region); while (gtkClass != null) { CircularIdentityList classValues = (CircularIdentityList) classSpecificValues.get(gtkClass); if (classValues != null) { Object value = classValues.get(key); if (value != null) { return value; } } gtkClass = GTKStyleFactory.gtkSuperclass(gtkClass); } } return null; } /** * Returns the value for a class specific property. A class specific value * is a value that will be picked up based on class hierarchy. * For example, a value specified for JComponent would be inherited on * JButtons and JTrees, but not Button. * <p> * Note, the key used here should only contain the letters A-Z, a-z, the * digits 0-9, and the '-' character. If you need to request a value for * a key having characters outside this list, replace any other characters * with '-'. (ie. "default_border" should be "default-border"). * * @param context SynthContext indentifying requestor * @param key Key identifying class specific value * @return Value, or null if one has not been defined. */ public Object getClassSpecificValue(SynthContext context, String key) { return getClassSpecificValue(context.getRegion(), key); } /** * Convenience method to get a class specific integer value. * * @param context SynthContext indentifying requestor * @param key Key identifying class specific value * @param defaultValue Returned if there is no value for the specified * type * @return Value, or defaultValue if <code>key</code> is not defined */ public int getClassSpecificIntValue(SynthContext context, String key, int defaultValue) { Object value = getClassSpecificValue(context, key); if (value instanceof Number) { return ((Number)value).intValue(); } return defaultValue; } /** * Convenience method to get a class specific Insets value. * * @param context SynthContext indentifying requestor * @param key Key identifying class specific value * @param defaultValue Returned if there is no value for the specified * type * @return Value, or defaultValue if <code>key</code> is not defined */ public Insets getClassSpecificInsetsValue(SynthContext context, String key, Insets defaultValue) { Object value = getClassSpecificValue(context, key); if (value instanceof Insets) { return (Insets)value; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?