1 package net.sourceforge.phpeclipse.wiki.editor;
3 import java.io.IOException;
4 import java.lang.reflect.InvocationTargetException;
5 import java.lang.reflect.Method;
6 import java.net.MalformedURLException;
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;
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;
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;
44 public class WikiEditorPlugin extends AbstractUIPlugin {
46 private static WikiEditorPlugin fgPlugin;
48 public final static String AUTOMATICALLY_CREATED = "<!-- automatically created -->";
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 ";
57 public static final String PREFIX_STORE = "Store ";
59 public static final String HTTP_QUERY = "HTTP Query";
61 // public static final String WIKIPEDIA_GET_TEXT = "Load WikipediaEN";
63 // public static final String WIKIPEDIA_SET_TEXT = "Store WikipediaEN";
64 public static final String WIKIPEDIA_SQL = "Wikipedia SQL access";
66 public static final String BLOG_A_WIKI = "Blog as Wiki Text";
68 public static final String BLOG_A_HTML = "Blog as HTML Text";
70 public static final String PLOG4U_DE_LOAD = PREFIX_LOAD + "Plog4UDE";
72 public static final String PLOG4U_DE_STORE = PREFIX_STORE + "Plog4UDE";
74 public static final String PLOG4U_ORG_LOAD = PREFIX_LOAD + "Plog4UORG";
76 public static final String PLOG4U_ORG_STORE = PREFIX_STORE + "Plog4UORG";
78 public static final String[] PREDEFINED_TYPES = {
88 public static final String[] PREDEFINED_WIKIS = {
114 public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
117 public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
119 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
121 public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
123 public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
125 public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
127 public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
129 public final static String LOCAL_CSS_URL = "__local_css_url";
131 public final static String EXPORT_CSS_URL = "__export_css_url";
133 public final static String PREF_STRING_CONFIGURATIONS = "__configurations3";
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\"/>"
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\"/>"
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>";
155 public static WikipediaDB fWikiDB = null;
157 private static ConfigurationManager manager;
160 // public static final String IMG_MONITOR_ON = "monitorOn";
162 // public static final String IMG_MONITOR_OFF = "monitorOff";
165 * Creates an image and places it in the image registry.
168 * the identifier for the image
170 * the base URL for the image
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);
177 public static WikiEditorPlugin getDefault() {
182 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
185 * the identifier for the image to retrieve
186 * @return the image associated with the given ID
188 public static ImageDescriptor getImageDescriptor(String id) {
189 // Delegate to the plugin instance to avoid concurrent class loading problems
190 return getDefault().privateGetImageDescriptor(id);
194 * Convenience method to get an image descriptor for an extension
197 * the extension declaring the image
198 * @param subdirectoryAndFilename
199 * the path to the image
202 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
203 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
204 URL path = pluginDescriptor.getInstallURL();
205 URL fullPathString = null;
207 fullPathString = new URL(path, subdirectoryAndFilename);
208 return ImageDescriptor.createFromURL(fullPathString);
209 } catch (MalformedURLException e) {
214 public static String getResourceString(String key) {
215 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
217 return (bundle != null) ? bundle.getString(key) : key;
218 } catch (MissingResourceException e) {
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.
227 public static Display getStandardDisplay() {
228 Display display = Display.getCurrent();
229 if (display == null) {
230 display = Display.getDefault();
235 private ContributionContextTypeRegistry fContextTypeRegistry;
237 private ResourceBundle fResourceBundle;
239 private ContributionTemplateStore fTemplateStore;
241 private Hashtable imageDescriptors = new Hashtable(20);
243 public WikiEditorPlugin(IPluginDescriptor descriptor) {
247 for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
248 CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
251 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
252 CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
254 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
255 CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
257 manager = ConfigurationManager.getInstance();
259 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
260 } catch (MissingResourceException x) {
261 fResourceBundle = null;
266 * Creates an image and places it in the image registry.
268 protected void createImageDescriptor(String id, URL baseURL) {
271 url = new URL(baseURL, ICON_PATH + id);
272 } catch (MalformedURLException e) {
274 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
275 imageDescriptors.put(id, desc);
278 public IWorkbenchPage getActivePage() {
279 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
281 return window.getActivePage();
285 public ContextTypeRegistry getContextTypeRegistry() {
286 if (fContextTypeRegistry == null) {
287 fContextTypeRegistry = new ContributionContextTypeRegistry();
288 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
290 return fContextTypeRegistry;
293 public Image getImage(String key) {
294 Image image = getImageRegistry().get(key);
296 ImageDescriptor d = getImageDescriptor(key);
297 image = d.createImage();
298 getImageRegistry().put(key, image);
303 public ResourceBundle getResourceBundle() {
304 return fResourceBundle;
307 public TemplateStore getTemplateStore() {
308 if (fTemplateStore == null) {
309 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
311 fTemplateStore.load();
312 } catch (IOException e) {
317 return fTemplateStore;
323 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
325 protected void initializeDefaultPreferences(IPreferenceStore store) {
326 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
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.
333 private void initializeImages() {
334 URL baseURL = getDescriptor().getInstallURL();
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$
348 public void log(String message) {
349 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
352 public void log(String message, Exception e) {
353 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
356 public void logAndReport(String title, String message, Exception e) {
358 reportError(title, message);
361 private void privateCreateImageDescriptor(String id, URL baseURL) {
364 url = new URL(baseURL, ICON_PATH + id);
365 } catch (MalformedURLException e) {
367 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
368 imageDescriptors.put(id, desc);
371 private ImageDescriptor privateGetImageDescriptor(String id) {
372 if (!imageDescriptors.containsKey(id)) {
373 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
374 createImageDescriptor(getDefault(), id, baseURL);
376 return (ImageDescriptor) imageDescriptors.get(id);
379 public void reportError(String title, String message) {
381 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
382 MessageDialog.openError(shell, title, message);
383 } catch (RuntimeException e) {
384 log(e.getLocalizedMessage(), e);
388 public void startup() throws CoreException {
393 * Returns the translated String found with the given key.
395 * @return java.lang.String
399 public static String getResource(String key) {
401 return Platform.getResourceString(getDefault().getBundle(), key);
402 } catch (Exception e) {
408 * Returns the translated String found with the given key, and formatted with the given object.
414 * @return java.lang.String
416 public static String getResource(String key, Object[] obj) {
418 return MessageFormat.format(getResource(key), obj);
419 } catch (Exception e) {
425 * Returns the translated String found with the given key, and formatted with the given object.
431 * @return java.lang.String
433 public static String getResource(String key, String s) {
435 return MessageFormat.format(getResource(key), new String[] { s });
436 } catch (Exception e) {
442 * Return a list of all the existing configurations.
444 * @return java.util.List
446 public static List getConfigurations() {
447 return manager.getConfigurations();
451 * Create a new monitor.
453 * @return working copy
455 public static IConfigurationWorkingCopy createConfiguration() {
456 return manager.createConfiguration();
459 public static ArrayList getTypes() {
460 return CONFIGURATION_TYPES;
463 public static void addType(String type) {
464 CONFIGURATION_TYPES.add(type);
470 * @see org.eclipse.core.runtime.Plugin#shutdown()
472 public void shutdown() throws CoreException {
473 if (fWikiDB != null) {
476 } catch (SQLException e) {
482 public static void log(int severity, String message) {
483 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
487 public static void log(IStatus status) {
488 getDefault().getLog().log(status);
491 public static void log(Throwable e) {
492 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
496 * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
500 * @throws NoSuchMethodException
501 * @throws IllegalAccessException
502 * @throws ClassNotFoundException
503 * @throws InvocationTargetException
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]);
514 * Returns the workspace instance.
516 public static IWorkspace getWorkspace() {
517 return ResourcesPlugin.getWorkspace();
521 // public static void main(String[] args) {
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();