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