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.catalog.internal.ui;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import net.refractions.udig.catalog.ui.CatalogUIPlugin;
023: import net.refractions.udig.catalog.ui.ISharedImages;
024:
025: import org.eclipse.jface.resource.ImageDescriptor;
026: import org.eclipse.jface.resource.ImageRegistry;
027: import org.eclipse.swt.graphics.Image;
028:
029: /**
030: * The image descrptors for the plugin
031: */
032: public class Images implements ISharedImages {
033:
034: /** Hashtable of ImageDescriptors */
035: private ImageRegistry imageCache;
036: private URL baseURL;
037:
038: /**
039: * Creates an image descriptor for later use.
040: */
041: synchronized ImageDescriptor create(String id) {
042: URL url = null;
043: try {
044: url = new URL(baseURL, id);
045: } catch (MalformedURLException e) {
046: return null;
047: }
048: ImageDescriptor image = ImageDescriptor.createFromURL(url);
049: imageCache.put(id, image);
050: return image;
051: }
052:
053: /**
054: * Returns the image descriptor for ID, or null if not found.
055: * <p>
056: * Images are from CatalogUIPlugin.getDefault().getImages()
057: * </p>
058: *
059: * @param id the key
060: * @return ImageDescriptor, or null if there is no such image.
061: */
062: public static ImageDescriptor getDescriptor(String id) {
063: Images images = (Images) CatalogUIPlugin.getDefault()
064: .getImages();
065: ImageDescriptor found = images.imageCache.getDescriptor(id);
066: if (found != null) {
067: return found;
068: }
069: return images.create(id);
070: }
071:
072: /**
073: * Returns the image associated with the given key, or <code>null</code> if none.
074: *
075: * @param id the key
076: * @return the image, or <code>null</code> if none
077: */
078: public Image get(String id) {
079: Images images = (Images) CatalogUIPlugin.getDefault()
080: .getImages();
081: ImageDescriptor found = images.imageCache.getDescriptor(id);
082: if (found == null) {
083: images.create(id);
084: }
085: return images.imageCache.get(id);
086: }
087:
088: /**
089: * Initializes the table of images used in this plugin.
090: * <p>
091: * The Images from ISharedImages will be registered with
092: * CatalogUIPlugin.getDefault().getImageRegistry().
093: *
094: * @param url
095: * @param shared
096: */
097: public synchronized void initializeImages(URL url,
098: ImageRegistry shared) {
099: imageCache = shared;
100: baseURL = url;
101:
102: // objects
103: create(ImageConstants.ADD_CO);
104: // create( ImageConstants.PATH_ETOOL );
105: // create( ImageConstants.PATH_OBJECT );
106: // create( ImageConstants.PATH_ELOCALTOOL );
107: create(ImageConstants.DISCOVERY_WIZ);
108: // create( ImageConstants.PATH_WIZBAN);
109: create(ImageConstants.GRID_OBJ);
110: create(ImageConstants.ADD_CO);
111: create(ImageConstants.REFRESH_CO);
112: create(ImageConstants.REMOVE_CO);
113:
114: // obj
115: create(ImageConstants.FEATURE_FILE_OBJ);
116: create(ImageConstants.FEATURE_OBJ);
117: create(ImageConstants.REPOSITORY_OBJ);
118: create(ImageConstants.MEMORY_OBJ);
119: create(ISharedImages.GCE_OBJ);
120: create(ISharedImages.DATABASE_OBJ);
121: create(ISharedImages.DATASTORE_OBJ);
122: create(ISharedImages.SERVER_OBJ);
123: create(ISharedImages.WFS_OBJ);
124: create(ISharedImages.WMS_OBJ);
125: }
126:
127: /**
128: * Cleanup image cache.
129: */
130: public void cleanUp() {
131: imageCache = null; // Display shutdown will clear imageCache
132: }
133:
134: /*
135: * @see net.refractions.udig.catalog.ui.ISharedImages#getImageDescriptor(java.lang.String)
136: */
137: public ImageDescriptor getImageDescriptor(String id) {
138: return getDescriptor(id);
139: }
140: }
|