gtkstyle.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,779 行 · 第 1/5 页

JAVA
1,779
字号
                icon = bestSource.toIcon();            }        }                if (icon == null) {            // Use a default icon            String propName = ICON_PROPERTY_PREFIX + key + '.' + type + '.' +                              (direction == RTL ? "rtl" : "ltr");             Image img = (Image)Toolkit.getDefaultToolkit().                                       getDesktopProperty(propName);            if (img != null) {                icon = new ImageIcon(img);                return icon;            } else {                icon = (Icon)((UIDefaults.LazyValue)LookAndFeel.makeIcon(                              GTKStyle.class, "resources/" + key + "-" + type +                              ".png")).createValue(null);            }        }                if (icon == null) {            return null;        }        BufferedImage image = null;         // If the stock icon we found had a wildcard size,         // we force the size to match that requested         if (bestSource == null || bestSource.getSize() == UNDEFINED) {             Dimension iconSize = GTKStockIconInfo.getIconSize(type);                         if (iconSize != null && (icon.getIconWidth() != iconSize.width ||                     icon.getIconHeight() != iconSize.height)) {                 image = new BufferedImage(iconSize.width, iconSize.height,                         BufferedImage.TYPE_INT_ARGB);                                 Graphics2D g2d = (Graphics2D)image.getGraphics();                                 // for nicer scaling                 g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,                         RenderingHints.VALUE_INTERPOLATION_BILINEAR);                                 Image oldImage = getImageFromIcon(icon, false);                 g2d.drawImage(oldImage, 0, 0, iconSize.width, iconSize.height, null);                 g2d.dispose();             }        }/* This is not done for now. We cache icons and use cached copies regardless   of the component state, so we don't want to cache desaturated icons           if (bestSource == null || bestSource.getState() == UNDEFINED) {             // We may need to change saturation for some states            int state = context.getComponentState();            if (state == SynthConstants.DISABLED ||                state == SynthConstants.MOUSE_OVER) {                                if (image == null) {                    image = (BufferedImage)getImageFromIcon(icon, true);                }                float rescaleFactor =                        (state == SynthConstants.DISABLED ? 0.8f : 1.2f);                 RescaleOp op = new RescaleOp(rescaleFactor, 0, null);                // RescaleOp allows for in-place filtering                op.filter(image, image);            }        }*/                if (image != null) {            icon = new ImageIcon(image);        }        return icon;    }        private Image getImageFromIcon(Icon icon, boolean requireBufferedImage) {        Image img = null;                if (icon instanceof ImageIcon) {             img = ((ImageIcon)icon).getImage();            if (requireBufferedImage && !(img instanceof BufferedImage)) {                img = null;            }        }        if (img == null) {             img = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(),                                    BufferedImage.TYPE_INT_ARGB);                                         Graphics g = img.getGraphics();             icon.paintIcon(null, g, 0, 0);             g.dispose();         }         return img;    }        /**               * Adds the specific label based properties from <code>style</code> to     * this style.     */    void addLabelProperties(GTKStyle style) {        StateInfo[] states = getStateInfo();        StateInfo[] oStates = style.getStateInfo();        // Take the font        setFont(style.getFontForState(null, null, 0));        // And TEXT_FOREGROUND        if (states == null) {            if (oStates == null) {                return;            }            states = new StateInfo[oStates.length];            for (int counter = 0; counter < oStates.length; counter++) {                Color color = oStates[counter].getColor(                                     GTKColorType.FOREGROUND);                states[counter] = createStateInfo(oStates[counter].                     getComponentState(), GTKColorType.TEXT_FOREGROUND, color);            }        }        else {            // Reset the text foreground of all our states, this will ensure            // the correct color is picked up if style doesn't specify a            // text color.            for (int counter = states.length - 1; counter >= 0; counter--) {                ((GTKStateInfo)states[counter]).setColor(                               GTKColorType.TEXT_FOREGROUND, null);            }            if (oStates != null) {                for (int oCounter = oStates.length - 1; oCounter >= 0;                         oCounter--) {                    boolean matched = false;                    StateInfo oState = oStates[oCounter];                    int componentState = oState.getComponentState();                    Color color = oState.getColor(GTKColorType.FOREGROUND);                    for (int tCounter = states.length - 1; tCounter >= 0;                             tCounter--) {                        if (componentState == states[tCounter].                                     getComponentState()) {                            ((GTKStateInfo)states[tCounter]).setColor(                                      GTKColorType.TEXT_FOREGROUND, color);                            matched = true;                            break;                        }                    }                    if (!matched) {                        StateInfo[] newStates = new StateInfo[states.length+1];                        System.arraycopy(states, 0, newStates, 0,                                         states.length);                        newStates[states.length] = createStateInfo(                                 componentState, GTKColorType.TEXT_FOREGROUND,                                 color);                        states = newStates;                    }                }            }        }    }    /**     * Creates a StateInfo with the specified component state, ColorType     * and color. Subclasses that create a custom GTKStateInfo will need     * to subclass and override this.     */    GTKStateInfo createStateInfo(int state, ColorType type, Color color) {        Color[] colors = new Color[GTKColorType.MAX_COUNT];        colors[type.getID()] = color;        return new GTKStateInfo(state, null, colors, null);    }    /**     * Adds a value specific to the style.     */    void put(Object key, Object value) {        Map data = getData();        if (data== null) {            data = new HashMap();            setData(data);        }        data.put(key, value);    }    /**     * Returns true if the style should fill in the background of the     * specified context for the specified state.     */    boolean fillBackground(SynthContext context, int state) {        GTKStateInfo info = (GTKStateInfo)getStateInfo(state);        if (info != null) {            Object backgroundImage = info.getBackgroundImage();            if (backgroundImage == "<none>" || backgroundImage == null) {                return true;            }            return false;        }        return true;    }    /**     * Returns the Icon to fill the background in with for the specified     * context and state.     */    Image getBackgroundImage(SynthContext context, int state) {        GTKStateInfo info = (GTKStateInfo)getStateInfo(state);        if (info != null) {            Object backgroundImage = info.getBackgroundImage();            if (backgroundImage instanceof Image) {                return (Image)backgroundImage;            }        }        return null;    }    /**     * Creates a clone of this GTKStyle.     *     * @return Clone of this GTKStyle.     */    public Object clone() {        GTKStyle style = (GTKStyle)super.clone();        style.classSpecificValues = cloneClassSpecificValues(                                         style.classSpecificValues);        return style;    }    /**     * Merges the contents of this Style with that of the passed in Style,     * returning the resulting merged syle. Properties of this     * <code>GTKStyle</code> will take precedence over those of the     * passed in <code>DefaultSynthStyle</code>. For example, if this     * style specifics a non-null font, the returned style will have its     * font so to that regardless of the <code>style</code>'s font.     *     * @param style Style to add our styles to     * @return Merged style.     */    public DefaultSynthStyle addTo(DefaultSynthStyle style) {        if (!(style instanceof GTKStyle)) {            style = new GTKStyle(style);        }        GTKStyle gtkStyle = (GTKStyle)super.addTo(style);        if (xThickness != UNDEFINED_THICKNESS) {            gtkStyle.xThickness = xThickness;        }        if (yThickness != UNDEFINED_THICKNESS) {            gtkStyle.yThickness = yThickness;        }        if (gtkStyle.icons == null) {            gtkStyle.icons = icons;        }        else if (icons != null) {            GTKStockIconInfo[] mergedIcons =                new GTKStockIconInfo[gtkStyle.icons.length + icons.length];                            System.arraycopy(icons, 0, mergedIcons, 0, icons.length);            System.arraycopy(gtkStyle.icons, 0, mergedIcons, icons.length, gtkStyle.icons.length);                        gtkStyle.icons = mergedIcons;        }                if (gtkStyle.classSpecificValues == null) {            gtkStyle.classSpecificValues =                cloneClassSpecificValues(classSpecificValues);        } else {            addClassSpecificValues(classSpecificValues, gtkStyle.classSpecificValues);        }                    return gtkStyle;    }    /**     * Adds the data from one set of class specific values into another.     *     * @param from the list to grab data from (may be null)     * @param to   the list to add data to (may not be null)     */    static void addClassSpecificValues(CircularIdentityList from,                                        CircularIdentityList to) {        if (to == null) {            throw new IllegalArgumentException("to may not be null");        }                if (from == null) {            return;        }        synchronized(from) {            Object firstKey = from.next();            if (firstKey != null) {                Object key = firstKey;                do {                    CircularIdentityList cList = ((CircularIdentityList)                            from.get());                    CircularIdentityList oSublist = (CircularIdentityList)                                     to.get(key);                    if (oSublist == null) {                        to.set(key, cList.clone());                    }                    else {                        Object cFirstKey = cList.next();                        if (cFirstKey != null) {                            Object cKey = cFirstKey;                            do {                                oSublist.set(cKey, cList.get());                                cKey = cList.next();                            } while (cKey != cFirstKey);                        }                    }                    key = from.next();                } while (key != firstKey);            }        }    }    /**     * Clones the class specific values.     */    static CircularIdentityList cloneClassSpecificValues(                    CircularIdentityList list) {        if (list == null) {            return null;        }        CircularIdentityList clone;        synchronized(list) {            Object firstKey = list.next();            if (firstKey == null) {                // Empty list                return null;            }            clone = new CircularIdentityList();            Object key = firstKey;            do {                clone.set(key, ((CircularIdentityList)list.get()).clone());                key = list.next();            } while (key != firstKey);        }        return clone;    }    /**         * GTKStockIconInfo mirrors the information from a stock declaration     * in the rc file: stock icon id, and a set of icon source     * specifications.     */    static class GTKStockIconInfo {        private String key;        private GTKIconSource[] sources;        private static Map<String,Integer> ICON_TYPE_MAP;        private static final Object ICON_SIZE_KEY = new StringBuffer("IconSize");                GTKStockIconInfo(String key, GTKIconSource[] sources) {            this.key = key.intern();            this.sources = sources;            Arrays.sort(this.sources);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?