gtkstyle.java
来自「java jdk 1.4的源码」· Java 代码 · 共 1,748 行 · 第 1/5 页
JAVA
1,748 行
/* * @(#)GTKStyle.java 1.90 03/09/04 * * Copyright 2003 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.java.swing.plaf.gtk;import java.awt.*;import java.awt.event.*;import java.awt.image.*;import java.io.*;import java.lang.reflect.*;import javax.swing.*;import javax.swing.colorchooser.*;import javax.swing.plaf.*;import javax.swing.text.DefaultEditorKit;import java.util.*;import sun.awt.*;import java.security.*;import java.awt.RenderingHints;/** * @version 1.90, 09/04/03 * @author Scott Violet */class GTKStyle extends DefaultSynthStyle implements GTKConstants { private static final Object PENDING = new StringBuffer("Pending"); private static final Color BLACK_COLOR = new ColorUIResource(Color.BLACK); private 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 GTKGraphics GTK_GRAPHICS = new GTKGraphics(); private static final Object ICON_SIZE_KEY = new StringBuffer("IconSize"); /** * Default size for icons on dialogs (optionpane). */ private static final Dimension DIALOG_ICON_SIZE = new Dimension(48, 48); /** * Default size for icons on buttons. */ private static final Dimension BUTTON_ICON_SIZE = new Dimension(20, 20); // These have been included for completeness, but currently are not used private static final Dimension MENU_ICON_SIZE = new Dimension(16, 16); private static final Dimension SMALL_TOOLBAR_ICON_SIZE = new Dimension(18, 18); private static final Dimension LARGE_TOOLBAR_ICON_SIZE = new Dimension(24, 24); private static final Dimension DND_ICON_SIZE = new Dimension(32, 32); /** * 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; /** * GTK components have a single font which is used in all states. */ private Font font; /** * Icons. */ private GTKStockIconInfo[] icons; /** * Sets the size of a particular icon type. */ static void setIconSize(String key, int w, int h) { getIconSizeMap().put(key, new Dimension(w, h)); } /** * Returns the size of a particular icon type. This should NOT * modify the return value. */ static Dimension getIconSize(String key) { return (Dimension)getIconSizeMap().get(key); } private static Map getIconSizeMap() { AppContext appContext = AppContext.getAppContext(); Map iconSizes = (Map)appContext.get(ICON_SIZE_KEY); if (iconSizes == null) { iconSizes = new HashMap(); iconSizes.put("gtk-dialog", DIALOG_ICON_SIZE); iconSizes.put("gtk-button", BUTTON_ICON_SIZE); iconSizes.put("gtk-menu", MENU_ICON_SIZE); iconSizes.put("gtk-small-toolbar", SMALL_TOOLBAR_ICON_SIZE); iconSizes.put("gtk-large-toolbar", LARGE_TOOLBAR_ICON_SIZE); iconSizes.put("gtk-dnd", DND_ICON_SIZE); appContext.put(ICON_SIZE_KEY, iconSizes); } return iconSizes; } /** * Calculates the LIGHT color from the background color. */ static Color calculateLightColor(Color bg) { return GTKColorType.adjustColor(bg, 1.3f, 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 background color. */ static Color calculateMidColor(Color bg) { int r = bg.getRed(); int g = bg.getGreen(); int b = bg.getBlue(); int rLight = Math.min(255, (int)(r * 1.3)); int gLight = Math.min(255, (int)(g * 1.3)); int bLight = Math.min(255, (int)(b * 1.3)); int rDark = (int)(r * .7); int gDark = (int)(g * .7); int bDark = (int)(b * .7); return new ColorUIResource( (rLight + rDark) / 2, (gLight + gDark) / 2, (bLight + bDark) / 2); } /** * 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[] colors = new Color[GTKColorType.MAX_COUNT]; int r = bg.getRed(); int g = bg.getGreen(); int b = bg.getBlue(); int rLight = Math.min(255, (int)(r * 1.3)); int gLight = Math.min(255, (int)(g * 1.3)); int bLight = Math.min(255, (int)(b * 1.3)); int rDark = (int)(r * .7); int gDark = (int)(g * .7); int bDark = (int)(b * .7); colors[GTKColorType.BACKGROUND.getID()] = bg; colors[GTKColorType.LIGHT.getID()] = new ColorUIResource( rLight, gLight, bLight); colors[GTKColorType.DARK.getID()] = new ColorUIResource( rDark, gDark, bDark); colors[GTKColorType.MID.getID()] = new ColorUIResource( (rLight + rDark) / 2, (gLight + gDark) / 2, (bLight + bDark) / 2); 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; } public GTKStyle(DefaultSynthStyle style) { super(style); if (style instanceof GTKStyle) { GTKStyle gStyle = (GTKStyle)style; font = gStyle.font; xThickness = gStyle.xThickness; yThickness = gStyle.yThickness; icons = gStyle.icons; classSpecificValues = cloneClassSpecificValues( gStyle.classSpecificValues); } } public GTKStyle() { super(new Insets(-1, -1, -1, -1), true, null, null); } public GTKStyle(Font font) { this(); this.font = font; } public GTKStyle(StateInfo[] states, CircularIdentityList classSpecificValues, Font font, int xThickness, int yThickness, GTKStockIconInfo[] icons) { super(new Insets(-1, -1, -1, -1), true, states, null); this.font = font; this.xThickness = xThickness; this.yThickness = yThickness; this.icons = icons; this.classSpecificValues = classSpecificValues; } public SynthGraphics getSynthGraphics(SynthContext context) { return GTK_GRAPHICS; } public GTKEngine getEngine(SynthContext context) { GTKEngine engine = (GTKEngine)get(context, "engine"); if (engine == null) { return GTKEngine.INSTANCE; } return engine; } public SynthPainter getBorderPainter(SynthContext state) { SynthPainter painter = super.getBorderPainter(state); if (painter == null) { return GTKPainter.INSTANCE; } return painter; } public SynthPainter getBackgroundPainter(SynthContext state) { SynthPainter painter = super.getBackgroundPainter(state); if (painter == null) { return GTKPainter.INSTANCE; } return painter; } 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. * * 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"). */ public Object getClassSpecificValue(SynthContext context, String key) { if (classSpecificValues != null) { String gtkClass = GTKStyleFactory.gtkClassFor(context.getRegion()); 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 a class specific property. */ public int getClassSpecificIntValue(SynthContext context, String key, int defaultValue) { Object value = getClassSpecificValue(context, key); if (value instanceof Number) { return ((Number)value).intValue(); } return defaultValue; } /** * Returns a class specific property. */ public Insets getClassSpecificInsetsValue(SynthContext context, String key, Insets defaultValue) { Object value = getClassSpecificValue(context, key); if (value instanceof Insets) { return (Insets)value; } return defaultValue; } /** * Returns a class specific property. */ public boolean getClassSpecificBoolValue(SynthContext context, String key, boolean defaultValue) { Object value = getClassSpecificValue(context, key); if (value instanceof Boolean) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?