nimbusstyle.java

来自「Mobile 应用程序使用 Java Micro Edition (Java M」· Java 代码 · 共 1,243 行 · 第 1/4 页

JAVA
1,243
字号
                    in.bottom *= MINI_SCALE;                    in.top *= MINI_SCALE;                    in.left *= MINI_SCALE;                    in.right *= MINI_SCALE;                }            }            return in;        }    }    /**     * @InheritDoc     *     * <p>Overridden to cause this style to populate itself with data from     * UIDefaults, if necessary.</p>     *     * <p>In addition, NimbusStyle handles ColorTypes slightly differently from     * Synth.</p>     * <ul>     *  <li>ColorType.BACKGROUND will equate to the color stored in UIDefaults     *      named "background".</li>     *  <li>ColorType.TEXT_BACKGROUND will equate to the color stored in     *      UIDefaults named "textBackground".</li>     *  <li>ColorType.FOREGROUND will equate to the color stored in UIDefaults     *      named "textForeground".</li>     *  <li>ColorType.TEXT_FOREGROUND will equate to the color stored in     *      UIDefaults named "textForeground".</li>     * </ul>     */    @Override protected Color getColorForState(SynthContext ctx, ColorType type) {        String key = null;        if (type == ColorType.BACKGROUND) {            key = "background";        } else if (type == ColorType.FOREGROUND) {            //map FOREGROUND as TEXT_FOREGROUND            key = "textForeground";        } else if (type == ColorType.TEXT_BACKGROUND) {            key = "textBackground";        } else if (type == ColorType.TEXT_FOREGROUND) {            key = "textForeground";        } else if (type == ColorType.FOCUS) {            key = "focus";        } else if (type != null) {            key = type.toString();        } else {            return DEFAULT_COLOR;        }        Color c = (Color) get(ctx, key);        //if all else fails, return a default color (which is a ColorUIResource)        if (c == null) c = DEFAULT_COLOR;        return c;    }    /**     * @InheritDoc     *     * Overridden to cause this style to populate itself with data from     * UIDefaults, if necessary. If a value named "font" is not found in     * UIDefaults, then the "defaultFont" font in UIDefaults will be returned     * instead.     */    @Override protected Font getFontForState(SynthContext ctx) {        Font f = (Font)get(ctx, "font");        if (f == null) f = UIManager.getFont("defaultFont");        // Account for scale        // The key "JComponent.sizeVariant" is used to match Apple's LAF        String scaleKey = (String)ctx.getComponent().getClientProperty(                "JComponent.sizeVariant");        if (scaleKey != null){            if (LARGE_KEY.equals(scaleKey)){                f = f.deriveFont(Math.round(f.getSize2D()*LARGE_SCALE));            } else if (SMALL_KEY.equals(scaleKey)){                f = f.deriveFont(Math.round(f.getSize2D()*SMALL_SCALE));            } else if (MINI_KEY.equals(scaleKey)){                f = f.deriveFont(Math.round(f.getSize2D()*MINI_SCALE));            }        }        return f;    }    /**     * @InheritDoc     *      * Returns the SynthPainter for this style, which ends up delegating to     * the Painters installed in this style.     */    @Override public SynthPainter getPainter(SynthContext ctx) {        return painter;    }    /**     * @InheritDoc     *     * Overridden to cause this style to populate itself with data from     * UIDefaults, if necessary. If opacity is not specified in UI defaults,     * then it defaults to being non-opaque.     */    @Override public boolean isOpaque(SynthContext ctx) {        // Force Table CellRenderers to be opaque        if ("Table.cellRenderer".equals(ctx.getComponent().getName())) {            return true;        }        Boolean opaque = (Boolean)get(ctx, "opaque");        return opaque == null ? false : opaque;    }    /**     * @InheritDoc     *     * <p>Overridden to cause this style to populate itself with data from     * UIDefaults, if necessary.</p>     *     * <p>Properties in UIDefaults may be specified in a chained manner. For     * example:     * <pre>     * background     * Button.opacity     * Button.Enabled.foreground     * Button.Enabled+Selected.background     * </pre></p>     *     * <p>In this example, suppose you were in the Enabled+Selected state and     * searched for "foreground". In this case, we first check for     * Button.Enabled+Selected.foreground, but no such color exists. We then     * fall back to the next valid state, in this case,     * Button.Enabled.foreground, and have a match. So we return it.</p>     *     * <p>Again, if we were in the state Enabled and looked for "background", we     * wouldn't find it in Button.Enabled, or in Button, but would at the top     * level in UIManager. So we return that value.</p>     *     * <p>One special note: the "key" passed to this method could be of the form     * "background" or "Button.background" where "Button" equals the prefix     * passed to the NimbusStyle constructor. In either case, it looks for     * "background".</p>     *      * @param ctx     * @param key must not be null     */    @Override public Object get(SynthContext ctx, Object key) {        Values v = getValues(ctx);        // strip off the prefix, if there is one.        String fullKey = key.toString();        String partialKey = fullKey.substring(fullKey.indexOf(".") + 1);        Object obj = null;        int xstate = getExtendedState(ctx, v);                // check the cache        tmpKey.init(partialKey, xstate);        obj = v.cache.get(tmpKey);        boolean wasInCache = obj != null;        if (!wasInCache){            // Search exact matching states and then lesser matching states            RuntimeState s = null;            int[] lastIndex = new int[] {-1};            while (obj == null &&                    (s = getNextState(v.states, lastIndex, xstate)) != null) {                obj = s.defaults.get(partialKey);            }            // Search Region Defaults            if (obj == null && v.defaults != null) {                obj = v.defaults.get(partialKey);            }            // return found object            // Search UIManager Defaults            if (obj == null) obj = UIManager.get(fullKey);            // Search Synth Defaults for InputMaps            if (obj == null && partialKey.equals("focusInputMap")) {                obj = super.get(ctx, fullKey);            }            // if all we got was a null, store this fact for later use            v.cache.put(new CacheKey(partialKey, xstate),                    obj == null ? NULL : obj);        }        // return found object        return obj == NULL ? null : obj;    }    /**     * Gets the appropriate background Painter, if there is one, for the state     * specified in the given SynthContext. This method does appropriate     * fallback searching, as described in #get.     *     * @param ctx The SynthContext. Must not be null.     * @return The background painter associated for the given state, or null if     * none could be found.     */    public Painter getBackgroundPainter(SynthContext ctx) {        Values v = getValues(ctx);        int xstate = getExtendedState(ctx, v);        Painter p = null;        // check the cache        tmpKey.init("backgroundPainter$$instance", xstate);        p = (Painter)v.cache.get(tmpKey);        if (p != null) return p;        // not in cache, so lookup and store in cache        RuntimeState s = null;        int[] lastIndex = new int[] {-1};        while ((s = getNextState(v.states, lastIndex, xstate)) != null) {            if (s.backgroundPainter != null) {                p = s.backgroundPainter;                break;            }        }        if (p == null) p = (Painter)get(ctx, "backgroundPainter");        if (p != null) {            v.cache.put(new CacheKey("backgroundPainter$$instance", xstate), p);        }        return p;    }    /**     * Gets the appropriate foreground Painter, if there is one, for the state     * specified in the given SynthContext. This method does appropriate     * fallback searching, as described in #get.     *     * @param ctx The SynthContext. Must not be null.     * @return The foreground painter associated for the given state, or null if     * none could be found.     */    public Painter getForegroundPainter(SynthContext ctx) {        Values v = getValues(ctx);        int xstate = getExtendedState(ctx, v);        Painter p = null;        // check the cache        tmpKey.init("foregroundPainter$$instance", xstate);        p = (Painter)v.cache.get(tmpKey);        if (p != null) return p;        // not in cache, so lookup and store in cache        RuntimeState s = null;        int[] lastIndex = new int[] {-1};        while ((s = getNextState(v.states, lastIndex, xstate)) != null) {            if (s.foregroundPainter != null) {                p = s.foregroundPainter;                break;            }        }        if (p == null) p = (Painter)get(ctx, "foregroundPainter");        if (p != null) {            v.cache.put(new CacheKey("foregroundPainter$$instance", xstate), p);        }        return p;    }    /**     * Gets the appropriate border Painter, if there is one, for the state     * specified in the given SynthContext. This method does appropriate     * fallback searching, as described in #get.     *     * @param ctx The SynthContext. Must not be null.     * @return The border painter associated for the given state, or null if     * none could be found.     */    public Painter getBorderPainter(SynthContext ctx) {        Values v = getValues(ctx);        int xstate = getExtendedState(ctx, v);        Painter p = null;        // check the cache        tmpKey.init("borderPainter$$instance", xstate);        p = (Painter)v.cache.get(tmpKey);        if (p != null) return p;        // not in cache, so lookup and store in cache        RuntimeState s = null;        int[] lastIndex = new int[] {-1};        while ((s = getNextState(v.states, lastIndex, xstate)) != null) {            if (s.borderPainter != null) {                p = s.borderPainter;                break;            }        }        if (p == null) p = (Painter)get(ctx, "borderPainter");        if (p != null) {            v.cache.put(new CacheKey("borderPainter$$instance", xstate), p);        }        return p;    }    /**     * Utility method which returns the proper Values based on the given     * SynthContext. Ensures that parsing of the values has occurred, or     * reoccurs as necessary.     *      * @param ctx The SynthContext     * @return a non-null values reference     */    private Values getValues(SynthContext ctx) {        validate();        return values;    }        /**     * Simple utility method that searchs the given array of Strings for the     * given string. This method is only called from getExtendedState if     * the developer has specified a specific state for the component to be     * in (ie, has "wedged" the component in that state) by specifying     * they client property "Nimbus.State".     *      * @param names a non-null array of strings     * @param name the name to look for in the array     * @return true or false based on whether the given name is in the array     */

⌨️ 快捷键说明

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