initial contribution
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiEditorPlugin.java
1 package net.sourceforge.phpeclipse.wiki.editor;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.text.MessageFormat;
7 import java.util.Hashtable;
8 import java.util.List;
9 import java.util.MissingResourceException;
10 import java.util.ResourceBundle;
11
12 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
13 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IExtension;
17 import org.eclipse.core.runtime.IPluginDescriptor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.text.templates.ContextTypeRegistry;
25 import org.eclipse.jface.text.templates.persistence.TemplateStore;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
33 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
35
36 public class WikiEditorPlugin extends AbstractUIPlugin {
37
38   private static WikiEditorPlugin fgPlugin;
39   public static final String HTTP_QUERY = "HTTP Query";
40   public static final String WIKIPEDIA_GET_TEXT = "Wikipedia-Load Text";
41   public static final String WEBLOG_API_SEND = "MetaWeblog API-Post";
42   
43   public static final String[] CONFIGURATION_TYPES = {
44       HTTP_QUERY,
45       WIKIPEDIA_GET_TEXT, 
46       WEBLOG_API_SEND
47   };
48   //image paths
49   public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
50
51   public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
52
53   public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
54
55   public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
56
57   private static ConfigurationManager manager;
58
59   public static final String IMG_MONITOR_ON = "monitorOn";
60
61   public static final String IMG_MONITOR_OFF = "monitorOff";
62
63   /**
64    * Creates an image and places it in the image registry.
65    * 
66    * @param id
67    *          the identifier for the image
68    * @param baseURL
69    *          the base URL for the image
70    */
71   protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
72     // Delegate to the plugin instance to avoid concurrent class loading problems
73     plugin.privateCreateImageDescriptor(id, baseURL);
74   }
75
76   public static WikiEditorPlugin getDefault() {
77     return fgPlugin;
78   }
79
80   /**
81    * Returns the image descriptor for the given image ID. Returns null if there is no such image.
82    * 
83    * @param id
84    *          the identifier for the image to retrieve
85    * @return the image associated with the given ID
86    */
87   public static ImageDescriptor getImageDescriptor(String id) {
88     // Delegate to the plugin instance to avoid concurrent class loading problems
89     return getDefault().privateGetImageDescriptor(id);
90   }
91
92   /**
93    * Convenience method to get an image descriptor for an extension
94    * 
95    * @param extension
96    *          the extension declaring the image
97    * @param subdirectoryAndFilename
98    *          the path to the image
99    * @return the image
100    */
101   public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
102     IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
103     URL path = pluginDescriptor.getInstallURL();
104     URL fullPathString = null;
105     try {
106       fullPathString = new URL(path, subdirectoryAndFilename);
107       return ImageDescriptor.createFromURL(fullPathString);
108     } catch (MalformedURLException e) {
109     }
110     return null;
111   }
112
113   public static String getResourceString(String key) {
114     ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
115     try {
116       return (bundle != null) ? bundle.getString(key) : key;
117     } catch (MissingResourceException e) {
118       return key;
119     }
120   }
121
122   /**
123    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
124    * If so, this display is returned. Otherwise the method returns the default display.
125    */
126   public static Display getStandardDisplay() {
127     Display display = Display.getCurrent();
128     if (display == null) {
129       display = Display.getDefault();
130     }
131     return display;
132   }
133
134   private ContributionContextTypeRegistry fContextTypeRegistry;
135
136   private ResourceBundle fResourceBundle;
137
138   private ContributionTemplateStore fTemplateStore;
139
140   private Hashtable imageDescriptors = new Hashtable(20);
141
142   public WikiEditorPlugin(IPluginDescriptor descriptor) {
143     super(descriptor);
144     initializeImages();
145     fgPlugin = this;
146     manager = ConfigurationManager.getInstance();
147     try {
148       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
149     } catch (MissingResourceException x) {
150       fResourceBundle = null;
151     }
152   }
153
154   /**
155    * Creates an image and places it in the image registry.
156    */
157   protected void createImageDescriptor(String id, URL baseURL) {
158     URL url = null;
159     try {
160       url = new URL(baseURL, ICON_PATH + id);
161     } catch (MalformedURLException e) {
162     }
163     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
164     imageDescriptors.put(id, desc);
165   }
166
167   public IWorkbenchPage getActivePage() {
168     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
169     if (window != null)
170       return window.getActivePage();
171     return null;
172   }
173
174   public ContextTypeRegistry getContextTypeRegistry() {
175     if (fContextTypeRegistry == null) {
176       fContextTypeRegistry = new ContributionContextTypeRegistry();
177       fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
178     }
179     return fContextTypeRegistry;
180   }
181
182   public Image getImage(String key) {
183     Image image = getImageRegistry().get(key);
184     if (image == null) {
185       ImageDescriptor d = getImageDescriptor(key);
186       image = d.createImage();
187       getImageRegistry().put(key, image);
188     }
189     return image;
190   }
191
192   public ResourceBundle getResourceBundle() {
193     return fResourceBundle;
194   }
195
196   public TemplateStore getTemplateStore() {
197     if (fTemplateStore == null) {
198       fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
199       try {
200         fTemplateStore.load();
201       } catch (IOException e) {
202         e.printStackTrace();
203       }
204     }
205
206     return fTemplateStore;
207   }
208
209   /*
210    * (non-Javadoc)
211    * 
212    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
213    */
214   protected void initializeDefaultPreferences(IPreferenceStore store) {
215   }
216
217   /*
218    * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
219    * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
220    */
221   private void initializeImages() {
222     URL baseURL = getDescriptor().getInstallURL();
223
224     //  special
225     createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
226     createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
227     createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
228     createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
229     createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
230     createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
231     createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
232     createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
233
234   }
235
236   public void log(String message) {
237     getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
238   }
239
240   public void log(String message, Exception e) {
241     getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
242   }
243
244   public void logAndReport(String title, String message, Exception e) {
245     log(message, e);
246     reportError(title, message);
247   }
248
249   private void privateCreateImageDescriptor(String id, URL baseURL) {
250     URL url = null;
251     try {
252       url = new URL(baseURL, ICON_PATH + id);
253     } catch (MalformedURLException e) {
254     }
255     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
256     imageDescriptors.put(id, desc);
257   }
258
259   private ImageDescriptor privateGetImageDescriptor(String id) {
260     if (!imageDescriptors.containsKey(id)) {
261       URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
262       createImageDescriptor(getDefault(), id, baseURL);
263     }
264     return (ImageDescriptor) imageDescriptors.get(id);
265   }
266
267   public void reportError(String title, String message) {
268     try {
269       Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
270       MessageDialog.openError(shell, title, message);
271     } catch (RuntimeException e) {
272       log(e.getLocalizedMessage(), e);
273     }
274   }
275
276   public void startup() throws CoreException {
277     super.startup();
278   }
279
280   /**
281    * Returns the translated String found with the given key.
282    * 
283    * @return java.lang.String
284    * @param key
285    *          java.lang.String
286    */
287   public static String getResource(String key) {
288     try {
289       return Platform.getResourceString(getDefault().getBundle(), key);
290     } catch (Exception e) {
291       return key;
292     }
293   }
294
295   /**
296    * Returns the translated String found with the given key, and formatted with the given object.
297    * 
298    * @param key
299    *          java.lang.String
300    * @param obj
301    *          java.lang.Object[]
302    * @return java.lang.String
303    */
304   public static String getResource(String key, Object[] obj) {
305     try {
306       return MessageFormat.format(getResource(key), obj);
307     } catch (Exception e) {
308       return key;
309     }
310   }
311
312   /**
313    * Returns the translated String found with the given key, and formatted with the given object.
314    * 
315    * @param key
316    *          java.lang.String
317    * @param s
318    *          java.lang.String
319    * @return java.lang.String
320    */
321   public static String getResource(String key, String s) {
322     try {
323       return MessageFormat.format(getResource(key), new String[] { s });
324     } catch (Exception e) {
325       return key;
326     }
327   }
328
329   /**
330    * Return a list of all the existing configurations.
331    * 
332    * @return java.util.List
333    */
334   public static List getConfigurations() {
335     return manager.getConfigurations();
336   }
337
338   /**
339    * Create a new monitor.
340    * 
341    * @return working copy
342    */
343   public static IConfigurationWorkingCopy createConfiguration() {
344     return manager.createConfiguration();
345   }
346   
347   public static String[] getTypes() {
348     return CONFIGURATION_TYPES;
349   }
350 }