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