001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.location.internal;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import net.refractions.udig.location.LocationUIPlugin;
023:
024: import org.eclipse.jface.resource.ImageDescriptor;
025: import org.eclipse.jface.resource.ImageRegistry;
026: import org.eclipse.swt.graphics.Image;
027:
028: /**
029: * The image descrptors for the plugin
030: */
031: public class Images {
032: public final static String PATH_ELOCALTOOL = "elcl16/"; //$NON-NLS-1$
033: public final static String REFRESH_CO = PATH_ELOCALTOOL
034: + "refresh_co.gif"; //$NON-NLS-1$
035: public final static String REMOVE_CO = PATH_ELOCALTOOL
036: + "remove_co.gif"; //$NON-NLS-1$
037:
038: public final static String PATH_OBJECT = "obj16/"; //$NON-NLS-1$
039: // later need address icon!
040: public static final String SHOW_CO = PATH_ELOCALTOOL
041: + "show_co.gif"; //$NON-NLS-1$
042:
043: /** Hashtable of ImageDescriptors */
044: private ImageRegistry imageCache;
045: private URL baseURL;
046:
047: /**
048: * Creates an image descriptor for later use.
049: */
050: synchronized ImageDescriptor create(String id) {
051: URL url = null;
052: try {
053: url = new URL(baseURL, id);
054: } catch (MalformedURLException e) {
055: return null;
056: }
057: ImageDescriptor image = ImageDescriptor.createFromURL(url);
058: imageCache.put(id, image);
059: return image;
060: }
061:
062: /**
063: * Returns the image descriptor for ID, or null if not found.
064: * <p>
065: * Images are from CatalogUIPlugin.getDefault().getImages()
066: * </p>
067: * @param id the key
068: * @return ImageDescriptor, or null if there is no such image.
069: */
070: public static ImageDescriptor getDescriptor(String id) {
071: Images images = (Images) LocationUIPlugin.getDefault()
072: .getImages();
073: ImageDescriptor found = images.imageCache.getDescriptor(id);
074: if (found != null) {
075: return found;
076: }
077: return images.create(id);
078: }
079:
080: /**
081: * Returns the image associated with the given key,
082: * or <code>null</code> if none.
083: *
084: * @param id the key
085: * @return the image, or <code>null</code> if none
086: */
087: public Image get(String id) {
088: Images images = (Images) LocationUIPlugin.getDefault()
089: .getImages();
090: ImageDescriptor found = images.imageCache.getDescriptor(id);
091: if (found == null) {
092: images.create(id);
093: }
094: return images.imageCache.get(id);
095: }
096:
097: /**
098: * Initializes the table of images used in this plugin.
099: * <p>
100: * The Images from ISharedImages will be registered with
101: * CatalogUIPlugin.getDefault().getImageRegistry().
102: * @param url
103: * @param shared
104: */
105: public void initializeImages(URL url, ImageRegistry shared) {
106: imageCache = shared;
107: baseURL = url;
108: }
109:
110: /**
111: * Cleanup image cache.
112: */
113: public void cleanUp() {
114: imageCache = null; // Display shutdown will clear imageCache
115: }
116:
117: /*
118: * @see net.refractions.udig.catalog.ui.ISharedImages#getImageDescriptor(java.lang.String)
119: */
120: public ImageDescriptor getImageDescriptor(String id) {
121: return getDescriptor(id);
122: }
123: }
|