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 = "__configurations4";
135 public final static String CONSOLE_OUTPUT = "__console_output";
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\"/>"
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\"/>"
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>";
157 public static WikipediaDB fWikiDB = null;
159 private static ConfigurationManager manager;
162 // public static final String IMG_MONITOR_ON = "monitorOn";
164 // public static final String IMG_MONITOR_OFF = "monitorOff";
167 * Creates an image and places it in the image registry.
170 * the identifier for the image
172 * the base URL for the image
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);
179 public static WikiEditorPlugin getDefault() {
184 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
187 * the identifier for the image to retrieve
188 * @return the image associated with the given ID
190 public static ImageDescriptor getImageDescriptor(String id) {
191 // Delegate to the plugin instance to avoid concurrent class loading problems
192 return getDefault().privateGetImageDescriptor(id);
196 * Convenience method to get an image descriptor for an extension
199 * the extension declaring the image
200 * @param subdirectoryAndFilename
201 * the path to the image
204 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
205 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
206 URL path = pluginDescriptor.getInstallURL();
207 URL fullPathString = null;
209 fullPathString = new URL(path, subdirectoryAndFilename);
210 return ImageDescriptor.createFromURL(fullPathString);
211 } catch (MalformedURLException e) {
216 public static String getResourceString(String key) {
217 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
219 return (bundle != null) ? bundle.getString(key) : key;
220 } catch (MissingResourceException e) {
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.
229 public static Display getStandardDisplay() {
230 Display display = Display.getCurrent();
231 if (display == null) {
232 display = Display.getDefault();
237 private ContributionContextTypeRegistry fContextTypeRegistry;
239 private ResourceBundle fResourceBundle;
241 private ContributionTemplateStore fTemplateStore;
243 private Hashtable imageDescriptors = new Hashtable(20);
245 public WikiEditorPlugin(IPluginDescriptor descriptor) {
249 for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
250 CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
253 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
254 CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
256 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
257 CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
259 manager = ConfigurationManager.getInstance();
261 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
262 } catch (MissingResourceException x) {
263 fResourceBundle = null;
268 * Creates an image and places it in the image registry.
270 protected void createImageDescriptor(String id, URL baseURL) {
273 url = new URL(baseURL, ICON_PATH + id);
274 } catch (MalformedURLException e) {
276 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
277 imageDescriptors.put(id, desc);
280 public IWorkbenchPage getActivePage() {
281 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
283 return window.getActivePage();
287 public ContextTypeRegistry getContextTypeRegistry() {
288 if (fContextTypeRegistry == null) {
289 fContextTypeRegistry = new ContributionContextTypeRegistry();
290 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
292 return fContextTypeRegistry;
295 public Image getImage(String key) {
296 Image image = getImageRegistry().get(key);
298 ImageDescriptor d = getImageDescriptor(key);
299 image = d.createImage();
300 getImageRegistry().put(key, image);
305 public ResourceBundle getResourceBundle() {
306 return fResourceBundle;
309 public TemplateStore getTemplateStore() {
310 if (fTemplateStore == null) {
311 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
313 fTemplateStore.load();
314 } catch (IOException e) {
319 return fTemplateStore;
325 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
327 protected void initializeDefaultPreferences(IPreferenceStore store) {
328 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
329 store.setDefault(CONSOLE_OUTPUT, "true");
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.
336 private void initializeImages() {
337 URL baseURL = getDescriptor().getInstallURL();
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$
351 public void log(String message) {
352 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
355 public void log(String message, Exception e) {
356 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
359 public void logAndReport(String title, String message, Exception e) {
361 reportError(title, message);
364 private void privateCreateImageDescriptor(String id, URL baseURL) {
367 url = new URL(baseURL, ICON_PATH + id);
368 } catch (MalformedURLException e) {
370 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
371 imageDescriptors.put(id, desc);
374 private ImageDescriptor privateGetImageDescriptor(String id) {
375 if (!imageDescriptors.containsKey(id)) {
376 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
377 createImageDescriptor(getDefault(), id, baseURL);
379 return (ImageDescriptor) imageDescriptors.get(id);
382 public void reportError(String title, String message) {
384 // Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
385 MessageDialog.openError(null, title, message);
386 } catch (RuntimeException e) {
387 log(e.getLocalizedMessage(), e);
391 public void startup() throws CoreException {
396 * Returns the translated String found with the given key.
398 * @return java.lang.String
402 public static String getResource(String key) {
404 return Platform.getResourceString(getDefault().getBundle(), key);
405 } catch (Exception e) {
411 * Returns the translated String found with the given key, and formatted with the given object.
417 * @return java.lang.String
419 public static String getResource(String key, Object[] obj) {
421 return MessageFormat.format(getResource(key), obj);
422 } catch (Exception e) {
428 * Returns the translated String found with the given key, and formatted with the given object.
434 * @return java.lang.String
436 public static String getResource(String key, String s) {
438 return MessageFormat.format(getResource(key), new String[] { s });
439 } catch (Exception e) {
445 * Return a list of all the existing configurations.
447 * @return java.util.List
449 public static List getConfigurations() {
450 return manager.getConfigurations();
454 * Create a new monitor.
456 * @return working copy
458 public static IConfigurationWorkingCopy createConfiguration() {
459 return manager.createConfiguration();
462 public static ArrayList getTypes() {
463 return CONFIGURATION_TYPES;
466 public static void addType(String type) {
467 CONFIGURATION_TYPES.add(type);
473 * @see org.eclipse.core.runtime.Plugin#shutdown()
475 public void shutdown() throws CoreException {
476 if (fWikiDB != null) {
479 } catch (SQLException e) {
485 public static void log(int severity, String message) {
486 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
490 public static void log(IStatus status) {
491 getDefault().getLog().log(status);
494 public static void log(Throwable e) {
495 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
499 * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
503 * @throws NoSuchMethodException
504 * @throws IllegalAccessException
505 * @throws ClassNotFoundException
506 * @throws InvocationTargetException
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]);
517 * Returns the workspace instance.
519 public static IWorkspace getWorkspace() {
520 return ResourcesPlugin.getWorkspace();
524 // public static void main(String[] args) {
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();