gtkstylefactory.java

来自「java jdk 1.4的源码」· Java 代码 · 共 795 行 · 第 1/2 页

JAVA
795
字号
                            java.util.List matches, JComponent c, Region id) {        for (int counter = styles.size() - 1; counter >= 0; counter--){            StyleAssociation sa = (StyleAssociation)styles.get(counter);            if (sa.matches(path)) {                matches.add(sa.getStyle());            }        }    }    private void getClassMatches(java.util.List matches, JComponent c,                                 Region id) {        if (_depth == null) {            _depth = new int[4];        }        int[] sDepth = _depth;        int matched = 0;        int startMatchLength = matches.size();        for (int counter = _classStyles.size() - 1; counter >= 0; counter--){            StyleAssociation sa = (StyleAssociation)_classStyles.get(counter);            Object klass = getClass(c, id);            while (klass != null) {                if (sa.matches(getClassName(klass))) {                    int depth = 0;                    while ((klass = getSuperclass(klass)) != null) {                        depth++;                    }                    if (matched == 0) {                        sDepth[0] = depth;                        matches.add(sa.getStyle());                    }                    else {                        int sCounter = 0;                        while (sCounter < matched && depth < sDepth[sCounter]){                            sCounter++;                        }                        matches.add(sCounter + startMatchLength,                                       sa.getStyle());                        if (matched + 1 == sDepth.length) {                            int[] newDepth = new int[sDepth.length * 2];                            System.arraycopy(sDepth, 0, newDepth, 0,                                             sDepth.length);                            _depth = newDepth;                            sDepth = newDepth;                        }                        if (sCounter < matched) {                            System.arraycopy(sDepth, 0, sDepth, 0, sCounter);                            System.arraycopy(sDepth, sCounter, sDepth,                                             sCounter + 1, matched - sCounter);                        }                        sDepth[sCounter] = depth;                    }                    matched++;                    break;                }                klass = getSuperclass(klass);            }        }    }    /**     * Caches the specified style.     */    private void cacheStyle(java.util.List styles, SynthStyle style) {        BakedArrayList cachedStyles = new BakedArrayList(styles);        _resolvedStyles.put(cachedStyles, style);    }    /**     * Returns the cached style from the passed in arguments.     */    private SynthStyle getCachedStyle(java.util.List styles) {        if (styles.size() == 0) {            return null;        }        return (SynthStyle)_resolvedStyles.get(styles);    }    /**     * Creates a single Style from the passed in styles. The passed in List     * is reverse sorted, that is the most recently added style found to     * match will be first.     */    private SynthStyle mergeStyles(java.util.List styles) {        int size = styles.size();        if (size == 0) {            return null;        }        else if (size == 1) {            return (SynthStyle)((DefaultSynthStyle)styles.get(0)).clone();        }        // NOTE: merging is done backwards as DefaultSynthStyleFactory reverses        // order, that is, the most specific style is first.        DefaultSynthStyle style = (DefaultSynthStyle)styles.get(size - 1);        style = (DefaultSynthStyle)style.clone();        for (int counter = size - 2; counter >= 0; counter--) {            style = ((DefaultSynthStyle)styles.get(counter)).addTo(style);        }        return style;    }    /**     * Builds the path returning a CharSequence describing the path. A     * temporary StringBuffer is provided that should NOT be cached.     */    private CharSequence getPath(int type, Component c, Region id) {        _tmpPath.setLength(0);        if (type == WIDGET && id == Region.TOOL_TIP) {            if (c.getName() == null) {                _tmpPath.append("gtk-tooltips");            }            else {                _tmpPath.append(c.getName());            }        }        else {            _getPath(_tmpPath, type, c, id);        }        if (_isLabel) {            if (_tmpPath.length() > 0) {                _tmpPath.append('.');            }            _tmpPath.append(getName(c, Region.LABEL));        }        return _tmpPath;    }    private void _getPath(StringBuffer path, int type, Component c,Region id) {        if (c instanceof JComponent) {            boolean isSubregion = (id != null && id.isSubregion());            if (isSubregion) {                _getPath(path, type, c, null);            }            else {                _getPath(path, type, c.getParent(), id);            }            String key = null;            if (type == WIDGET && !isSubregion) {                key = c.getName();            }            if (key == null) {                if (isSubregion) {                    key = getName(c, id);                }                else {                    Region region = SynthLookAndFeel.getRegion((JComponent)c);                    if (region != null) {                        key = getName(c, region);                    }                }            }            if (path.length() > 0) {                path.append('.');            }            path.append(key);        }    }    /**     * Returns a class identifer for <code>c</code>.     */    protected Object getClass(JComponent c, Region id) {        if (_isLabel) {            id = Region.LABEL;        }        else if (id == Region.ROOT_PANE) {            Object name = getRootPaneParentType(c);            if (name != null) {                return name;            }        }        String klass = gtkClassFor(id);        if (klass == "GtkLabel") {            if (c.getName() == "TableHeader.renderer") {                return "GtkButton";            }        }        return klass;    }    private SynthStyle getMergedStyle(JComponent c, Region id,                                      SynthStyle style) {        SynthStyle labelStyle;        try {            _isLabel = true;            labelStyle = (GTKStyle)_getStyle(c, id);        } finally {            _isLabel = false;        }        _labelStyleList.clear();        _labelStyleList.add(style);        _labelStyleList.add(labelStyle);        _labelStyleList.bake();        GTKStyle mergedStyle = (GTKStyle)_mergedStyleMap.get(_labelStyleList);        if (mergedStyle == null) {            mergedStyle = (GTKStyle)((DefaultSynthStyle)style).clone();            mergedStyle.addLabelProperties((GTKStyle)labelStyle);            _mergedStyleMap.put(_labelStyleList, mergedStyle);            _labelStyleList = new BakedArrayList(2);        }        return mergedStyle;    }    /**     * Returns the super class of a klass as returned from     * <code>getClass</code>, or null if <code>klass</code> has no     * super classes.     */    private Object getSuperclass(Object klass) {        return gtkSuperclass((String)klass);    }    /**     * Returns the name of a class returned from <code>getClass</code>.     */    private String getClassName(Object klass) {        return (String)klass;    }    /**     * Returns the name of the Region.     */    private String getName(Component c, Region region) {        if (region == Region.ROOT_PANE && c != null) {            String name = getRootPaneParentType(c);            if (name != null) {                return name;            }        }        return gtkClassFor(region);    }    private String getRootPaneParentType(Component c) {        Component parent = c.getParent();        if (parent instanceof Frame) {            return "GtkWindow";        }        else if (parent instanceof Dialog) {            return "GtkDialog";        }        else if (parent instanceof Window) {            return "GtkWindow";        }        else if (parent instanceof JInternalFrame) {            return "GtkFrame";        }        return null;    }    private GTKStyle getToolTipStyle() {        if (_tooltipStyle == null) {            // PENDING: this should have a priority of gtk, but we don't            // yet supported priority.            Color[] ttColors = new Color[GTKColorType.MAX_COUNT];            ttColors[GTKColorType.BACKGROUND.getID()] = new ColorUIResource(                0xFFFFC0);            ttColors[GTKColorType.FOREGROUND.getID()] =  new ColorUIResource(                0x000000);            _tooltipStyle = new GTKStyle(new GTKStyle.GTKStateInfo[] {                new GTKStyle.GTKStateInfo(SynthConstants.ENABLED, null, null,                null, ttColors, null)}, null, null,                GTKStyle.UNDEFINED_THICKNESS, GTKStyle.UNDEFINED_THICKNESS,                null);        }        return _tooltipStyle;    }    /**     * A specialized ArrayList that caches its hashCode as well as overriding     * equals to avoid creating an Iterator.     */    private static class BakedArrayList extends ArrayList {        private int hashCode;        BakedArrayList(int size) {            super(size);        }        BakedArrayList(java.util.List data) {            this(data.size());            for (int counter = 0, max = data.size(); counter < max; counter++){                add(data.get(counter));            }            bake();        }        void bake() {            hashCode = 1;            for (int counter = size() - 1; counter >= 0; counter--) {                hashCode = 31 * hashCode + get(counter).hashCode();            }        }        public int hashCode() {            return hashCode;        }        public boolean equals(Object o) {            BakedArrayList list = (BakedArrayList)o;            int size = size();            if (list.size() != size) {                return false;            }            while (size-- > 0) {                if (!get(size).equals(list.get(size))) {                    return false;                }            }            return true;        }    }    /**     * StyleAssociation is used to lookup a style for a particular     * component (or region).     */    private static class StyleAssociation {        /**         * The style         */        private SynthStyle _style;        /**         * Pattern used for matching.         */        private Pattern _pattern;        /**         * Matcher used for testing if path matches that of pattern.         */        private Matcher _matcher;        /**         * Returns a StyleAssociation that can be used to determine if         * a particular string matches the returned association.         */        public static StyleAssociation createStyleAssociation(                           String text, SynthStyle style)                           throws PatternSyntaxException {            return new StyleAssociation(text, style);        }        StyleAssociation(String text, SynthStyle style)                            throws PatternSyntaxException {            _style = style;            _pattern = Pattern.compile(text);        }        // NOTE: Matcher is not thread safe, it is assumed this will not be        // called from multiple threads.        public boolean matches(CharSequence path) {            if (_matcher == null) {                _matcher = _pattern.matcher(path);            }            else {                _matcher.reset(path);            }            return _matcher.matches();        }        /**         * Returns the text used in matching the string.         */        public String getText() {            return _pattern.pattern();        }        /**         * Returns the style this association is mapped to.         */        public SynthStyle getStyle() {            return _style;        }    }}

⌨️ 快捷键说明

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