RefreshJob loads max 10 articles at a time; there's a delay of 1 second for the next...
[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 = "__configurations4";
134   
135   public final static String CONSOLE_OUTPUT = "__console_output";
136
137   public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
138       + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
139       + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
140       + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
141
142       + "<config name=\"Plog4U.de Download\" type-id=\"" + PLOG4U_DE_LOAD
143       + "\" url=\"http://www.plog4u.de/index.php/Spezial:Export\"/>" + "<config name=\"Plog4U.org Download\" type-id=\""
144       + PLOG4U_ORG_LOAD + "\" url=\"http://www.plog4u.org/index.php/Special:Export\"/>"
145       + "<config name=\"Plog4U.de Upload\" type-id=\"" + PLOG4U_DE_STORE + "\" url=\"http://www.plog4u.de/index.php\"/>"
146       + "<config name=\"Plog4U.org Upload\" type-id=\"" + PLOG4U_ORG_STORE + "\" url=\"http://www.plog4u.org/index.php\"/>"
147
148       + "<config name=\"WikipediaEN Download\" type-id=\"" + PREFIX_LOAD
149       + "WikipediaEN\" url=\"http://en.wikipedia.org/wiki/Special:Export\"/>" + "<config name=\"WikibooksEN Download\" type-id=\""
150       + PREFIX_LOAD + "WikibooksEN\" url=\"http://en.wikibooks.org/wiki/Special:Export\"/>"
151       + "<config name=\"WikipediaEN Upload\" type-id=\"" + PREFIX_STORE
152       + "WikipediaEN\" url=\"http://en.wikipedia.org/w/index.php\"/>" + "<config name=\"WikibooksEN Upload\" type-id=\""
153       + PREFIX_STORE + "WikibooksEN\" url=\"http://en.wikibooks.org/w/index.php\"/>"
154       + "<config name=\"Wikipedia-SQL\" type-id=\"Wikipedia SQL access\" user=\"root\" url=\"jdbc:mysql://localhost/wikidb\"/>"
155       + "</configurations>";
156
157   public static WikipediaDB fWikiDB = null;
158
159   private static ConfigurationManager manager;
160
161   //
162   //  public static final String IMG_MONITOR_ON = "monitorOn";
163   //
164   //  public static final String IMG_MONITOR_OFF = "monitorOff";
165
166   /**
167    * Creates an image and places it in the image registry.
168    * 
169    * @param id
170    *          the identifier for the image
171    * @param baseURL
172    *          the base URL for the image
173    */
174   protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
175     // Delegate to the plugin instance to avoid concurrent class loading problems
176     plugin.privateCreateImageDescriptor(id, baseURL);
177   }
178
179   public static WikiEditorPlugin getDefault() {
180     return fgPlugin;
181   }
182
183   /**
184    * Returns the image descriptor for the given image ID. Returns null if there is no such image.
185    * 
186    * @param id
187    *          the identifier for the image to retrieve
188    * @return the image associated with the given ID
189    */
190   public static ImageDescriptor getImageDescriptor(String id) {
191     // Delegate to the plugin instance to avoid concurrent class loading problems
192     return getDefault().privateGetImageDescriptor(id);
193   }
194
195   /**
196    * Convenience method to get an image descriptor for an extension
197    * 
198    * @param extension
199    *          the extension declaring the image
200    * @param subdirectoryAndFilename
201    *          the path to the image
202    * @return the image
203    */
204   public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
205     IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
206     URL path = pluginDescriptor.getInstallURL();
207     URL fullPathString = null;
208     try {
209       fullPathString = new URL(path, subdirectoryAndFilename);
210       return ImageDescriptor.createFromURL(fullPathString);
211     } catch (MalformedURLException e) {
212     }
213     return null;
214   }
215
216   public static String getResourceString(String key) {
217     ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
218     try {
219       return (bundle != null) ? bundle.getString(key) : key;
220     } catch (MissingResourceException e) {
221       return key;
222     }
223   }
224
225   /**
226    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
227    * If so, this display is returned. Otherwise the method returns the default display.
228    */
229   public static Display getStandardDisplay() {
230     Display display = Display.getCurrent();
231     if (display == null) {
232       display = Display.getDefault();
233     }
234     return display;
235   }
236
237   private ContributionContextTypeRegistry fContextTypeRegistry;
238
239   private ResourceBundle fResourceBundle;
240
241   private ContributionTemplateStore fTemplateStore;
242
243   private Hashtable imageDescriptors = new Hashtable(20);
244
245   public WikiEditorPlugin(IPluginDescriptor descriptor) {
246     super(descriptor);
247     initializeImages();
248     fgPlugin = this;
249     for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
250       CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
251     }
252
253     for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
254       CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
255     }
256     for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
257       CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
258     }
259     manager = ConfigurationManager.getInstance();
260     try {
261       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
262     } catch (MissingResourceException x) {
263       fResourceBundle = null;
264     }
265   }
266
267   /**
268    * Creates an image and places it in the image registry.
269    */
270   protected void createImageDescriptor(String id, URL baseURL) {
271     URL url = null;
272     try {
273       url = new URL(baseURL, ICON_PATH + id);
274     } catch (MalformedURLException e) {
275     }
276     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
277     imageDescriptors.put(id, desc);
278   }
279
280   public IWorkbenchPage getActivePage() {
281     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
282     if (window != null)
283       return window.getActivePage();
284     return null;
285   }
286
287   public ContextTypeRegistry getContextTypeRegistry() {
288     if (fContextTypeRegistry == null) {
289       fContextTypeRegistry = new ContributionContextTypeRegistry();
290       fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
291     }
292     return fContextTypeRegistry;
293   }
294
295   public Image getImage(String key) {
296     Image image = getImageRegistry().get(key);
297     if (image == null) {
298       ImageDescriptor d = getImageDescriptor(key);
299       image = d.createImage();
300       getImageRegistry().put(key, image);
301     }
302     return image;
303   }
304
305   public ResourceBundle getResourceBundle() {
306     return fResourceBundle;
307   }
308
309   public TemplateStore getTemplateStore() {
310     if (fTemplateStore == null) {
311       fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
312       try {
313         fTemplateStore.load();
314       } catch (IOException e) {
315         e.printStackTrace();
316       }
317     }
318
319     return fTemplateStore;
320   }
321
322   /*
323    * (non-Javadoc)
324    * 
325    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
326    */
327   protected void initializeDefaultPreferences(IPreferenceStore store) {
328     store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
329     store.setDefault(CONSOLE_OUTPUT, "true");
330   }
331
332   /*
333    * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
334    * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
335    */
336   private void initializeImages() {
337     URL baseURL = getDescriptor().getInstallURL();
338
339     //  special
340     createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
341     createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
342     createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
343     createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
344     createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
345     createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
346     createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
347     createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
348
349   }
350
351   public void log(String message) {
352     getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
353   }
354
355   public void log(String message, Exception e) {
356     getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
357   }
358
359   public void logAndReport(String title, String message, Exception e) {
360     log(message, e);
361     reportError(title, message);
362   }
363
364   private void privateCreateImageDescriptor(String id, URL baseURL) {
365     URL url = null;
366     try {
367       url = new URL(baseURL, ICON_PATH + id);
368     } catch (MalformedURLException e) {
369     }
370     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
371     imageDescriptors.put(id, desc);
372   }
373
374   private ImageDescriptor privateGetImageDescriptor(String id) {
375     if (!imageDescriptors.containsKey(id)) {
376       URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
377       createImageDescriptor(getDefault(), id, baseURL);
378     }
379     return (ImageDescriptor) imageDescriptors.get(id);
380   }
381
382   public void reportError(String title, String message) {
383     try {
384 //      Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
385       MessageDialog.openError(null, title, message);
386     } catch (RuntimeException e) {
387       log(e.getLocalizedMessage(), e);
388     }
389   }
390
391   public void startup() throws CoreException {
392     super.startup();
393   }
394
395   /**
396    * Returns the translated String found with the given key.
397    * 
398    * @return java.lang.String
399    * @param key
400    *          java.lang.String
401    */
402   public static String getResource(String key) {
403     try {
404       return Platform.getResourceString(getDefault().getBundle(), key);
405     } catch (Exception e) {
406       return key;
407     }
408   }
409
410   /**
411    * Returns the translated String found with the given key, and formatted with the given object.
412    * 
413    * @param key
414    *          java.lang.String
415    * @param obj
416    *          java.lang.Object[]
417    * @return java.lang.String
418    */
419   public static String getResource(String key, Object[] obj) {
420     try {
421       return MessageFormat.format(getResource(key), obj);
422     } catch (Exception e) {
423       return key;
424     }
425   }
426
427   /**
428    * Returns the translated String found with the given key, and formatted with the given object.
429    * 
430    * @param key
431    *          java.lang.String
432    * @param s
433    *          java.lang.String
434    * @return java.lang.String
435    */
436   public static String getResource(String key, String s) {
437     try {
438       return MessageFormat.format(getResource(key), new String[] { s });
439     } catch (Exception e) {
440       return key;
441     }
442   }
443
444   /**
445    * Return a list of all the existing configurations.
446    * 
447    * @return java.util.List
448    */
449   public static List getConfigurations() {
450     return manager.getConfigurations();
451   }
452
453   /**
454    * Create a new monitor.
455    * 
456    * @return working copy
457    */
458   public static IConfigurationWorkingCopy createConfiguration() {
459     return manager.createConfiguration();
460   }
461
462   public static ArrayList getTypes() {
463     return CONFIGURATION_TYPES;
464   }
465
466   public static void addType(String type) {
467     CONFIGURATION_TYPES.add(type);
468   }
469
470   /*
471    * (non-Javadoc)
472    * 
473    * @see org.eclipse.core.runtime.Plugin#shutdown()
474    */
475   public void shutdown() throws CoreException {
476     if (fWikiDB != null) {
477       try {
478         fWikiDB.shutdown();
479       } catch (SQLException e) {
480       }
481     }
482     super.shutdown();
483   }
484
485   public static void log(int severity, String message) {
486     Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
487     log(status);
488   }
489
490   public static void log(IStatus status) {
491     getDefault().getLog().log(status);
492   }
493
494   public static void log(Throwable e) {
495     log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
496   }
497
498   /**
499    * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
500    * 
501    * @param wikiLocale
502    * @return
503    * @throws NoSuchMethodException
504    * @throws IllegalAccessException
505    * @throws ClassNotFoundException
506    * @throws InvocationTargetException
507    */
508   public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
509       ClassNotFoundException, InvocationTargetException {
510     String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
511     Class cls = Class.forName(className);
512     Method method = cls.getMethod("getInstance", new Class[0]);
513     return (IWikipedia) method.invoke(null, new Object[0]);
514   }
515
516   /**
517    * Returns the workspace instance.
518    */
519   public static IWorkspace getWorkspace() {
520     return ResourcesPlugin.getWorkspace();
521   }
522
523   // test code
524   //  public static void main(String[] args) {
525   //    try {
526   //      getWikiInstance("WikipediaEN");
527   //    } catch (NoSuchMethodException e) {
528   //      // TODO Auto-generated catch block
529   //      e.printStackTrace();
530   //    } catch (IllegalAccessException e) {
531   //      // TODO Auto-generated catch block
532   //      e.printStackTrace();
533   //    } catch (ClassNotFoundException e) {
534   //      // TODO Auto-generated catch block
535   //      e.printStackTrace();
536   //    } catch (InvocationTargetException e) {
537   //      // TODO Auto-generated catch block
538   //      e.printStackTrace();
539   //    }
540   //  }
541
542 }