fixed additional catches block bug
[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         private static final String JPG_FILE_EXTENSION = ".jpg";
29         
30         public static final String ADD = "add";
31         public static final String ADD_SCHEMA = "add_schema";
32         public static final String APPEND = "append";
33         public static final String AUTOCOMMIT = "autocommit";
34         public static final String BOOKMARK = "bookmarks";
35         public static final String BOOKMARKFILE = "bookmarkfile";
36         public static final String CLASS = "class";
37         public static final String CLEAR = "clear";
38         public static final String CLOSE = "close";
39         public static final String CLOSE_DISABLED = "close_disabled";
40         public static final String CLOSE_ALL = "close_all";
41         public static final String CLOSE_ALL_DISABLED = "close_all_disabled";
42         public static final String COLUMN = "column";
43         public static final String CONNECT = "connect";
44         public static final String CONNECTED = "connected";
45         public static final String COPY = "copy";
46         public static final String CUT = "cut";
47         public static final String DELETE = "delete";
48         public static final String DISCONNECT = "disconnect";
49         public static final String DRIVER = "driver";
50         public static final String EDIT = "edit";
51         public static final String ENTITYGROUP = "entitygroup";
52         public static final String EXPORT = "export";
53         public static final String EXTERNAL_JAR = "external_jar";
54         public static final String FILTER = "filter";
55         public static final String FOREIGNKEY = "foreignkey";
56         public static final String FULLDATA = "fulldata";
57         public static final String GRID = "grid";
58         public static final String GROUP = "group";
59         public static final String IMPORT = "import";
60         public static final String KEY = "key";
61         public static final String KEYCOLUMN = "keycolumn";
62         public static final String LOG = "log";
63         public static final String MISSINGSCHEMA = "missingschema";
64         public static final String MISSINGTABLE = "table";
65         public static final String NEXT = "next";
66         public static final String NEW_BOOKMARK = "new_bookmark";
67         public static final String NEW_BOOKMARK_WIZARD = "new_bookmark_wizard";
68         public static final String OPEN_TABLE = "table";
69         public static final String PASTE = "paste";
70         public static final String PLAY = "play";
71         public static final String PREVIOUS = "previous";
72         public static final String QMODEL = "qmodel";
73         public static final String QUANTUM = "quantum";
74         public static final String REFRESH = "refresh";
75         public static final String ROLLBACK = "rollback";
76         public static final String SAMPLE = "sample";
77         public static final String SCHEMA = "schema";
78         public static final String SCRIPT = "script";
79         public static final String SEQUENCE = "sequence";
80         public static final String STATEMENT = "statement";
81         public static final String STOP = "stop";
82         public static final String SUBSET = "subset";
83         public static final String SUCCESS = "success";
84         public static final String TABLE = "bigtable";
85         public static final String SYNONYM_TABLE = "big_syn_table";
86         public static final String TABLE_DETAILS = "table_details";
87         public static final String TEMPLATE = "template";
88         public static final String TRIGGER = "trigger";
89         public static final String USER = "user";
90         public static final String VIEW = "view";
91         public static final String WARNING = "warning";
92         public static final String XML = "xml";
93         public static final String CHECKED = "checked";
94         public static final String UNCHECKED = "unchecked";
95         
96         public static ImageDescriptor getImageDescriptor(String imageName) {
97                 return getImageDescriptor(imageName, QuantumPlugin.getDefault());
98         }
99
100         public static Image getImage(String imageName) {
101                 return getImage(imageName, QuantumPlugin.getDefault());
102         }
103
104         /**
105          * @param imageName
106          * @param default1
107          * @return
108          */
109         public static Image getImage(String imageName, AbstractUIPlugin plugin) {
110                 ImageRegistry registry = plugin.getImageRegistry();
111                 String key = stripSuffix(imageName);
112                 Image image = registry.get(getVersionAppropriateImage(key, plugin));
113                 return image;
114         }
115
116         public static ImageDescriptor getImageDescriptor(String imageName, AbstractUIPlugin plugin) {
117                 ImageRegistry registry = plugin.getImageRegistry();
118                 String key = stripSuffix(imageName);
119                 key = getVersionAppropriateImage(key, plugin);
120
121                 if (VersioningHelper.isEclipse21OrHigher()) {
122                         return VersioningHelper.getDescriptor(registry, key);
123                 } else {
124                         return (ImageDescriptor) getImageDescriptorMap(plugin).get(key);
125                 }
126         }
127         
128         private static Map getImageDescriptorMap(AbstractUIPlugin plugin) {
129                 String key = plugin.getClass().getName();
130                 if (!imageDescriptorMap.containsKey(key)) {
131                         imageDescriptorMap.put(key, new HashMap());
132                 }
133                 return (Map) imageDescriptorMap.get(key);
134         }
135
136         /**
137          * @param imageName
138          * @return
139          */
140         private static String stripSuffix(String imageName) {
141                 if (imageName.endsWith(GIF_FILE_EXTENSION)) {
142                         return imageName.substring(0, imageName.length() - 4);
143                 }else if (imageName.endsWith(JPG_FILE_EXTENSION)) {
144                                 return imageName.substring(0, imageName.length() - 4);
145                 } else {
146                         return imageName;
147                 }
148         }
149
150         /**
151          * @param imageName
152          * @return
153          */
154         private static String getVersionAppropriateImage(String imageName, AbstractUIPlugin plugin) {
155                 String version2Key = imageName + VERSION_2;
156                 return !VersioningHelper.isEclipse30() && getImageDescriptorMap(plugin).containsKey(version2Key) 
157                         ? version2Key : imageName;
158         }
159
160         /**
161          * @param registry
162          * @param url
163          */
164         static void initialize(AbstractUIPlugin plugin, ImageRegistry registry, URL url) {
165                 
166                 addImage(plugin, registry, url, ADD);
167                 addImage(plugin, registry, url, BOOKMARK);
168                 addImage(plugin, registry, url, DISCONNECT);
169                 addImage(plugin, registry, url, ADD);
170                 addImage(plugin, registry, url, ADD_SCHEMA);
171                 addImage(plugin, registry, url, APPEND);
172                 addImage(plugin, registry, url, AUTOCOMMIT);
173                 addImage(plugin, registry, url, BOOKMARK);
174                 addImage(plugin, registry, url, BOOKMARKFILE);
175                 addImage(plugin, registry, url, CLASS);
176                 addImage(plugin, registry, url, CLEAR);
177                 addImage(plugin, registry, url, CLOSE);
178                 addImage(plugin, registry, url, CLOSE_DISABLED);
179                 addImage(plugin, registry, url, CLOSE_ALL);
180                 addImage(plugin, registry, url, CLOSE_ALL_DISABLED);
181                 addImage(plugin, registry, url, COLUMN);
182                 addImage(plugin, registry, url, CONNECT);
183                 addImage(plugin, registry, url, CONNECTED);
184                 addImage(plugin, registry, url, COPY);
185                 addImage(plugin, registry, url, CUT);
186                 addImage(plugin, registry, url, DELETE);
187                 addImage(plugin, registry, url, DISCONNECT);
188                 addImage(plugin, registry, url, DRIVER);
189                 addImage(plugin, registry, url, EDIT);
190                 addImage(plugin, registry, url, ENTITYGROUP);
191                 addImage(plugin, registry, url, EXPORT);
192                 addImage(plugin, registry, url, EXTERNAL_JAR);
193                 addImage(plugin, registry, url, FILTER);
194                 addImage(plugin, registry, url, FOREIGNKEY);
195                 addImage(plugin, registry, url, FULLDATA);
196                 addImage(plugin, registry, url, GRID);
197                 addImage(plugin, registry, url, GROUP);
198                 addImage(plugin, registry, url, IMPORT);
199                 addImage(plugin, registry, url, KEY);
200                 addImage(plugin, registry, url, KEYCOLUMN);
201                 addImage(plugin, registry, url, LOG);
202                 addImage(plugin, registry, url, MISSINGSCHEMA);
203                 addImage(plugin, registry, url, MISSINGTABLE);
204                 addImage(plugin, registry, url, NEXT);
205                 addImage(plugin, registry, url, NEW_BOOKMARK);
206                 addImage(plugin, registry, url, NEW_BOOKMARK_WIZARD, JPG_FILE_EXTENSION);
207                 addImage(plugin, registry, url, PASTE);
208                 addImage(plugin, registry, url, PLAY);
209                 addImage(plugin, registry, url, PREVIOUS);
210                 addImage(plugin, registry, url, QMODEL);
211                 addImage(plugin, registry, url, QUANTUM);
212                 addImage(plugin, registry, url, REFRESH);
213                 addImage(plugin, registry, url, ROLLBACK);
214                 addImage(plugin, registry, url, SAMPLE);
215                 addImage(plugin, registry, url, SCHEMA);
216                 addImage(plugin, registry, url, SCRIPT);
217                 addImage(plugin, registry, url, SEQUENCE);
218                 addImage(plugin, registry, url, STATEMENT);
219                 addImage(plugin, registry, url, STOP);
220                 addImage(plugin, registry, url, SUBSET);
221                 addImage(plugin, registry, url, SUCCESS);
222                 addImage(plugin, registry, url, TABLE);
223                 addImage(plugin, registry, url, SYNONYM_TABLE);
224                 addImage(plugin, registry, url, TABLE_DETAILS);
225                 addImage(plugin, registry, url, TEMPLATE);
226                 addImage(plugin, registry, url, TRIGGER);
227                 addImage(plugin, registry, url, USER);
228                 addImage(plugin, registry, url, VIEW);
229                 addImage(plugin, registry, url, WARNING);
230                 addImage(plugin, registry, url, XML);
231                 addImage(plugin, registry, url, CHECKED);
232                 addImage(plugin, registry, url, UNCHECKED);
233                 
234                 if (!VersioningHelper.isEclipse30()) {
235                         addImage(plugin, registry, url, CLOSE + VERSION_2);
236                         addImage(plugin, registry, url, CLOSE_ALL + VERSION_2);
237                         addImage(plugin, registry, url, COPY + VERSION_2);
238                         addImage(plugin, registry, url, DELETE + VERSION_2);
239                         addImage(plugin, registry, url, EXPORT + VERSION_2);
240                         addImage(plugin, registry, url, EXTERNAL_JAR + VERSION_2);
241                         addImage(plugin, registry, url, IMPORT + VERSION_2);
242                         addImage(plugin, registry, url, PASTE + VERSION_2);
243                         addImage(plugin, registry, url, REFRESH + VERSION_2);
244                 }
245         }
246         public static void addImage(AbstractUIPlugin plugin, ImageRegistry registry, 
247                         URL url, String key) {
248                 addImage(plugin, registry, url, key, GIF_FILE_EXTENSION);
249         }
250
251         public static void addImage(AbstractUIPlugin plugin, ImageRegistry registry, 
252                         URL url, String key, String extension) {
253                 try {
254                         ImageDescriptor descriptor = ImageDescriptor.createFromURL(
255                                         new URL(url, key + extension));
256                         getImageDescriptorMap(plugin).put(key, descriptor);
257                         registry.put(key, descriptor);
258                 } catch (MalformedURLException e) {
259                         // skip, but try to go on to the next one...
260                 }
261         }
262 }