Upload/Download of multiple files now possible
[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.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.sql.SQLException;
9 import java.text.MessageFormat;
10 import java.util.ArrayList;
11 import java.util.Hashtable;
12 import java.util.List;
13 import java.util.MissingResourceException;
14 import java.util.ResourceBundle;
15
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
17 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
18 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
19 import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB;
20
21 import org.eclipse.core.resources.IWorkspace;
22 import org.eclipse.core.resources.ResourcesPlugin;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IExtension;
25 import org.eclipse.core.runtime.IPluginDescriptor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Platform;
28 import org.eclipse.core.runtime.Status;
29 import org.eclipse.jface.dialogs.MessageDialog;
30 import org.eclipse.jface.preference.IPreferenceStore;
31 import org.eclipse.jface.resource.ImageDescriptor;
32 import org.eclipse.jface.text.templates.ContextTypeRegistry;
33 import org.eclipse.jface.text.templates.persistence.TemplateStore;
34 import org.eclipse.swt.graphics.Image;
35 import org.eclipse.swt.widgets.Display;
36 import org.eclipse.swt.widgets.Shell;
37 import org.eclipse.ui.IWorkbenchPage;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
41 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
42 import org.eclipse.ui.plugin.AbstractUIPlugin;
43
44 public class WikiEditorPlugin extends AbstractUIPlugin {
45
46   private static WikiEditorPlugin fgPlugin;
47
48   public final static String AUTOMATICALLY_CREATED = "<!-- automatically created -->";
49
50   public final static String WP_EXTENSION = "wp";
51   public final static String XML_START_1 = "<?xml version=\"1.0\" encoding=\"";
52   public final static String XML_START_2 = "\" ?>\n"
53     + "<mediawiki version=\"0.1\">\n";
54   public final static String XML_END ="\n</mediawiki>";
55   public static final String PREFIX_LOAD = "Load ";
56
57   public static final String PREFIX_STORE = "Store ";
58
59   public static final String HTTP_QUERY = "HTTP Query";
60
61   //  public static final String WIKIPEDIA_GET_TEXT = "Load WikipediaEN";
62   //
63   //  public static final String WIKIPEDIA_SET_TEXT = "Store WikipediaEN";
64   public static final String WIKIPEDIA_SQL = "Wikipedia SQL access";
65
66   public static final String BLOG_A_WIKI = "Blog as Wiki Text";
67
68   public static final String BLOG_A_HTML = "Blog as HTML Text";
69
70   public static final String PLOG4U_DE_LOAD = PREFIX_LOAD + "Plog4UDE";
71
72   public static final String PLOG4U_DE_STORE = PREFIX_STORE + "Plog4UDE";
73
74   public static final String PLOG4U_ORG_LOAD = PREFIX_LOAD + "Plog4UORG";
75
76   public static final String PLOG4U_ORG_STORE = PREFIX_STORE + "Plog4UORG";
77
78   public static final String[] PREDEFINED_TYPES = {
79       HTTP_QUERY,
80       WIKIPEDIA_SQL,
81       PLOG4U_DE_LOAD,
82       PLOG4U_DE_STORE,
83       PLOG4U_ORG_LOAD,
84       PLOG4U_ORG_STORE,
85       BLOG_A_WIKI,
86       BLOG_A_HTML };
87
88   public static final String[] PREDEFINED_WIKIS = {
89       "WikibooksDE",
90       "WikibooksEN",
91       "WikinewsDE",
92       "WikinewsEN",
93       "WikipediaAR",
94       "WikipediaCA",
95       "WikipediaDE",
96       "WikipediaEN",
97       "WikipediaEO",
98       "WikipediaES",
99       "WikipediaET",
100       "WikipediaFI",
101       "WikipediaFR",
102       "WikipediaHE",
103       "WikipediaIA",
104       "WikipediaIT",
105       "WikipediaJA",
106       "WikipediaNL",
107       "WikipediaPL",
108       "WikipediaPT",
109       "WikipediaRO",
110       "WikipediaSL",
111       "WikipediaSV",
112       "WikipediaZH" };
113
114   public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
115
116   //image paths
117   public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
118
119   public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
120
121   public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
122
123   public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
124
125   public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
126
127   public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
128
129   public final static String LOCAL_CSS_URL = "__local_css_url";
130
131   public final static String EXPORT_CSS_URL = "__export_css_url";
132
133   public final static String PREF_STRING_CONFIGURATIONS = "__configurations3";
134
135   public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
136       + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
137       + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
138       + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
139
140       + "<config name=\"Plog4U.de Download\" type-id=\"" + PLOG4U_DE_LOAD
141       + "\" url=\"http://www.plog4u.de/index.php/Spezial:Export\"/>" + "<config name=\"Plog4U.org Download\" type-id=\""
142       + PLOG4U_ORG_LOAD + "\" url=\"http://www.plog4u.org/index.php/Special:Export\"/>"
143       + "<config name=\"Plog4U.de Upload\" type-id=\"" + PLOG4U_DE_STORE + "\" url=\"http://en.wikipedia.org/w/index.php\"/>"
144       + "<config name=\"Plog4U.org Upload\" type-id=\"" + PLOG4U_ORG_STORE + "\" url=\"http://en.wikibooks.org/w/index.php\"/>"
145
146       + "<config name=\"WikipediaEN Download\" type-id=\"" + PREFIX_LOAD
147       + "WikipediaEN\" url=\"http://en.wikipedia.org/wiki/Special:Export\"/>" + "<config name=\"WikibooksEN Download\" type-id=\""
148       + PREFIX_LOAD + "WikibooksEN\" url=\"http://en.wikibooks.org/wiki/Special:Export\"/>"
149       + "<config name=\"WikipediaEN Upload\" type-id=\"" + PREFIX_STORE
150       + "WikipediaEN\" url=\"http://en.wikipedia.org/w/index.php\"/>" + "<config name=\"WikibooksEN Upload\" type-id=\""
151       + PREFIX_STORE + "WikibooksEN\" url=\"http://en.wikibooks.org/w/index.php\"/>"
152       + "<config name=\"Wikipedia-SQL\" type-id=\"Wikipedia SQL access\" user=\"root\" url=\"jdbc:mysql://localhost/wikidb\"/>"
153       + "</configurations>";
154
155   public static WikipediaDB fWikiDB = null;
156
157   private static ConfigurationManager manager;
158
159   //
160   //  public static final String IMG_MONITOR_ON = "monitorOn";
161   //
162   //  public static final String IMG_MONITOR_OFF = "monitorOff";
163
164   /**
165    * Creates an image and places it in the image registry.
166    * 
167    * @param id
168    *          the identifier for the image
169    * @param baseURL
170    *          the base URL for the image
171    */
172   protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
173     // Delegate to the plugin instance to avoid concurrent class loading problems
174     plugin.privateCreateImageDescriptor(id, baseURL);
175   }
176
177   public static WikiEditorPlugin getDefault() {
178     return fgPlugin;
179   }
180
181   /**
182    * Returns the image descriptor for the given image ID. Returns null if there is no such image.
183    * 
184    * @param id
185    *          the identifier for the image to retrieve
186    * @return the image associated with the given ID
187    */
188   public static ImageDescriptor getImageDescriptor(String id) {
189     // Delegate to the plugin instance to avoid concurrent class loading problems
190     return getDefault().privateGetImageDescriptor(id);
191   }
192
193   /**
194    * Convenience method to get an image descriptor for an extension
195    * 
196    * @param extension
197    *          the extension declaring the image
198    * @param subdirectoryAndFilename
199    *          the path to the image
200    * @return the image
201    */
202   public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
203     IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
204     URL path = pluginDescriptor.getInstallURL();
205     URL fullPathString = null;
206     try {
207       fullPathString = new URL(path, subdirectoryAndFilename);
208       return ImageDescriptor.createFromURL(fullPathString);
209     } catch (MalformedURLException e) {
210     }
211     return null;
212   }
213
214   public static String getResourceString(String key) {
215     ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
216     try {
217       return (bundle != null) ? bundle.getString(key) : key;
218     } catch (MissingResourceException e) {
219       return key;
220     }
221   }
222
223   /**
224    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
225    * If so, this display is returned. Otherwise the method returns the default display.
226    */
227   public static Display getStandardDisplay() {
228     Display display = Display.getCurrent();
229     if (display == null) {
230       display = Display.getDefault();
231     }
232     return display;
233   }
234
235   private ContributionContextTypeRegistry fContextTypeRegistry;
236
237   private ResourceBundle fResourceBundle;
238
239   private ContributionTemplateStore fTemplateStore;
240
241   private Hashtable imageDescriptors = new Hashtable(20);
242
243   public WikiEditorPlugin(IPluginDescriptor descriptor) {
244     super(descriptor);
245     initializeImages();
246     fgPlugin = this;
247     for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
248       CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
249     }
250
251     for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
252       CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
253     }
254     for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
255       CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
256     }
257     manager = ConfigurationManager.getInstance();
258     try {
259       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
260     } catch (MissingResourceException x) {
261       fResourceBundle = null;
262     }
263   }
264
265   /**
266    * Creates an image and places it in the image registry.
267    */
268   protected void createImageDescriptor(String id, URL baseURL) {
269     URL url = null;
270     try {
271       url = new URL(baseURL, ICON_PATH + id);
272     } catch (MalformedURLException e) {
273     }
274     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
275     imageDescriptors.put(id, desc);
276   }
277
278   public IWorkbenchPage getActivePage() {
279     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
280     if (window != null)
281       return window.getActivePage();
282     return null;
283   }
284
285   public ContextTypeRegistry getContextTypeRegistry() {
286     if (fContextTypeRegistry == null) {
287       fContextTypeRegistry = new ContributionContextTypeRegistry();
288       fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
289     }
290     return fContextTypeRegistry;
291   }
292
293   public Image getImage(String key) {
294     Image image = getImageRegistry().get(key);
295     if (image == null) {
296       ImageDescriptor d = getImageDescriptor(key);
297       image = d.createImage();
298       getImageRegistry().put(key, image);
299     }
300     return image;
301   }
302
303   public ResourceBundle getResourceBundle() {
304     return fResourceBundle;
305   }
306
307   public TemplateStore getTemplateStore() {
308     if (fTemplateStore == null) {
309       fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
310       try {
311         fTemplateStore.load();
312       } catch (IOException e) {
313         e.printStackTrace();
314       }
315     }
316
317     return fTemplateStore;
318   }
319
320   /*
321    * (non-Javadoc)
322    * 
323    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
324    */
325   protected void initializeDefaultPreferences(IPreferenceStore store) {
326     store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
327   }
328
329   /*
330    * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
331    * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
332    */
333   private void initializeImages() {
334     URL baseURL = getDescriptor().getInstallURL();
335
336     //  special
337     createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
338     createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
339     createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
340     createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
341     createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
342     createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
343     createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
344     createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
345
346   }
347
348   public void log(String message) {
349     getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
350   }
351
352   public void log(String message, Exception e) {
353     getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
354   }
355
356   public void logAndReport(String title, String message, Exception e) {
357     log(message, e);
358     reportError(title, message);
359   }
360
361   private void privateCreateImageDescriptor(String id, URL baseURL) {
362     URL url = null;
363     try {
364       url = new URL(baseURL, ICON_PATH + id);
365     } catch (MalformedURLException e) {
366     }
367     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
368     imageDescriptors.put(id, desc);
369   }
370
371   private ImageDescriptor privateGetImageDescriptor(String id) {
372     if (!imageDescriptors.containsKey(id)) {
373       URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
374       createImageDescriptor(getDefault(), id, baseURL);
375     }
376     return (ImageDescriptor) imageDescriptors.get(id);
377   }
378
379   public void reportError(String title, String message) {
380     try {
381       Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
382       MessageDialog.openError(shell, title, message);
383     } catch (RuntimeException e) {
384       log(e.getLocalizedMessage(), e);
385     }
386   }
387
388   public void startup() throws CoreException {
389     super.startup();
390   }
391
392   /**
393    * Returns the translated String found with the given key.
394    * 
395    * @return java.lang.String
396    * @param key
397    *          java.lang.String
398    */
399   public static String getResource(String key) {
400     try {
401       return Platform.getResourceString(getDefault().getBundle(), key);
402     } catch (Exception e) {
403       return key;
404     }
405   }
406
407   /**
408    * Returns the translated String found with the given key, and formatted with the given object.
409    * 
410    * @param key
411    *          java.lang.String
412    * @param obj
413    *          java.lang.Object[]
414    * @return java.lang.String
415    */
416   public static String getResource(String key, Object[] obj) {
417     try {
418       return MessageFormat.format(getResource(key), obj);
419     } catch (Exception e) {
420       return key;
421     }
422   }
423
424   /**
425    * Returns the translated String found with the given key, and formatted with the given object.
426    * 
427    * @param key
428    *          java.lang.String
429    * @param s
430    *          java.lang.String
431    * @return java.lang.String
432    */
433   public static String getResource(String key, String s) {
434     try {
435       return MessageFormat.format(getResource(key), new String[] { s });
436     } catch (Exception e) {
437       return key;
438     }
439   }
440
441   /**
442    * Return a list of all the existing configurations.
443    * 
444    * @return java.util.List
445    */
446   public static List getConfigurations() {
447     return manager.getConfigurations();
448   }
449
450   /**
451    * Create a new monitor.
452    * 
453    * @return working copy
454    */
455   public static IConfigurationWorkingCopy createConfiguration() {
456     return manager.createConfiguration();
457   }
458
459   public static ArrayList getTypes() {
460     return CONFIGURATION_TYPES;
461   }
462
463   public static void addType(String type) {
464     CONFIGURATION_TYPES.add(type);
465   }
466
467   /*
468    * (non-Javadoc)
469    * 
470    * @see org.eclipse.core.runtime.Plugin#shutdown()
471    */
472   public void shutdown() throws CoreException {
473     if (fWikiDB != null) {
474       try {
475         fWikiDB.shutdown();
476       } catch (SQLException e) {
477       }
478     }
479     super.shutdown();
480   }
481
482   public static void log(int severity, String message) {
483     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
484     log(status);
485   }
486
487   public static void log(IStatus status) {
488     getDefault().getLog().log(status);
489   }
490
491   public static void log(Throwable e) {
492     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
493   }
494
495   /**
496    * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
497    * 
498    * @param wikiLocale
499    * @return
500    * @throws NoSuchMethodException
501    * @throws IllegalAccessException
502    * @throws ClassNotFoundException
503    * @throws InvocationTargetException
504    */
505   public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
506       ClassNotFoundException, InvocationTargetException {
507     String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
508     Class cls = Class.forName(className);
509     Method method = cls.getMethod("getInstance", new Class[0]);
510     return (IWikipedia) method.invoke(null, new Object[0]);
511   }
512
513   /**
514    * Returns the workspace instance.
515    */
516   public static IWorkspace getWorkspace() {
517     return ResourcesPlugin.getWorkspace();
518   }
519
520   // test code
521   //  public static void main(String[] args) {
522   //    try {
523   //      getWikiInstance("WikipediaEN");
524   //    } catch (NoSuchMethodException e) {
525   //      // TODO Auto-generated catch block
526   //      e.printStackTrace();
527   //    } catch (IllegalAccessException e) {
528   //      // TODO Auto-generated catch block
529   //      e.printStackTrace();
530   //    } catch (ClassNotFoundException e) {
531   //      // TODO Auto-generated catch block
532   //      e.printStackTrace();
533   //    } catch (InvocationTargetException e) {
534   //      // TODO Auto-generated catch block
535   //      e.printStackTrace();
536   //    }
537   //  }
538
539 }