gtkstyle.java

来自「JAVA 所有包」· Java 代码 · 共 1,081 行 · 第 1/3 页

JAVA
1,081
字号
        if (stockIcon != null) {            return stockIcon;        }                // Is it another kind of value ?        if (key != "engine") {            // For backward compatability we'll fallback to the UIManager.            // We don't go to the UIManager for engine as the engine is GTK            // specific.            Object value = UIManager.get(key);            if (key == "Table.rowHeight") {                int focusLineWidth = getClassSpecificIntValue(context,                         "focus-line-width", 0);                if (value == null && focusLineWidth > 0) {                    value = new Integer(16 + 2 * focusLineWidth);                }            }            return value;        }                // Don't call super, we don't want to pick up defaults from        // SynthStyle.        return null;    }    /**     * Returns true if the style should fill in the background of the     * specified context for the specified state.     */    boolean fillBackground(SynthContext context, int state) {        return true;    }    /**     * Returns the Icon to fill the background in with for the specified     * context and state.     */    Image getBackgroundImage(SynthContext context, int state) {        return null;    }    private Icon getStockIcon(SynthContext context, String key, int type) {        Icon icon = null;        TextDirection direction = TextDirection.LTR;                if (context != null) {            ComponentOrientation co = context.getComponent().                                              getComponentOrientation();            if (co != null && !co.isLeftToRight()) {                direction = TextDirection.RTL;            }        }                icon = getStyleSpecificIcon(key, direction, type);        // Native GTK does resizing for us.        if (icon != null) {            return icon;        }                // Use a default icon        String propName = ICON_PROPERTY_PREFIX + key + '.' + type + '.' +                          (direction == TextDirection.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;         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();         }        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;    }    Icon getStyleSpecificIcon(String key, TextDirection direction, int type) {        return null;    }    static class GTKStockIconInfo {        private static Map<String,Integer> ICON_TYPE_MAP;        private static final Object ICON_SIZE_KEY = new StringBuffer("IconSize");        private static Dimension[] getIconSizesMap() {            AppContext appContext = AppContext.getAppContext();             Dimension[] iconSizes = (Dimension[])appContext.get(ICON_SIZE_KEY);             if (iconSizes == null) {                 iconSizes = new Dimension[7];                iconSizes[0] = null;                  // GTK_ICON_SIZE_INVALID                iconSizes[1] = new Dimension(16, 16); // GTK_ICON_SIZE_MENU                iconSizes[2] = new Dimension(18, 18); // GTK_ICON_SIZE_SMALL_TOOLBAR                iconSizes[3] = new Dimension(24, 24); // GTK_ICON_SIZE_LARGE_TOOLBAR                iconSizes[4] = new Dimension(20, 20); // GTK_ICON_SIZE_BUTTON                iconSizes[5] = new Dimension(32, 32); // GTK_ICON_SIZE_DND                iconSizes[6] = new Dimension(48, 48); // GTK_ICON_SIZE_DIALOG                appContext.put(ICON_SIZE_KEY, iconSizes);            }            return iconSizes;        }        /**          * Return the size of a particular icon type (logical size)         *          * @param type icon type (GtkIconSize value)         * @return a Dimension object, or null if lsize is invalid         */        public static Dimension getIconSize(int type) {            Dimension[] iconSizes = getIconSizesMap();            return type >= 0 && type < iconSizes.length ?                iconSizes[type] : null;          }        /**         * Change icon size in a type to size mapping. This is called by code         * that parses the gtk-icon-sizes setting         *           * @param type icon type (GtkIconSize value)         * @param w the new icon width         * @param h the new icon height         */         public static void setIconSize(int type, int w, int h) {            Dimension[] iconSizes = getIconSizesMap();            if (type >= 0 && type < iconSizes.length) {                iconSizes[type] = new Dimension(w, h);            }        }        /**         * Return icon type (GtkIconSize value) given a symbolic name which can         * occur in a theme file.         *          * @param size symbolic name, e.g. gtk-button         * @return icon type. Valid types are 1 to 6          */         public static int getIconType(String size) {            if (size == null) {                return UNDEFINED;            }            if (ICON_TYPE_MAP == null) {                initIconTypeMap();            }            Integer n = ICON_TYPE_MAP.get(size);            return n != null ? n.intValue() : UNDEFINED;        }        private static void initIconTypeMap() {            ICON_TYPE_MAP = new HashMap<String,Integer>();            ICON_TYPE_MAP.put("gtk-menu", new Integer(1));            ICON_TYPE_MAP.put("gtk-small-toolbar", new Integer(2));            ICON_TYPE_MAP.put("gtk-large-toolbar", new Integer(3));            ICON_TYPE_MAP.put("gtk-button", new Integer(4));            ICON_TYPE_MAP.put("gtk-dnd", new Integer(5));            ICON_TYPE_MAP.put("gtk-dialog", new Integer(6));        }            }        /**     * An Icon that is fetched using getStockIcon.     */    private static class GTKStockIcon extends SynthIcon {        private String key;        private int size;        private boolean loadedLTR;        private boolean loadedRTL;        private Icon ltrIcon;        private Icon rtlIcon;        private SynthStyle style;        GTKStockIcon(String key, int size) {            this.key = key;            this.size = size;        }                public void paintIcon(SynthContext context, Graphics g, int x,                              int y, int w, int h) {            Icon icon = getIcon(context);            if (icon != null) {                if (context == null) {                    icon.paintIcon(null, g, x, y);                }                else {                    icon.paintIcon(context.getComponent(), g, x, y);                }            }        }        public int getIconWidth(SynthContext context) {            Icon icon = getIcon(context);            if (icon != null) {                return icon.getIconWidth();            }            return 0;        }        public int getIconHeight(SynthContext context) {            Icon icon = getIcon(context);            if (icon != null) {                return icon.getIconHeight();            }            return 0;        }        private Icon getIcon(SynthContext context) {            if (context != null) {                ComponentOrientation co = context.getComponent().                                                  getComponentOrientation();                SynthStyle style = context.getStyle();                if (style != this.style) {                    this.style = style;                    loadedLTR = loadedRTL = false;                }                if (co == null || co.isLeftToRight()) {                    if (!loadedLTR) {                        loadedLTR = true;                        ltrIcon = ((GTKStyle)context.getStyle()).                                  getStockIcon(context, key, size);                    }                    return ltrIcon;                }                else if (!loadedRTL) {                    loadedRTL = true;                    rtlIcon = ((GTKStyle)context.getStyle()).                              getStockIcon(context, key,size);                }                return rtlIcon;            }            return ltrIcon;        }    }    /**     * GTKLazyValue is a slimmed down version of <code>ProxyLaxyValue</code>.     * The code is duplicate so that it can get at the package private     * classes in gtk.     */    static class GTKLazyValue implements UIDefaults.LazyValue {        /**         * Name of the class to create.         */        private String className;        private String methodName;        GTKLazyValue(String name) {            this(name, null);        }        GTKLazyValue(String name, String methodName) {            this.className = name;            this.methodName = methodName;        }        public Object createValue(UIDefaults table) {            try {                Class c = Class.forName(className, true,Thread.currentThread().                                        getContextClassLoader());                if (methodName == null) {                    return c.newInstance();                }                Method m = c.getMethod(methodName, (Class[])null);                return m.invoke(c, (Object[])null);            } catch (ClassNotFoundException cnfe) {            } catch (IllegalAccessException iae) {            } catch (InvocationTargetException ite) {            } catch (NoSuchMethodException nsme) {            } catch (InstantiationException ie) {            }            return null;        }    }    static {        CLASS_SPECIFIC_MAP = new HashMap<String,String>();        CLASS_SPECIFIC_MAP.put("Slider.thumbHeight", "slider-width");        CLASS_SPECIFIC_MAP.put("Slider.trackBorder", "trough-border");        CLASS_SPECIFIC_MAP.put("SplitPane.size", "handle-size");        CLASS_SPECIFIC_MAP.put("Tree.expanderSize", "expander-size");        CLASS_SPECIFIC_MAP.put("ScrollBar.thumbHeight", "slider-width");        CLASS_SPECIFIC_MAP.put("TextArea.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("TextArea.caretAspectRatio", "cursor-aspect-ratio");        CLASS_SPECIFIC_MAP.put("TextField.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("TextField.caretAspectRatio", "cursor-aspect-ratio");        CLASS_SPECIFIC_MAP.put("PasswordField.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("PasswordField.caretAspectRatio", "cursor-aspect-ratio");        CLASS_SPECIFIC_MAP.put("FormattedTextField.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("FormattedTextField.caretAspectRatio", "cursor-aspect-");        CLASS_SPECIFIC_MAP.put("TextPane.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("TextPane.caretAspectRatio", "cursor-aspect-ratio");        CLASS_SPECIFIC_MAP.put("EditorPane.caretForeground", "cursor-color");        CLASS_SPECIFIC_MAP.put("EditorPane.caretAspectRatio", "cursor-aspect-ratio");                ICONS_MAP = new HashMap<String, GTKStockIcon>();        ICONS_MAP.put("FileChooser.cancelIcon", new GTKStockIcon("gtk-cancel", 4));        ICONS_MAP.put("FileChooser.okIcon",     new GTKStockIcon("gtk-ok",     4));        ICONS_MAP.put("OptionPane.errorIcon", new GTKStockIcon("gtk-dialog-error", 6));        ICONS_MAP.put("OptionPane.informationIcon", new GTKStockIcon("gtk-dialog-info", 6));        ICONS_MAP.put("OptionPane.warningIcon", new GTKStockIcon("gtk-dialog-warning", 6));        ICONS_MAP.put("OptionPane.questionIcon", new GTKStockIcon("gtk-dialog-question", 6));        ICONS_MAP.put("OptionPane.yesIcon", new GTKStockIcon("gtk-yes", 4));        ICONS_MAP.put("OptionPane.noIcon", new GTKStockIcon("gtk-no", 4));        ICONS_MAP.put("OptionPane.cancelIcon", new GTKStockIcon("gtk-cancel", 4));        ICONS_MAP.put("OptionPane.okIcon", new GTKStockIcon("gtk-ok", 4));    }}

⌨️ 快捷键说明

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