using Runnable to comply to the SWT threading model.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.quantum.sql / src / com / quantum / ImageStore.java
1 package com.quantum;
2
3 import java.net.MalformedURLException;
4 import java.net.URL;
5 import java.util.Collections;
6 import java.util.HashMap;
7
8 import java.util.Map;
9
10 import com.quantum.util.versioning.VersioningHelper;
11
12 import org.eclipse.jface.resource.ImageDescriptor;
13 import org.eclipse.jface.resource.ImageRegistry;
14 import org.eclipse.swt.graphics.Image;
15 import org.eclipse.ui.plugin.AbstractUIPlugin;
16
17
18
19 /**
20  * @author BC
21  */
22 public class ImageStore {
23         
24         private static final Map imageDescriptorMap = Collections.synchronizedMap(new HashMap());
25         
26         private static final String VERSION_2 = "_2x";
27         private static final String GIF_FILE_EXTENSION = ".gif";
28         
29         public static final String ADD = "add";
30         public static final String ADD_SCHEMA = "add_schema";
31         public static final String APPEND = "append";
32         public static final String AUTOCOMMIT = "autocommit";
33         public static final String BOOKMARK = "bookmarks";
34         public static final String BOOKMARKFILE = "bookmarkfile";
35         public static final String CLASS = "class";
36         public static final String CLEAR = "clear";
37         public static final String CLOSE = "close";
38         public static final String CLOSE_DISABLED = "close_disabled";
39         public static final String CLOSE_ALL = "close_all";
40         public static final String CLOSE_ALL_DISABLED = "close_all_disabled";
41         public static final String COLUMN = "column";
42         public static final String CONNECT = "connect";
43         public static final String CONNECTED = "connected";
44         public static final String COPY = "copy";
45         public static final String CUT = "cut";
46         public static final String DELETE = "delete";
47         public static final String DISCONNECT = "disconnect";
48         public static final String DRIVER = "driver";
49         public static final String EDIT = "edit";
50         public static final String ENTITYGROUP = "entitygroup";
51         public static final String EXPORT = "export";
52         public static final String EXTERNAL_JAR = "external_jar";
53         public static final String FILTER = "filter";
54         public static final String FOREIGNKEY = "foreignkey";
55         public static final String FULLDATA = "fulldata";
56         public static final String GRID = "grid";
57         public static final String GROUP = "group";
58         public static final String IMPORT = "import";
59         public static final String KEY = "key";
60         public static final String KEYCOLUMN = "keycolumn";
61         public static final String LOG = "log";
62         public static final String MISSINGSCHEMA = "missingschema";
63         public static final String MISSINGTABLE = "table";
64         public static final String NEXT = "next";
65         public static final String OPEN_TABLE = "table";
66         public static final String PASTE = "paste";
67         public static final String PLAY = "play";
68         public static final String PREVIOUS = "previous";
69         public static final String QMODEL = "qmodel";
70         public static final String QUANTUM = "quantum";
71         public static final String REFRESH = "refresh";
72         public static final String ROLLBACK = "rollback";
73         public static final String SAMPLE = "sample";
74         public static final String SCHEMA = "schema";
75         public static final String SCRIPT = "script";
76         public static final String SEQUENCE = "sequence";
77         public static final String STATEMENT = "statement";
78         public static final String STOP = "stop";
79         public static final String SUBSET = "subset";
80         public static final String SUCCESS = "success";
81         public static final String TABLE = "bigtable";
82         public static final String TABLE_DETAILS = "table_details";
83         public static final String TEMPLATE = "template";
84         public static final String TORQUE = "torque";
85         public static final String TRIGGER = "trigger";
86         public static final String USER = "user";
87         public static final String VIEW = "view";
88         public static final String WARNING = "warning";
89         public static final String XML = "xml";
90         
91         public static ImageDescriptor getImageDescriptor(String imageName) {
92                 return getImageDescriptor(imageName, QuantumPlugin.getDefault());
93         }
94
95         public static Image getImage(String imageName) {
96                 return getImage(imageName, QuantumPlugin.getDefault());
97         }
98
99         /**
100          * @param imageName
101          * @param default1
102          * @return
103          */
104         public static Image getImage(String imageName, AbstractUIPlugin plugin) {
105                 ImageRegistry registry = plugin.getImageRegistry();
106                 String key = stripSuffix(imageName);
107                 Image image = registry.get(getVersionAppropriateImage(key));
108                 return image;
109         }
110
111         public static ImageDescriptor getImageDescriptor(String imageName, AbstractUIPlugin plugin) {
112                 ImageRegistry registry = plugin.getImageRegistry();
113                 String key = stripSuffix(imageName);
114                 key = getVersionAppropriateImage(key);
115
116                 if (VersioningHelper.isEclipse21OrHigher()) {
117                         return VersioningHelper.getDescriptor(registry, key);
118                 } else {
119                         return (ImageDescriptor) imageDescriptorMap.get(key);
120                 }
121         }
122
123         /**
124          * @param imageName
125          * @return
126          */
127         private static String stripSuffix(String imageName) {
128                 return imageName.endsWith(GIF_FILE_EXTENSION) 
129                         ? imageName.substring(0, imageName.length() - 4)
130                         : imageName;
131         }
132
133         /**
134          * @param imageName
135          * @return
136          */
137         private static String getVersionAppropriateImage(String imageName) {
138                 String version2Key = imageName + VERSION_2;
139                 return !VersioningHelper.isEclipse30() && imageDescriptorMap.containsKey(version2Key) 
140                         ? version2Key : imageName;
141         }
142
143         /**
144          * @param registry
145          * @param url
146          */
147         static void initialize(ImageRegistry registry, URL url) {
148                 
149                 addImage(registry, url, ADD);
150                 addImage(registry, url, BOOKMARK);
151                 addImage(registry, url, DISCONNECT);
152                 addImage(registry, url, ADD);
153                 addImage(registry, url, ADD_SCHEMA);
154                 addImage(registry, url, APPEND);
155                 addImage(registry, url, AUTOCOMMIT);
156                 addImage(registry, url, BOOKMARK);
157                 addImage(registry, url, BOOKMARKFILE);
158                 addImage(registry, url, CLASS);
159                 addImage(registry, url, CLEAR);
160                 addImage(registry, url, CLOSE);
161                 addImage(registry, url, CLOSE_DISABLED);
162                 addImage(registry, url, CLOSE_ALL);
163                 addImage(registry, url, CLOSE_ALL_DISABLED);
164                 addImage(registry, url, COLUMN);
165                 addImage(registry, url, CONNECT);
166                 addImage(registry, url, CONNECTED);
167                 addImage(registry, url, COPY);
168                 addImage(registry, url, CUT);
169                 addImage(registry, url, DELETE);
170                 addImage(registry, url, DISCONNECT);
171                 addImage(registry, url, DRIVER);
172                 addImage(registry, url, EDIT);
173                 addImage(registry, url, ENTITYGROUP);
174                 addImage(registry, url, EXPORT);
175                 addImage(registry, url, EXTERNAL_JAR);
176                 addImage(registry, url, FILTER);
177                 addImage(registry, url, FOREIGNKEY);
178                 addImage(registry, url, FULLDATA);
179                 addImage(registry, url, GRID);
180                 addImage(registry, url, GROUP);
181                 addImage(registry, url, IMPORT);
182                 addImage(registry, url, KEY);
183                 addImage(registry, url, KEYCOLUMN);
184                 addImage(registry, url, LOG);
185                 addImage(registry, url, MISSINGSCHEMA);
186                 addImage(registry, url, MISSINGTABLE);
187                 addImage(registry, url, NEXT);
188                 addImage(registry, url, PASTE);
189                 addImage(registry, url, PLAY);
190                 addImage(registry, url, PREVIOUS);
191                 addImage(registry, url, QMODEL);
192                 addImage(registry, url, QUANTUM);
193                 addImage(registry, url, REFRESH);
194                 addImage(registry, url, ROLLBACK);
195                 addImage(registry, url, SAMPLE);
196                 addImage(registry, url, SCHEMA);
197                 addImage(registry, url, SCRIPT);
198                 addImage(registry, url, SEQUENCE);
199                 addImage(registry, url, STATEMENT);
200                 addImage(registry, url, STOP);
201                 addImage(registry, url, SUBSET);
202                 addImage(registry, url, SUCCESS);
203                 addImage(registry, url, TABLE);
204                 addImage(registry, url, TABLE_DETAILS);
205                 addImage(registry, url, TEMPLATE);
206                 addImage(registry, url, TORQUE);
207                 addImage(registry, url, TRIGGER);
208                 addImage(registry, url, USER);
209                 addImage(registry, url, VIEW);
210                 addImage(registry, url, WARNING);
211                 addImage(registry, url, XML);
212                 
213                 if (!VersioningHelper.isEclipse30()) {
214                         addImage(registry, url, CLOSE + VERSION_2);
215                         addImage(registry, url, CLOSE_ALL + VERSION_2);
216                         addImage(registry, url, COPY + VERSION_2);
217                         addImage(registry, url, DELETE + VERSION_2);
218                         addImage(registry, url, EXPORT + VERSION_2);
219                         addImage(registry, url, EXTERNAL_JAR + VERSION_2);
220                         addImage(registry, url, IMPORT + VERSION_2);
221                         addImage(registry, url, PASTE + VERSION_2);
222                         addImage(registry, url, REFRESH + VERSION_2);
223                 }
224         }
225
226         /**
227          * @param registry
228          * @param url
229          */
230         private static void addImage(ImageRegistry registry, URL url, String key) {
231                 try {
232                         ImageDescriptor descriptor = ImageDescriptor.createFromURL(
233                                         new URL(url, key + GIF_FILE_EXTENSION));
234                         ImageStore.imageDescriptorMap.put(key, descriptor);
235                         registry.put(key, descriptor);
236                 } catch (MalformedURLException e) {
237                         // skip, but try to go on to the next one...
238                 }
239         }
240 }