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