package com.quantum; import java.net.MalformedURLException; import java.net.URL; import java.util.Collections; import java.util.HashMap; import java.util.Map; import com.quantum.util.versioning.VersioningHelper; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.resource.ImageRegistry; import org.eclipse.swt.graphics.Image; import org.eclipse.ui.plugin.AbstractUIPlugin; /** * @author BC */ public class ImageStore { private static final Map imageDescriptorMap = Collections.synchronizedMap(new HashMap()); private static final String VERSION_2 = "_2x"; private static final String GIF_FILE_EXTENSION = ".gif"; public static final String ADD = "add"; public static final String ADD_SCHEMA = "add_schema"; public static final String APPEND = "append"; public static final String AUTOCOMMIT = "autocommit"; public static final String BOOKMARK = "bookmarks"; public static final String BOOKMARKFILE = "bookmarkfile"; public static final String CLASS = "class"; public static final String CLEAR = "clear"; public static final String CLOSE = "close"; public static final String COLUMN = "column"; public static final String CONNECT = "connect"; public static final String CONNECTED = "connected"; public static final String COPY = "copy"; public static final String CUT = "cut"; public static final String DELETE = "delete"; public static final String DISCONNECT = "disconnect"; public static final String DRIVER = "driver"; public static final String EDIT = "edit"; public static final String ENTITYGROUP = "entitygroup"; public static final String EXPORT = "export"; public static final String FILTER = "filter"; public static final String FOREIGNKEY = "foreignkey"; public static final String FULLDATA = "fulldata"; public static final String GRID = "grid"; public static final String GROUP = "group"; public static final String IMPORT = "import"; public static final String KEY = "key"; public static final String KEYCOLUMN = "keycolumn"; public static final String LOG = "log"; public static final String MISSINGSCHEMA = "missingschema"; public static final String MISSINGTABLE = "table"; public static final String NEXT = "next"; public static final String OPEN_TABLE = "table"; public static final String PASTE = "paste"; public static final String PLAY = "play"; public static final String PREVIOUS = "previous"; public static final String QMODEL = "qmodel"; public static final String QUANTUM = "quantum"; public static final String REFRESH = "refresh"; public static final String ROLLBACK = "rollback"; public static final String SAMPLE = "sample"; public static final String SCHEMA = "schema"; public static final String SCRIPT = "script"; public static final String SEQUENCE = "sequence"; public static final String STATEMENT = "statement"; public static final String STOP = "stop"; public static final String SUBSET = "subset"; public static final String SUCCESS = "success"; public static final String TABLE = "bigtable"; public static final String TEMPLATE = "template"; public static final String TORQUE = "torque"; public static final String TRIGGER = "trigger"; public static final String USER = "user"; public static final String VIEW = "view"; public static final String WARNING = "warning"; public static final String XML = "xml"; public static ImageDescriptor getImageDescriptor(String imageName) { return getImageDescriptor(imageName, QuantumPlugin.getDefault()); } public static Image getImage(String imageName) { return getImage(imageName, QuantumPlugin.getDefault()); } /** * @param imageName * @param default1 * @return */ private static Image getImage(String imageName, AbstractUIPlugin plugin) { ImageRegistry registry = plugin.getImageRegistry(); String key = stripSuffix(imageName); Image image = registry.get(getVersionAppropriateImage(key)); return image; } public static ImageDescriptor getImageDescriptor(String imageName, AbstractUIPlugin plugin) { ImageRegistry registry = plugin.getImageRegistry(); String key = stripSuffix(imageName); key = getVersionAppropriateImage(key); if (VersioningHelper.isEclipse21OrHigher()) { return VersioningHelper.getDescriptor(registry, key); } else { return (ImageDescriptor) imageDescriptorMap.get(key); } } /** * @param imageName * @return */ private static String stripSuffix(String imageName) { return imageName.endsWith(GIF_FILE_EXTENSION) ? imageName.substring(0, imageName.length() - 4) : imageName; } /** * @param imageName * @return */ private static String getVersionAppropriateImage(String imageName) { String version2Key = imageName + VERSION_2; return !VersioningHelper.isEclipse30() && imageDescriptorMap.containsKey(version2Key) ? version2Key : imageName; } /** * @param registry * @param url */ static void initialize(ImageRegistry registry, URL url) { addImage(registry, url, ADD); addImage(registry, url, BOOKMARK); addImage(registry, url, DISCONNECT); addImage(registry, url, ADD); addImage(registry, url, ADD_SCHEMA); addImage(registry, url, APPEND); addImage(registry, url, AUTOCOMMIT); addImage(registry, url, BOOKMARK); addImage(registry, url, BOOKMARKFILE); addImage(registry, url, CLASS); addImage(registry, url, CLEAR); addImage(registry, url, CLOSE); addImage(registry, url, COLUMN); addImage(registry, url, CONNECT); addImage(registry, url, CONNECTED); addImage(registry, url, COPY); addImage(registry, url, CUT); addImage(registry, url, DELETE); addImage(registry, url, DISCONNECT); addImage(registry, url, DRIVER); addImage(registry, url, EDIT); addImage(registry, url, ENTITYGROUP); addImage(registry, url, EXPORT); addImage(registry, url, FILTER); addImage(registry, url, FOREIGNKEY); addImage(registry, url, FULLDATA); addImage(registry, url, GRID); addImage(registry, url, GROUP); addImage(registry, url, IMPORT); addImage(registry, url, KEY); addImage(registry, url, KEYCOLUMN); addImage(registry, url, LOG); addImage(registry, url, MISSINGSCHEMA); addImage(registry, url, MISSINGTABLE); addImage(registry, url, NEXT); addImage(registry, url, PASTE); addImage(registry, url, PLAY); addImage(registry, url, PREVIOUS); addImage(registry, url, QMODEL); addImage(registry, url, QUANTUM); addImage(registry, url, REFRESH); addImage(registry, url, ROLLBACK); addImage(registry, url, SAMPLE); addImage(registry, url, SCHEMA); addImage(registry, url, SCRIPT); addImage(registry, url, SEQUENCE); addImage(registry, url, STATEMENT); addImage(registry, url, STOP); addImage(registry, url, SUBSET); addImage(registry, url, SUCCESS); addImage(registry, url, TABLE); addImage(registry, url, TEMPLATE); addImage(registry, url, TORQUE); addImage(registry, url, TRIGGER); addImage(registry, url, USER); addImage(registry, url, VIEW); addImage(registry, url, WARNING); addImage(registry, url, XML); if (!VersioningHelper.isEclipse30()) { addImage(registry, url, CLOSE + VERSION_2); addImage(registry, url, COPY + VERSION_2); addImage(registry, url, DELETE + VERSION_2); addImage(registry, url, EXPORT + VERSION_2); addImage(registry, url, IMPORT + VERSION_2); addImage(registry, url, PASTE + VERSION_2); addImage(registry, url, REFRESH + VERSION_2); } } /** * @param registry * @param url */ private static void addImage(ImageRegistry registry, URL url, String key) { try { ImageDescriptor descriptor = ImageDescriptor.createFromURL( new URL(url, key + GIF_FILE_EXTENSION)); ImageStore.imageDescriptorMap.put(key, descriptor); registry.put(key, descriptor); } catch (MalformedURLException e) { // skip, but try to go on to the next one... } } }