cc.warlock.scribe.ui.ScribeSharedImages.java Source code

Java tutorial

Introduction

Here is the source code for cc.warlock.scribe.ui.ScribeSharedImages.java

Source

/**
 * Warlock, the open-source cross-platform game client
 *  
 * Copyright 2008, Warlock LLC, and individual contributors as indicated
 * by the @authors tag. 
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package cc.warlock.scribe.ui;

import java.util.HashMap;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.osgi.framework.Bundle;

/**
 * @author Marshall
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ScribeSharedImages {

    public static final String IMG_SCRIPT = "/icons/script.gif";
    public static final String IMG_START = "/icons/run_exc.gif";
    public static final String IMG_RESUME = "/icons/resume_co.gif";
    public static final String IMG_SUSPEND = "/icons/suspend_co.gif";
    public static final String IMG_TERMINATE = "/icons/terminate_co.gif";

    private static ScribeSharedImages instance;

    private HashMap<String, Image> images;
    private HashMap<String, ImageDescriptor> descriptors;

    private ScribeSharedImages() {
        instance = this;
        images = new HashMap<String, Image>();
        descriptors = new HashMap<String, ImageDescriptor>();
    }

    private void addImage(String path) {
        Bundle pluginBundle = Activator.getDefault().getBundle();
        ImageDescriptor descriptor = ImageDescriptor.createFromURL(pluginBundle.getEntry(path));

        descriptors.put(path, descriptor);
        images.put(path, descriptor.createImage());
    }

    private static ScribeSharedImages instance() {
        if (instance == null)
            new ScribeSharedImages();

        return instance;
    }

    public static Image getImage(String key) {
        return instance().image(key);
    }

    public static ImageDescriptor getImageDescriptor(String key) {
        return instance().descriptor(key);
    }

    public Image image(String key) {
        if (!images.containsKey(key)) {
            addImage(key);
        }

        return (Image) images.get(key);
    }

    public ImageDescriptor descriptor(String key) {
        if (!descriptors.containsKey(key)) {
            addImage(key);
        }

        return (ImageDescriptor) descriptors.get(key);
    }

    protected void finalize() throws Throwable {
        for (Image image : images.values()) {
            image.dispose();
        }
        super.finalize();
    }
}