improved codetemplate wizards; new html tag wizards
[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 TABLE_DETAILS = "table_details";
86         public static final String TEMPLATE = "template";
87         public static final String TRIGGER = "trigger";
88         public static final String USER = "user";
89         public static final String VIEW = "view";
90         public static final String WARNING = "warning";
91         public static final String XML = "xml";
92         public static final String CHECKED = "checked";
93         public static final String UNCHECKED = "unchecked";
94         
95         public static ImageDescriptor getImageDescriptor(String imageName) {
96                 return getImageDescriptor(imageName, QuantumPlugin.getDefault());
97         }
98
99         public static Image getImage(String imageName) {
100                 return getImage(imageName, QuantumPlugin.getDefault());
101         }
102
103         /**
104          * @param imageName
105          * @param default1
106          * @return
107          */
108         public static Image getImage(String imageName, AbstractUIPlugin plugin) {
109                 ImageRegistry registry = plugin.getImageRegistry();
110                 String key = stripSuffix(imageName);
111                 Image image = registry.get(getVersionAppropriateImage(key, plugin));
112                 return image;
113         }
114
115         public static ImageDescriptor getImageDescriptor(String imageName, AbstractUIPlugin plugin) {
116                 ImageRegistry registry = plugin.getImageRegistry();
117                 String key = stripSuffix(imageName);
118                 key = getVersionAppropriateImage(key, plugin);
119
120                 if (VersioningHelper.isEclipse21OrHigher()) {
121                         return VersioningHelper.getDescriptor(registry, key);
122                 } else {
123                         return (ImageDescriptor) getImageDescriptorMap(plugin).get(key);
124                 }
125         }
126         
127         private static Map getImageDescriptorMap(AbstractUIPlugin plugin) {
128                 String key = plugin.getClass().getName();
129                 if (!imageDescriptorMap.containsKey(key)) {
130                         imageDescriptorMap.put(key, new HashMap());
131                 }
132                 return (Map) imageDescriptorMap.get(key);
133         }
134
135         /**
136          * @param imageName
137          * @return
138          */
139         private static String stripSuffix(String imageName) {
140                 if (imageName.endsWith(GIF_FILE_EXTENSION)) {
141                         return imageName.substring(0, imageName.length() - 4);
142                 }else if (imageName.endsWith(JPG_FILE_EXTENSION)) {
143                                 return imageName.substring(0, imageName.length() - 4);
144                 } else {
145                         return imageName;
146                 }
147         }
148
149         /**
150          * @param imageName
151          * @return
152          */
153         private static String getVersionAppropriateImage(String imageName, AbstractUIPlugin plugin) {
154                 String version2Key = imageName + VERSION_2;
155                 return !VersioningHelper.isEclipse30() && getImageDescriptorMap(plugin).containsKey(version2Key) 
156                         ? version2Key : imageName;
157         }
158
159         /**
160          * @param registry
161          * @param url
162          */
163         static void initialize(AbstractUIPlugin plugin, ImageRegistry registry, URL url) {
164                 
165                 addImage(plugin, registry, url, ADD);
166                 addImage(plugin, registry, url, BOOKMARK);
167                 addImage(plugin, registry, url, DISCONNECT);
168                 addImage(plugin, registry, url, ADD);
169                 addImage(plugin, registry, url, ADD_SCHEMA);
170                 addImage(plugin, registry, url, APPEND);
171                 addImage(plugin, registry, url, AUTOCOMMIT);
172                 addImage(plugin, registry, url, BOOKMARK);
173                 addImage(plugin, registry, url, BOOKMARKFILE);
174                 addImage(plugin, registry, url, CLASS);
175                 addImage(plugin, registry, url, CLEAR);
176                 addImage(plugin, registry, url, CLOSE);
177                 addImage(plugin, registry, url, CLOSE_DISABLED);
178                 addImage(plugin, registry, url, CLOSE_ALL);
179                 addImage(plugin, registry, url, CLOSE_ALL_DISABLED);
180                 addImage(plugin, registry, url, COLUMN);
181                 addImage(plugin, registry, url, CONNECT);
182                 addImage(plugin, registry, url, CONNECTED);
183                 addImage(plugin, registry, url, COPY);
184                 addImage(plugin, registry, url, CUT);
185                 addImage(plugin, registry, url, DELETE);
186                 addImage(plugin, registry, url, DISCONNECT);
187                 addImage(plugin, registry, url, DRIVER);
188                 addImage(plugin, registry, url, EDIT);
189                 addImage(plugin, registry, url, ENTITYGROUP);
190                 addImage(plugin, registry, url, EXPORT);
191                 addImage(plugin, registry, url, EXTERNAL_JAR);
192                 addImage(plugin, registry, url, FILTER);
193                 addImage(plugin, registry, url, FOREIGNKEY);
194                 addImage(plugin, registry, url, FULLDATA);
195                 addImage(plugin, registry, url, GRID);
196                 addImage(plugin, registry, url, GROUP);
197                 addImage(plugin, registry, url, IMPORT);
198                 addImage(plugin, registry, url, KEY);
199                 addImage(plugin, registry, url, KEYCOLUMN);
200                 addImage(plugin, registry, url, LOG);
201                 addImage(plugin, registry, url, MISSINGSCHEMA);
202                 addImage(plugin, registry, url, MISSINGTABLE);
203                 addImage(plugin, registry, url, NEXT);
204                 addImage(plugin, registry, url, NEW_BOOKMARK);
205                 addImage(plugin, registry, url, NEW_BOOKMARK_WIZARD, JPG_FILE_EXTENSION);
206                 addImage(plugin, registry, url, PASTE);
207                 addImage(plugin, registry, url, PLAY);
208                 addImage(plugin, registry, url, PREVIOUS);
209                 addImage(plugin, registry, url, QMODEL);
210                 addImage(plugin, registry, url, QUANTUM);
211                 addImage(plugin, registry, url, REFRESH);
212                 addImage(plugin, registry, url, ROLLBACK);
213                 addImage(plugin, registry, url, SAMPLE);
214                 addImage(plugin, registry, url, SCHEMA);
215                 addImage(plugin, registry, url, SCRIPT);
216                 addImage(plugin, registry, url, SEQUENCE);
217                 addImage(plugin, registry, url, STATEMENT);
218                 addImage(plugin, registry, url, STOP);
219                 addImage(plugin, registry, url, SUBSET);
220                 addImage(plugin, registry, url, SUCCESS);
221                 addImage(plugin, registry, url, TABLE);
222                 addImage(plugin, registry, url, TABLE_DETAILS);
223                 addImage(plugin, registry, url, TEMPLATE);
224                 addImage(plugin, registry, url, TRIGGER);
225                 addImage(plugin, registry, url, USER);
226                 addImage(plugin, registry, url, VIEW);
227                 addImage(plugin, registry, url, WARNING);
228                 addImage(plugin, registry, url, XML);
229                 addImage(plugin, registry, url, CHECKED);
230                 addImage(plugin, registry, url, UNCHECKED);
231                 
232                 if (!VersioningHelper.isEclipse30()) {
233                         addImage(plugin, registry, url, CLOSE + VERSION_2);
234                         addImage(plugin, registry, url, CLOSE_ALL + VERSION_2);
235                         addImage(plugin, registry, url, COPY + VERSION_2);
236                         addImage(plugin, registry, url, DELETE + VERSION_2);
237                         addImage(plugin, registry, url, EXPORT + VERSION_2);
238                         addImage(plugin, registry, url, EXTERNAL_JAR + VERSION_2);
239                         addImage(plugin, registry, url, IMPORT + VERSION_2);
240                         addImage(plugin, registry, url, PASTE + VERSION_2);
241                         addImage(plugin, registry, url, REFRESH + VERSION_2);
242                 }
243         }
244         public static void addImage(AbstractUIPlugin plugin, ImageRegistry registry, 
245                         URL url, String key) {
246                 addImage(plugin, registry, url, key, GIF_FILE_EXTENSION);
247         }
248
249         public static void addImage(AbstractUIPlugin plugin, ImageRegistry registry, 
250                         URL url, String key, String extension) {
251                 try {
252                         ImageDescriptor descriptor = ImageDescriptor.createFromURL(
253                                         new URL(url, key + extension));
254                         getImageDescriptorMap(plugin).put(key, descriptor);
255                         registry.put(key, descriptor);
256                 } catch (MalformedURLException e) {
257                         // skip, but try to go on to the next one...
258                 }
259         }
260 }