1 package net.sourceforge.phpeclipse.wiki.editor;
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.lang.reflect.InvocationTargetException;
6 import java.lang.reflect.Method;
7 import java.net.MalformedURLException;
9 import java.sql.SQLException;
10 import java.text.MessageFormat;
11 import java.util.ArrayList;
12 import java.util.Hashtable;
13 import java.util.List;
14 import java.util.MissingResourceException;
15 import java.util.PropertyResourceBundle;
16 import java.util.ResourceBundle;
18 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
19 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
20 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
21 import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB;
23 import org.eclipse.core.resources.IFile;
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.resources.IWorkspace;
27 import org.eclipse.core.resources.ResourcesPlugin;
28 import org.eclipse.core.runtime.IAdapterFactory;
29 import org.eclipse.core.runtime.IAdapterManager;
30 import org.eclipse.core.runtime.IExtension;
31 import org.eclipse.core.runtime.IPluginDescriptor;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.Path;
34 import org.eclipse.core.runtime.Platform;
35 import org.eclipse.core.runtime.Status;
36 import org.eclipse.jface.dialogs.MessageDialog;
37 import org.eclipse.jface.preference.IPreferenceStore;
38 import org.eclipse.jface.resource.ImageDescriptor;
39 import org.eclipse.jface.text.templates.ContextTypeRegistry;
40 import org.eclipse.jface.text.templates.persistence.TemplateStore;
41 import org.eclipse.swt.graphics.Image;
42 import org.eclipse.swt.widgets.Display;
43 import org.eclipse.ui.IContributorResourceAdapter;
44 import org.eclipse.ui.IPersistableElement;
45 import org.eclipse.ui.IWorkbenchPage;
46 import org.eclipse.ui.IWorkbenchWindow;
47 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
48 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
49 import org.eclipse.ui.plugin.AbstractUIPlugin;
50 import org.eclipse.ui.views.properties.IPropertySource;
51 import org.osgi.framework.BundleContext;
53 public class WikiEditorPlugin extends AbstractUIPlugin {
55 private static WikiEditorPlugin fgPlugin;
57 public final static String AUTOMATICALLY_CREATED = "<!-- automatically created -->";
59 public final static String WP_EXTENSION = "wp";
61 public final static String XML_START_1 = "<?xml version=\"1.0\" encoding=\"";
63 public final static String XML_START_2 = "\" ?>\n" + "<mediawiki version=\"0.1\">\n";
65 public final static String XML_END = "\n</mediawiki>";
67 public static final String PREFIX_LOAD = "Load ";
69 public static final String PREFIX_STORE = "Store ";
71 public static final String HTTP_QUERY = "HTTP Query";
73 // public static final String WIKIPEDIA_GET_TEXT = "Load WikipediaEN";
75 // public static final String WIKIPEDIA_SET_TEXT = "Store WikipediaEN";
76 public static final String WIKIPEDIA_SQL = "Wikipedia SQL access";
78 public static final String BLOG_A_WIKI = "Blog as Wiki Text";
80 public static final String BLOG_A_HTML = "Blog as HTML Text";
82 public static final String PLOG4U_DE_LOAD = PREFIX_LOAD + "Plog4UDE";
84 public static final String PLOG4U_DE_STORE = PREFIX_STORE + "Plog4UDE";
86 public static final String PLOG4U_ORG_LOAD = PREFIX_LOAD + "Plog4UORG";
88 public static final String PLOG4U_ORG_STORE = PREFIX_STORE + "Plog4UORG";
90 public static final String[] PREDEFINED_TYPES = {
100 public static final String[] PREDEFINED_WIKIS = {
126 public static final ArrayList CONFIGURATION_TYPES = new ArrayList();
129 public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
131 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
133 public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
135 public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
137 public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
139 public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
141 public final static String LOCAL_CSS_URL = "__local_css_url";
143 public final static String EXPORT_CSS_URL = "__export_css_url";
145 public final static String PREF_STRING_CONFIGURATIONS = "__configurations4";
147 public final static String CONSOLE_OUTPUT = "__console_output";
149 public final static String HTTP_GET_THROTTLE = "http.get.throttle";
151 public final static String HTTP_PUT_THROTTLE = "http.put.throttle";
153 public final static String HTTP_TIMEOUT = "http.timeout";
155 public final static String HTTP_PROXYHOST = "http.proxyHost";
157 public final static String HTTP_PROXYPORT = "http.proxyPort";
159 public final static String HTTP_PROXYUSERNAME = "http.proxyUserName";
161 public final static String HTTP_PROXYPASSWORD = "http.proxyPassword";
164 * A named preference that controls whether the 'wrap words' feature is enabled.
166 * Value is of type <code>Boolean</code>.
169 public final static String EDITOR_WRAP_WORDS = "editor.wrap.words"; //$NON-NLS-1$
171 public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
172 + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
173 + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
174 + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
176 + "<config name=\"Plog4U.de Download\" type-id=\"" + PLOG4U_DE_LOAD
177 + "\" url=\"http://www.plog4u.de/index.php/Spezial:Export\"/>" + "<config name=\"Plog4U.org Download\" type-id=\""
178 + PLOG4U_ORG_LOAD + "\" url=\"http://www.plog4u.org/index.php/Special:Export\"/>"
179 + "<config name=\"Plog4U.de Upload\" type-id=\"" + PLOG4U_DE_STORE + "\" url=\"http://www.plog4u.de/index.php\"/>"
180 + "<config name=\"Plog4U.org Upload\" type-id=\"" + PLOG4U_ORG_STORE + "\" url=\"http://www.plog4u.org/index.php\"/>"
182 + "<config name=\"WikipediaEN Download\" type-id=\"" + PREFIX_LOAD
183 + "WikipediaEN\" url=\"http://en.wikipedia.org/wiki/Special:Export\"/>" + "<config name=\"WikibooksEN Download\" type-id=\""
184 + PREFIX_LOAD + "WikibooksEN\" url=\"http://en.wikibooks.org/wiki/Special:Export\"/>"
185 + "<config name=\"WikipediaEN Upload\" type-id=\"" + PREFIX_STORE
186 + "WikipediaEN\" url=\"http://en.wikipedia.org/w/index.php\"/>" + "<config name=\"WikibooksEN Upload\" type-id=\""
187 + PREFIX_STORE + "WikibooksEN\" url=\"http://en.wikibooks.org/w/index.php\"/>"
188 + "<config name=\"Wikipedia-SQL\" type-id=\"Wikipedia SQL access\" user=\"root\" url=\"jdbc:mysql://localhost/wikidb\"/>"
189 + "</configurations>";
191 public static WikipediaDB fWikiDB = null;
193 private static ConfigurationManager manager;
196 // public static final String IMG_MONITOR_ON = "monitorOn";
198 // public static final String IMG_MONITOR_OFF = "monitorOff";
201 * Creates an image and places it in the image registry.
204 * the identifier for the image
206 * the base URL for the image
208 protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
209 // Delegate to the plugin instance to avoid concurrent class loading problems
210 plugin.privateCreateImageDescriptor(id, baseURL);
213 public static WikiEditorPlugin getDefault() {
218 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
221 * the identifier for the image to retrieve
222 * @return the image associated with the given ID
224 public static ImageDescriptor getImageDescriptor(String id) {
225 // Delegate to the plugin instance to avoid concurrent class loading problems
226 return getDefault().privateGetImageDescriptor(id);
230 * Convenience method to get an image descriptor for an extension
233 * the extension declaring the image
234 * @param subdirectoryAndFilename
235 * the path to the image
238 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
239 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
240 URL path = pluginDescriptor.getInstallURL();
241 URL fullPathString = null;
243 fullPathString = new URL(path, subdirectoryAndFilename);
244 return ImageDescriptor.createFromURL(fullPathString);
245 } catch (MalformedURLException e) {
250 public static String getResourceString(String key) {
251 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
253 return (bundle != null) ? bundle.getString(key) : key;
254 } catch (MissingResourceException e) {
260 * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
261 * If so, this display is returned. Otherwise the method returns the default display.
263 public static Display getStandardDisplay() {
264 Display display = Display.getCurrent();
265 if (display == null) {
266 display = Display.getDefault();
271 private ContributionContextTypeRegistry fContextTypeRegistry;
273 private ResourceBundle fResourceBundle;
275 private ContributionTemplateStore fTemplateStore;
277 private Hashtable imageDescriptors = new Hashtable(20);
279 public WikiEditorPlugin(IPluginDescriptor descriptor) {
283 for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
284 CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
287 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
288 CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
290 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
291 CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
293 manager = ConfigurationManager.getInstance();
295 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
296 } catch (MissingResourceException x) {
297 fResourceBundle = null;
302 * Creates an image and places it in the image registry.
304 protected void createImageDescriptor(String id, URL baseURL) {
307 url = new URL(baseURL, ICON_PATH + id);
308 } catch (MalformedURLException e) {
310 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
311 imageDescriptors.put(id, desc);
314 public IWorkbenchPage getActivePage() {
315 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
317 return window.getActivePage();
321 public ContextTypeRegistry getContextTypeRegistry() {
322 if (fContextTypeRegistry == null) {
323 fContextTypeRegistry = new ContributionContextTypeRegistry();
324 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
326 return fContextTypeRegistry;
329 public Image getImage(String key) {
330 Image image = getImageRegistry().get(key);
332 ImageDescriptor d = getImageDescriptor(key);
333 image = d.createImage();
334 getImageRegistry().put(key, image);
339 public ResourceBundle getResourceBundle() {
340 return fResourceBundle;
343 public TemplateStore getTemplateStore() {
344 if (fTemplateStore == null) {
345 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
347 fTemplateStore.load();
348 } catch (IOException e) {
353 return fTemplateStore;
356 private static boolean isDefined(String test) {
357 if (test != null && test.length() > 0) {
358 for (int i = 0; i < test.length(); i++) {
359 if (!Character.isWhitespace(test.charAt(i))) {
370 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
372 protected void initializeDefaultPreferences(IPreferenceStore store) {
373 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
374 store.setDefault(CONSOLE_OUTPUT, "true");
376 String operatingSystem = Platform.getOS();
377 // operatingSystem ==> maxosx, linux, solaris, win32,...
379 InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties"));
380 PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is);
382 String key = EDITOR_WRAP_WORDS;
384 String wordWrap = resourceBundle.getString(key);
385 Boolean b = Boolean.valueOf(wordWrap);
386 if (b.booleanValue()) {
387 store.setDefault(key, "true");
389 store.setDefault(key, "false");
391 } catch (Exception e) {
392 store.setDefault(key, "false");
395 key = HTTP_GET_THROTTLE;
397 String timeout = resourceBundle.getString(key);
398 int val = Integer.parseInt(timeout);
404 store.setDefault(key, Integer.toString(val));
405 } catch (Exception e) {
406 store.setDefault(key, "1000"); // 1 second
409 key = HTTP_PUT_THROTTLE;
411 String timeout = resourceBundle.getString(key);
412 int val = Integer.parseInt(timeout);
418 store.setDefault(key, Integer.toString(val));
419 } catch (Exception e) {
420 store.setDefault(key, "5000"); // 5 seconds
425 String timeout = resourceBundle.getString(key);
426 int val = Integer.parseInt(timeout);
432 store.setDefault(key, Integer.toString(val));
433 } catch (Exception e) {
434 store.setDefault(key, "30000"); // 30 seconds timeout
437 key = HTTP_PROXYHOST;
439 String proxyHost = resourceBundle.getString(key);
440 if (isDefined(proxyHost)) {
441 store.setDefault(key, proxyHost);
443 store.setDefault(key, System.getProperty(key, ""));
445 } catch (Exception e) {
446 store.setDefault(key, System.getProperty(key, ""));
449 key = HTTP_PROXYPORT;
451 String proxyPort = resourceBundle.getString(key);
452 if (isDefined(proxyPort)) {
453 // check for integer parse exception:
454 Integer.parseInt(proxyPort);
455 store.setDefault(key, proxyPort);
457 store.setDefault(key, System.getProperty(key, "80"));
459 } catch (Exception e) {
460 store.setDefault(key, System.getProperty(key, "80"));
463 key = HTTP_PROXYUSERNAME;
465 String proxyUserName = resourceBundle.getString(key);
466 if (isDefined(proxyUserName)) {
467 store.setDefault(key, proxyUserName);
469 store.setDefault(key, System.getProperty(key, ""));
471 } catch (Exception e) {
472 store.setDefault(key, System.getProperty(key, ""));
475 key = HTTP_PROXYPASSWORD;
477 String proxyPassWord = resourceBundle.getString(key);
478 if (isDefined(proxyPassWord)) {
479 store.setDefault(key, proxyPassWord);
481 store.setDefault(key, System.getProperty(key, ""));
483 } catch (Exception e) {
484 store.setDefault(key, System.getProperty(key, ""));
487 } catch (Exception e) {
493 * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
494 * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
496 private void initializeImages() {
497 URL baseURL = getDescriptor().getInstallURL();
500 createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
501 createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
502 createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
503 createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
504 createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
505 createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
506 createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
507 createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
511 public void log(String message) {
512 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
515 public void log(String message, Exception e) {
516 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
519 public void logAndReport(String title, String message, Exception e) {
521 reportError(title, message);
524 private void privateCreateImageDescriptor(String id, URL baseURL) {
527 url = new URL(baseURL, ICON_PATH + id);
528 } catch (MalformedURLException e) {
530 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
531 imageDescriptors.put(id, desc);
534 private ImageDescriptor privateGetImageDescriptor(String id) {
535 if (!imageDescriptors.containsKey(id)) {
536 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
537 createImageDescriptor(getDefault(), id, baseURL);
539 return (ImageDescriptor) imageDescriptors.get(id);
542 public void reportError(String title, String message) {
544 // Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
545 MessageDialog.openError(null, title, message);
546 } catch (RuntimeException e) {
547 log(e.getLocalizedMessage(), e);
551 // public void startup() throws CoreException {
556 * Returns the translated String found with the given key.
558 * @return java.lang.String
562 public static String getResource(String key) {
564 return Platform.getResourceString(getDefault().getBundle(), key);
565 } catch (Exception e) {
571 * Returns the translated String found with the given key, and formatted with the given object.
577 * @return java.lang.String
579 public static String getResource(String key, Object[] obj) {
581 return MessageFormat.format(getResource(key), obj);
582 } catch (Exception e) {
588 * Returns the translated String found with the given key, and formatted with the given object.
594 * @return java.lang.String
596 public static String getResource(String key, String s) {
598 return MessageFormat.format(getResource(key), new String[] { s });
599 } catch (Exception e) {
605 * Return a list of all the existing configurations.
607 * @return java.util.List
609 public static List getConfigurations() {
610 return manager.getConfigurations();
614 * Create a new monitor.
616 * @return working copy
618 public static IConfigurationWorkingCopy createConfiguration() {
619 return manager.createConfiguration();
622 public static ArrayList getTypes() {
623 return CONFIGURATION_TYPES;
626 public static void addType(String type) {
627 CONFIGURATION_TYPES.add(type);
631 * (non - Javadoc) Method declared in Plugin
633 public void start(BundleContext context) throws Exception {
634 super.start(context);
638 * @see org.eclipse.core.runtime.Plugin#stop
640 public void stop(BundleContext context) throws Exception {
642 if (fWikiDB != null) {
645 } catch (SQLException e) {
653 public static void log(int severity, String message) {
654 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
658 public static void log(IStatus status) {
659 getDefault().getLog().log(status);
662 public static void log(Throwable e) {
663 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
667 * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
671 * @throws NoSuchMethodException
672 * @throws IllegalAccessException
673 * @throws ClassNotFoundException
674 * @throws InvocationTargetException
676 public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
677 ClassNotFoundException, InvocationTargetException {
678 String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
679 Class cls = Class.forName(className);
680 Method method = cls.getMethod("getInstance", new Class[0]);
681 return (IWikipedia) method.invoke(null, new Object[0]);
685 * Returns the workspace instance.
687 public static IWorkspace getWorkspace() {
688 return ResourcesPlugin.getWorkspace();
692 // public static void main(String[] args) {
694 // getWikiInstance("WikipediaEN");
695 // } catch (NoSuchMethodException e) {
696 // // TODO Auto-generated catch block
697 // e.printStackTrace();
698 // } catch (IllegalAccessException e) {
699 // // TODO Auto-generated catch block
700 // e.printStackTrace();
701 // } catch (ClassNotFoundException e) {
702 // // TODO Auto-generated catch block
703 // e.printStackTrace();
704 // } catch (InvocationTargetException e) {
705 // // TODO Auto-generated catch block
706 // e.printStackTrace();