org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUIImages.java Source code

Java tutorial

Introduction

Here is the source code for org.springframework.ide.eclipse.beans.ui.livegraph.LiveGraphUIImages.java

Source

/*******************************************************************************
 * Copyright (c) 2016 Spring IDE Developers
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Spring IDE Developers - initial API and implementation
 *******************************************************************************/
package org.springframework.ide.eclipse.beans.ui.livegraph;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.graphics.Image;

public class LiveGraphUIImages {

    private static final String ICON_PATH_PREFIX = "icons/full/";
    private static final String NAME_PREFIX = LiveGraphUiPlugin.PLUGIN_ID + '.';
    private static final int NAME_PREFIX_LENGTH = NAME_PREFIX.length();

    /* Declare Common paths */
    private static URL ICON_BASE_URL = null;

    static {
        try {
            ICON_BASE_URL = new URL(LiveGraphUiPlugin.getDefault().getBundle().getEntry("/"), ICON_PATH_PREFIX);
        } catch (MalformedURLException e) {
            LiveGraphUiPlugin.log(e);
        }
    }

    /** A table of all the <code>ImageDescriptor</code>s. */
    private static Map<String, ImageDescriptor> imageDescriptors;

    /*
     * Available cached Images in the Spring Beans UI plugin image registry.
     */
    public static final String IMG_OBJS_BEAN = NAME_PREFIX + "bean_obj.gif";
    public static final String IMG_OBJS_BEAN_REF = NAME_PREFIX + "beanref_obj.gif";
    public static final String IMG_OBJS_CONFIG = NAME_PREFIX + "config_obj.gif";
    public static final String IMG_OBJS_COLLECTION = NAME_PREFIX + "collection_obj.gif";
    public static final String IMG_OBJS_REFRESH = NAME_PREFIX + "refresh_obj.gif";

    // Use IPath and toOSString to build the names to ensure they have the slashes correct
    private final static String OBJECT = "obj16/"; //basic colors - size 16x16

    public static final ImageDescriptor DESC_OBJS_BEAN_REF = createManaged(OBJECT, IMG_OBJS_BEAN_REF);
    public static final ImageDescriptor DESC_OBJS_COLLECTION = createManaged(OBJECT, IMG_OBJS_COLLECTION);
    public static final ImageDescriptor DESC_OBJS_BEAN = createManaged(OBJECT, IMG_OBJS_BEAN);
    public static final ImageDescriptor DESC_OBJS_CONFIG = createManaged(OBJECT, IMG_OBJS_CONFIG);
    public static final ImageDescriptor REFRESH = createManaged(OBJECT, IMG_OBJS_REFRESH);

    /*
     * Helper method to initialize the image registry from the BeansUIPlugin
     * class.
     */
    /* package */ static void initializeImageRegistry(ImageRegistry registry) {
        for (String key : imageDescriptors.keySet()) {
            registry.put(key, imageDescriptors.get(key));
        }
    }

    /**
     * Returns the {@link Image} identified by the given key,
     * or <code>null</code> if it does not exist.
     */
    public static Image getImage(String key) {
        return LiveGraphUiPlugin.getDefault().getImageRegistry().get(key);
    }

    private static ImageDescriptor createManaged(String prefix, String name) {
        try {
            ImageDescriptor result = ImageDescriptor
                    .createFromURL(makeIconFileURL(prefix, name.substring(NAME_PREFIX_LENGTH)));
            if (imageDescriptors == null) {
                imageDescriptors = new HashMap<String, ImageDescriptor>();
            }
            imageDescriptors.put(name, result);
            return result;
        } catch (MalformedURLException e) {
            LiveGraphUiPlugin.log(e);
            return ImageDescriptor.getMissingImageDescriptor();
        }
    }

    private static URL makeIconFileURL(String prefix, String name) throws MalformedURLException {
        if (ICON_BASE_URL == null) {
            throw new MalformedURLException();
        }

        StringBuffer buffer = new StringBuffer(prefix);
        buffer.append('/').append(name);
        return new URL(ICON_BASE_URL, buffer.toString());
    }
}