⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 resourcemanager.java

📁 Eclipse从入门到精通源代码/第二篇 SWT_JFace篇(6-16章)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     */
    public static Image decorateImage(Image baseImage, Image decorator) {
        HashMap decoratedMap = (HashMap) m_ImageToDecoratorMap.get(baseImage);
        if (decoratedMap == null) {
            decoratedMap = new HashMap();
            m_ImageToDecoratorMap.put(baseImage, decoratedMap);
        }
        Image result = (Image) decoratedMap.get(decorator);
        if (result == null) {
            ImageData bid = baseImage.getImageData();
            ImageData did = decorator.getImageData();
            result = new Image(Display.getCurrent(), bid.width, bid.height);
            GC gc = new GC(result);
            //
            gc.drawImage(baseImage, 0, 0);
            gc.drawImage(decorator, bid.width - did.width - 1, bid.height - did.height - 1);
            //
            gc.dispose();
            decoratedMap.put(decorator, result);
        }
        return result;
    }

    /**
     * Dispose all of the cached images
     */
    public static void disposeImages() {
        for (Iterator iter = m_ClassImageMap.values().iterator(); iter.hasNext();)
            ((Image) iter.next()).dispose();
        m_ClassImageMap.clear();
        for (Iterator iter = m_DescriptorImageMap.values().iterator(); iter.hasNext();)
            ((Image) iter.next()).dispose();
        m_DescriptorImageMap.clear();
    }

    //////////////////////////////
    // Plugin images support
    //////////////////////////////

    /**
     * Maps URL to images
     */
    private static HashMap m_URLImageMap = new HashMap();

    /**
     * Retuns an image based on a plugin and file path
     * 
     * @param plugin
     *            Object The plugin containing the image
     * @param name
     *            String The path to th eimage within the plugin
     * @return Image The image stored in the file at the specified path
     */
    public static Image getPluginImage(Object plugin, String name) {
        try {
            try {
                URL url = getPluginImageURL(plugin, name);
                if (m_URLImageMap.containsKey(url))
                    return (Image) m_URLImageMap.get(url);
                InputStream is = url.openStream();
                Image image;
                try {
                    image = getImage(is);
                    m_URLImageMap.put(url, image);
                } finally {
                    is.close();
                }
                return image;
            } catch (Throwable e) {
            }
        } catch (Throwable e) {
        }
        return null;
    }

    /**
     * Retuns an image descriptor based on a plugin and file path
     * 
     * @param plugin
     *            Object The plugin containing the image
     * @param name
     *            String The path to th eimage within the plugin
     * @return ImageDescriptor The image descriptor stored in the file at the
     *         specified path
     */
    public static ImageDescriptor getPluginImageDescriptor(Object plugin, String name) {
        try {
            try {
                URL url = getPluginImageURL(plugin, name);
                return ImageDescriptor.createFromURL(url);
            } catch (Throwable e) {
            }
        } catch (Throwable e) {
        }
        return null;
    }

    /**
     * Retuns an URL based on a plugin and file path
     * 
     * @param plugin
     *            Object The plugin containing the file path
     * @param name
     *            String The file path
     * @return URL The URL representing the file at the specified path
     * @throws Exception
     */
    private static URL getPluginImageURL(Object plugin, String name) throws Exception {
        Class pluginClass = Class.forName("org.eclipse.core.runtime.Plugin");
        Method getDescriptorMethod = pluginClass.getMethod("getDescriptor", new Class[] {});
        Class pluginDescriptorClass = Class.forName("org.eclipse.core.runtime.IPluginDescriptor");
        Method getInstallURLMethod = pluginDescriptorClass.getMethod("getInstallURL", new Class[] {});
        //
        Object pluginDescriptor = getDescriptorMethod.invoke(plugin, new Object[] {});
        URL installURL = (URL) getInstallURLMethod.invoke(pluginDescriptor, new Object[] {});
        URL url = new URL(installURL, name);
        return url;
    }

    //////////////////////////////
    // Font support
    //////////////////////////////

    /**
     * Maps font names to fonts
     */
    private static HashMap m_FontMap = new HashMap();

    /**
     * Maps fonts to their bold versions
     */
    private static HashMap m_FontToBoldFontMap = new HashMap();

    /**
     * Returns a font based on its name, height and style
     * 
     * @param name
     *            String The name of the font
     * @param height
     *            int The height of the font
     * @param style
     *            int The style of the font
     * @return Font The font matching the name, height and style
     */
    public static Font getFont(String name, int height, int style) {
        String fullName = name + "|" + height + "|" + style;
        Font font = (Font) m_FontMap.get(fullName);
        if (font == null) {
            font = new Font(Display.getCurrent(), name, height, style);
            m_FontMap.put(fullName, font);
        }
        return font;
    }

    /**
     * Return a bold version of the give font
     * 
     * @param baseFont
     *            Font The font for whoch a bold version is desired
     * @return Font The bold version of the give font
     */
    public static Font getBoldFont(Font baseFont) {
        Font font = (Font) m_FontToBoldFontMap.get(baseFont);
        if (font == null) {
            FontData fontDatas[] = baseFont.getFontData();
            FontData data = fontDatas[0];
            font = new Font(Display.getCurrent(), data.getName(), data.getHeight(), SWT.BOLD);
            m_FontToBoldFontMap.put(baseFont, font);
        }
        return font;
    }

    /**
     * Dispose all of the cached fonts
     */
    public static void disposeFonts() {
        for (Iterator iter = m_FontMap.values().iterator(); iter.hasNext();)
            ((Font) iter.next()).dispose();
        m_FontMap.clear();
    }

    //////////////////////////////
    // CoolBar support
    //////////////////////////////

    /**
     * Fix the layout of the specified CoolBar
     * 
     * @param bar
     *            CoolBar The CoolBar that shgoud be fixed
     */
    public static void fixCoolBarSize(CoolBar bar) {
        CoolItem[] items = bar.getItems();
        // ensure that each item has control (at least empty one)
        for (int i = 0; i < items.length; i++) {
            CoolItem item = items[i];
            if (item.getControl() == null)
                item.setControl(new Canvas(bar, SWT.NONE) {
                    public Point computeSize(int wHint, int hHint, boolean changed) {
                        return new Point(20, 20);
                    }
                });
        }
        // compute size for each item
        for (int i = 0; i < items.length; i++) {
            CoolItem item = items[i];
            Control control = item.getControl();
            control.pack();
            Point size = control.getSize();
            item.setSize(item.computeSize(size.x, size.y));
        }
    }

    //////////////////////////////
    // Cursor support
    //////////////////////////////

    /**
     * Maps IDs to cursors
     */
    private static HashMap m_IdToCursorMap = new HashMap();

    /**
     * Returns the system cursor matching the specific ID
     * 
     * @param id
     *            int The ID value for the cursor
     * @return Cursor The system cursor matching the specific ID
     */
    public static Cursor getCursor(int id) {
        Integer key = new Integer(id);
        Cursor cursor = (Cursor) m_IdToCursorMap.get(key);
        if (cursor == null) {
            cursor = new Cursor(Display.getDefault(), id);
            m_IdToCursorMap.put(key, cursor);
        }
        return cursor;
    }

    /**
     * Dispose all of the cached cursors
     */
    public static void disposeCursors() {
        for (Iterator iter = m_IdToCursorMap.values().iterator(); iter.hasNext();)
            ((Cursor) iter.next()).dispose();
        m_IdToCursorMap.clear();
    }
}

⌨️ 快捷键说明

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