Java tutorial
/* * JGrass - Free Open Source Java GIS http://www.jgrass.org * (C) HydroloGIS - www.hydrologis.com * * This program 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 3 of the License, or * (at your option) any later version. * * This program 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 Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package eu.hydrologis.jgrass.database.utils; import java.util.HashMap; import java.util.Set; import java.util.Map.Entry; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.plugin.AbstractUIPlugin; import eu.hydrologis.jgrass.database.DatabasePlugin; /** * A singleton cache for images. * * @author Andrea Antonello (www.hydrologis.com) */ @SuppressWarnings("nls") public class ImageCache { public static final String ADD_LOCAL_DB = "icons/add_local_db.gif"; public static final String ADD_REMOTE_DB = "icons/add_remote_db.gif"; public static final String DATABASE_VIEW = "icons/database_view.gif"; public static final String EXPORT_LOCAL_DB = "icons/export_local_db.gif"; public static final String IMPORT_LOCAL_DB = "icons/import_local_db.gif"; public static final String LOCAL_DB = "icons/local_db.gif"; public static final String LOCAL_DB_ACTIVE = "icons/local_db_active.gif"; public static final String REMOTE_DB = "icons/remote_db.gif"; public static final String REMOTE_DB_ACTIVE = "icons/remote_db_active.gif"; public static final String REMOVE_DB = "icons/remove_db.gif"; private static ImageCache imageCache; private HashMap<String, Image> imageMap = new HashMap<String, Image>(); private ImageCache() { } public static ImageCache getInstance() { if (imageCache == null) { imageCache = new ImageCache(); } return imageCache; } /** * Get an image for a certain key. * * <p><b>The only keys to be used are the static strings in this class!!</b></p> * * @param key a file key, as for example {@link ImageCache#DATABASE_VIEW}. * @return the image. */ public Image getImage(String key) { Image image = imageMap.get(key); if (image == null) { image = createImage(key); imageMap.put(key, image); } return image; } private Image createImage(String key) { ImageDescriptor id = AbstractUIPlugin.imageDescriptorFromPlugin(DatabasePlugin.PLUGIN_ID, key); Image image = id.createImage(); return image; } /** * Disposes the images and clears the internal map. */ public void dispose() { Set<Entry<String, Image>> entrySet = imageMap.entrySet(); for (Entry<String, Image> entry : entrySet) { entry.getValue().dispose(); } imageMap.clear(); } }