gtkstylefactory.java

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

JAVA
814
字号
    private SynthStyle _getStyle(JComponent c, Region id) {        BakedArrayList matches = _tmpList;        matches.clear();        getMatchingStyles(matches, c, id);        return getStyle(matches);    }    private SynthStyle getStyle(BakedArrayList matches) {        // Use a cached Style if possible, otherwise create a new one.        matches.cacheHashCode();        SynthStyle style = getCachedStyle(matches);        if (style == null) {            style = mergeStyles(matches);            if (style != null) {                cacheStyle(matches, style);            }        }        return style;    }    /**     * Fetches any styles that match the passed into arguments into     * <code>matches</code>.     */    private void getMatchingStyles(java.util.List matches, JComponent c,                                   Region id) {        // TableHeaderer.renderer is special cased as it descends from        // DefaultTableCellRenderer which does NOT pass along the property        // change that would trigger the style to be refetched.        if (c != null && (c.getParent() != null ||                          c.getName() == "TableHeader.renderer" ||                           c.getName() == "Slider.label") ||                          c.getName() == "ComboBox.list") {            // First match on WIDGET            if (_widgetStyles != null) {                getMatches(getPath(WIDGET, c, id), _widgetStyles, matches, c,                           id);            }            // Then match on WIDGET_CLASS            if (_widgetClassStyles != null) {                getMatches(getPath(WIDGET_CLASS, c, id), _widgetClassStyles,                           matches, c, id);            }            // Lastly match on CLASS            if (_classStyles != null) {                getClassMatches(matches, c, id);            }        }        if (id == Region.TOOL_TIP) {            matches.add(getToolTipStyle());        }        else if (id == Region.PROGRESS_BAR && GTKLookAndFeel.is2_2()) {            matches.add(getProgressBarStyle());        }        else if ((id == Region.MENU || id == Region.MENU_ITEM ||                  id == Region.POPUP_MENU_SEPARATOR ||                  id == Region.CHECK_BOX_MENU_ITEM ||                  id == Region.RADIO_BUTTON_MENU_ITEM ||                  id == Region.MENU_ITEM_ACCELERATOR) &&                 GTKLookAndFeel.is2_2()) {            matches.add(getMenuItemStyle());        }        matches.add(_defaultStyle);    }    private void getMatches(CharSequence path, java.util.List styles,                            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.indexOf(sa.getStyle()) == -1) {                matches.add(sa.getStyle());            }        }    }    private void getClassMatches(java.util.List matches, JComponent c,                                 Region id) {        getClassMatches(matches, getClass(c, id));    }    private void getClassMatches(java.util.List matches, Object gtkClassName){        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 = gtkClassName;            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.cacheHashCode();        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 getProgressBarStyle() {        if (_pbStyle == null) {            Color[] moColors = new Color[GTKColorType.MAX_COUNT];            Color[] normalColors = new Color[GTKColorType.MAX_COUNT];            moColors[GTKColorType.BACKGROUND.getID()] = new ColorUIResource(                0x4B6983);            normalColors[GTKColorType.BACKGROUND.getID()] =                   new ColorUIResource(0xBAB5AB);            _pbStyle = new GTKStyle(new GTKStyle.GTKStateInfo[]                { new GTKStyle.GTKStateInfo(SynthConstants.ENABLED,                                            null, normalColors, null),                  new GTKStyle.GTKStateInfo(SynthConstants.MOUSE_OVER,                                            null, moColors, null)                }, null, null, GTKStyle.UNDEFINED_THICKNESS,                GTKStyle.UNDEFINED_THICKNESS, null);        }        return _pbStyle;    }    private GTKStyle getMenuItemStyle() {        if (_menuItemStyle == null) {            Color[] moColors = new Color[GTKColorType.MAX_COUNT];            Color[] selectedColors = new Color[GTKColorType.MAX_COUNT];            moColors[GTKColorType.BACKGROUND.getID()] = new ColorUIResource(                0x9db8d2);            moColors[GTKColorType.FOREGROUND.getID()] = GTKStyle.WHITE_COLOR;            moColors[GTKColorType.TEXT_FOREGROUND.getID()] =                                  new ColorUIResource(0xFFFFFF);            selectedColors[GTKColorType.TEXT_FOREGROUND.getID()] =                   new ColorUIResource(0xFFFFFF);            _menuItemStyle = new GTKStyle(new GTKStyle.GTKStateInfo[]                {                   new GTKStyle.GTKStateInfo(SynthConstants.MOUSE_OVER,                                            null, moColors, null),                  new GTKStyle.GTKStateInfo(SynthConstants.SELECTED,                                            null, selectedColors, null),                }, null, null, GTKStyle.UNDEFINED_THICKNESS,                GTKStyle.UNDEFINED_THICKNESS, null);        }        return _menuItemStyle;    }    private GTKStyle getToolTipStyle() {        if (_tooltipStyle == null) {            Color[] ttColors = new Color[GTKColorType.MAX_COUNT];            if (GTKLookAndFeel.is2_2()) {                ttColors[GTKColorType.BACKGROUND.getID()] =                                 new ColorUIResource(0xEEE1B3);                ttColors[GTKColorType.FOREGROUND.getID()] =                                  new ColorUIResource(0x000000);            }            else {                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, ttColors, null)}, null, null,                GTKStyle.UNDEFINED_THICKNESS, GTKStyle.UNDEFINED_THICKNESS,                null);        }        return _tooltipStyle;    }}

⌨️ 快捷键说明

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