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.style;
018:
019: import java.net.MalformedURLException;
020: import java.net.URL;
021:
022: import net.refractions.udig.style.ui.ImageConstants;
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 descriptors for the plugin
030: */
031: public class Images {
032:
033: /** Hashtable of ImageDescriptors */
034: private ImageRegistry imageCache;
035: private URL baseURL;
036:
037: /**
038: * Creates an image descriptor for later use.
039: */
040: synchronized ImageDescriptor create(String id) {
041: URL url = null;
042: try {
043: url = new URL(baseURL, id);
044: } catch (MalformedURLException e) {
045: return null;
046: }
047: ImageDescriptor image = ImageDescriptor.createFromURL(url);
048: imageCache.put(id, image);
049: return image;
050: }
051:
052: /**
053: * Returns the image descriptor for ID, or null if not found.
054: * <p>
055: * Images are from CatalogUIPlugin.getDefault().getImages()
056: * </p>
057: * @param id
058: * @return ImageDescriptor, or null if there is no such image.
059: */
060: public static ImageDescriptor getDescriptor(String id) {
061: Images images = StylePlugin.getDefault().getImages();
062: ImageDescriptor found = images.imageCache.getDescriptor(id);
063: if (found != null) {
064: return found;
065: }
066: return images.create(id);
067: }
068:
069: /**
070: * Initializes the table of images used in this plugin.
071: * <p>
072: * The Images from ISharedImages will be registered with
073: * CatalogUIPlugin.getDefault().getImageRegistry().
074: * @param url
075: * @param shared
076: */
077: public synchronized void initializeImages(URL url,
078: ImageRegistry shared) {
079: imageCache = shared;
080: baseURL = url;
081:
082: // objects
083: create(ImageConstants.APPLY_STYLE);
084: create(ImageConstants.CANCEL_STYLE);
085: }
086:
087: /**
088: * Cleanup image cache.
089: */
090: public void cleanUp() {
091: imageCache = null; // Display shutdown will clear imageCache
092: }
093:
094: public ImageDescriptor getImageDescriptor(String id) {
095: return getDescriptor(id);
096: }
097:
098: /**
099: * Returns the image associated with the given key, or <code>null</code> if none.
100: *
101: * @param id the key
102: * @return the image, or <code>null</code> if none
103: */
104: public static Image get(String id) {
105: Images images = StylePlugin.getDefault().getImages();
106: return images.imageCache.get(id);
107: }
108:
109: /**
110: * @see net.refractions.udig.catalog.ISharedImages#get(net.refractions.udig.catalog.IService)
111: *
112: public Image get( IService service ) {
113: ImageDescriptor icon = IService.getGenericIcon( service );
114: return cache.getImage( icon );
115: }
116:
117: /**
118: * @see net.refractions.udig.catalog.ISharedImages#get(net.refractions.udig.catalog.IGeoResource)
119: *
120: public Image get( IGeoResource service ) {
121: ImageDescriptor icon = IGeoResource.getGenericIcon( service );
122: return cache.getImage( icon );
123: } */
124: }
|