com.feup.contribution.druid.view.SharedImages.java Source code

Java tutorial

Introduction

Here is the source code for com.feup.contribution.druid.view.SharedImages.java

Source

/*
 * This file is part of drUID.
 * 
 * drUID is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * drUID 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 General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with drUID.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.feup.contribution.druid.view;

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.swt.graphics.Image;

import com.feup.contribution.druid.DruidPlugin;

public class SharedImages {
    private static Map<ImageDescriptor, Image> imageCache = new HashMap<ImageDescriptor, Image>(11);

    public static Image getImage(String name) {
        ImageDescriptor descriptor = getImageDescriptor(name);
        Image image = (Image) imageCache.get(descriptor);
        if (image == null) {
            image = descriptor.createImage();
            imageCache.put(descriptor, image);
        }
        return image;
    }

    @SuppressWarnings("deprecation")
    public static ImageDescriptor getImageDescriptor(String name) {
        String iconPath = "icons/";
        try {
            URL installURL = DruidPlugin.getPlugin().getDescriptor().getInstallURL();
            URL url = new URL(installURL, iconPath + name);
            return ImageDescriptor.createFromURL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return ImageDescriptor.getMissingImageDescriptor();
        }
    }
}