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_TIMEOUT = "http.timeout";
151 public final static String HTTP_PROXYHOST = "http.proxyHost";
153 public final static String HTTP_PROXYPORT = "http.proxyPort";
155 public final static String HTTP_PROXYUSERNAME = "http.proxyUserName";
157 public final static String HTTP_PROXYPASSWORD = "http.proxyPassword";
160 * A named preference that controls whether the 'wrap words' feature is enabled.
162 * Value is of type <code>Boolean</code>.
165 public final static String EDITOR_WRAP_WORDS = "editor.wrap.words"; //$NON-NLS-1$
167 public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<configurations>"
168 + "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>"
169 + "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>"
170 + "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>"
172 + "<config name=\"Plog4U.de Download\" type-id=\"" + PLOG4U_DE_LOAD
173 + "\" url=\"http://www.plog4u.de/index.php/Spezial:Export\"/>" + "<config name=\"Plog4U.org Download\" type-id=\""
174 + PLOG4U_ORG_LOAD + "\" url=\"http://www.plog4u.org/index.php/Special:Export\"/>"
175 + "<config name=\"Plog4U.de Upload\" type-id=\"" + PLOG4U_DE_STORE + "\" url=\"http://www.plog4u.de/index.php\"/>"
176 + "<config name=\"Plog4U.org Upload\" type-id=\"" + PLOG4U_ORG_STORE + "\" url=\"http://www.plog4u.org/index.php\"/>"
178 + "<config name=\"WikipediaEN Download\" type-id=\"" + PREFIX_LOAD
179 + "WikipediaEN\" url=\"http://en.wikipedia.org/wiki/Special:Export\"/>" + "<config name=\"WikibooksEN Download\" type-id=\""
180 + PREFIX_LOAD + "WikibooksEN\" url=\"http://en.wikibooks.org/wiki/Special:Export\"/>"
181 + "<config name=\"WikipediaEN Upload\" type-id=\"" + PREFIX_STORE
182 + "WikipediaEN\" url=\"http://en.wikipedia.org/w/index.php\"/>" + "<config name=\"WikibooksEN Upload\" type-id=\""
183 + PREFIX_STORE + "WikibooksEN\" url=\"http://en.wikibooks.org/w/index.php\"/>"
184 + "<config name=\"Wikipedia-SQL\" type-id=\"Wikipedia SQL access\" user=\"root\" url=\"jdbc:mysql://localhost/wikidb\"/>"
185 + "</configurations>";
187 public static WikipediaDB fWikiDB = null;
189 private static ConfigurationManager manager;
192 // public static final String IMG_MONITOR_ON = "monitorOn";
194 // public static final String IMG_MONITOR_OFF = "monitorOff";
197 * Creates an image and places it in the image registry.
200 * the identifier for the image
202 * the base URL for the image
204 protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
205 // Delegate to the plugin instance to avoid concurrent class loading problems
206 plugin.privateCreateImageDescriptor(id, baseURL);
209 public static WikiEditorPlugin getDefault() {
214 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
217 * the identifier for the image to retrieve
218 * @return the image associated with the given ID
220 public static ImageDescriptor getImageDescriptor(String id) {
221 // Delegate to the plugin instance to avoid concurrent class loading problems
222 return getDefault().privateGetImageDescriptor(id);
226 * Convenience method to get an image descriptor for an extension
229 * the extension declaring the image
230 * @param subdirectoryAndFilename
231 * the path to the image
234 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
235 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
236 URL path = pluginDescriptor.getInstallURL();
237 URL fullPathString = null;
239 fullPathString = new URL(path, subdirectoryAndFilename);
240 return ImageDescriptor.createFromURL(fullPathString);
241 } catch (MalformedURLException e) {
246 public static String getResourceString(String key) {
247 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
249 return (bundle != null) ? bundle.getString(key) : key;
250 } catch (MissingResourceException e) {
256 * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
257 * If so, this display is returned. Otherwise the method returns the default display.
259 public static Display getStandardDisplay() {
260 Display display = Display.getCurrent();
261 if (display == null) {
262 display = Display.getDefault();
267 private ContributionContextTypeRegistry fContextTypeRegistry;
269 private ResourceBundle fResourceBundle;
271 private ContributionTemplateStore fTemplateStore;
273 private Hashtable imageDescriptors = new Hashtable(20);
275 public WikiEditorPlugin(IPluginDescriptor descriptor) {
279 for (int i = 0; i < PREDEFINED_TYPES.length; i++) {
280 CONFIGURATION_TYPES.add(PREDEFINED_TYPES[i]);
283 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
284 CONFIGURATION_TYPES.add(PREFIX_LOAD + PREDEFINED_WIKIS[i]);
286 for (int i = 0; i < PREDEFINED_WIKIS.length; i++) {
287 CONFIGURATION_TYPES.add(PREFIX_STORE + PREDEFINED_WIKIS[i]);
289 manager = ConfigurationManager.getInstance();
291 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
292 } catch (MissingResourceException x) {
293 fResourceBundle = null;
298 * Creates an image and places it in the image registry.
300 protected void createImageDescriptor(String id, URL baseURL) {
303 url = new URL(baseURL, ICON_PATH + id);
304 } catch (MalformedURLException e) {
306 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
307 imageDescriptors.put(id, desc);
310 public IWorkbenchPage getActivePage() {
311 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
313 return window.getActivePage();
317 public ContextTypeRegistry getContextTypeRegistry() {
318 if (fContextTypeRegistry == null) {
319 fContextTypeRegistry = new ContributionContextTypeRegistry();
320 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
322 return fContextTypeRegistry;
325 public Image getImage(String key) {
326 Image image = getImageRegistry().get(key);
328 ImageDescriptor d = getImageDescriptor(key);
329 image = d.createImage();
330 getImageRegistry().put(key, image);
335 public ResourceBundle getResourceBundle() {
336 return fResourceBundle;
339 public TemplateStore getTemplateStore() {
340 if (fTemplateStore == null) {
341 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
343 fTemplateStore.load();
344 } catch (IOException e) {
349 return fTemplateStore;
352 private static boolean isDefined(String test) {
353 if (test != null && test.length() > 0) {
354 for (int i = 0; i < test.length(); i++) {
355 if (!Character.isWhitespace(test.charAt(i))) {
366 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
368 protected void initializeDefaultPreferences(IPreferenceStore store) {
369 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
370 store.setDefault(CONSOLE_OUTPUT, "true");
372 String operatingSystem = Platform.getOS();
373 // operatingSystem ==> maxosx, linux, solaris, win32,...
375 InputStream is = getDefault().openStream(new Path("prefs/default_" + operatingSystem + ".properties"));
376 PropertyResourceBundle resourceBundle = new PropertyResourceBundle(is);
378 String key = EDITOR_WRAP_WORDS;
380 String wordWrap = resourceBundle.getString(key);
381 Boolean b = Boolean.valueOf(wordWrap);
382 if (b.booleanValue()) {
383 store.setDefault(key, "true");
385 store.setDefault(key, "false");
387 } catch (Exception e) {
388 store.setDefault(key, "true");
393 String timeout = resourceBundle.getString(key);
394 int val = Integer.parseInt(timeout);
400 store.setDefault(key, Integer.toString(val));
401 } catch (Exception e) {
402 store.setDefault(key, "30000"); // 30 seconds timeout
405 key = HTTP_PROXYHOST;
407 String proxyHost = resourceBundle.getString(key);
408 if (isDefined(proxyHost)) {
409 store.setDefault(key, proxyHost);
411 store.setDefault(key, System.getProperty(key, ""));
413 } catch (Exception e) {
414 store.setDefault(key, System.getProperty(key, ""));
417 key = HTTP_PROXYPORT;
419 String proxyPort = resourceBundle.getString(key);
420 if (isDefined(proxyPort)) {
421 // check for integer parse exception:
422 Integer.parseInt(proxyPort);
423 store.setDefault(key, proxyPort);
425 store.setDefault(key, System.getProperty(key, "80"));
427 } catch (Exception e) {
428 store.setDefault(key, System.getProperty(key, "80"));
431 key = HTTP_PROXYUSERNAME;
433 String proxyUserName = resourceBundle.getString(key);
434 if (isDefined(proxyUserName)) {
435 store.setDefault(key, proxyUserName);
437 store.setDefault(key, System.getProperty(key, ""));
439 } catch (Exception e) {
440 store.setDefault(key, System.getProperty(key, ""));
443 key = HTTP_PROXYPASSWORD;
445 String proxyPassWord = resourceBundle.getString(key);
446 if (isDefined(proxyPassWord)) {
447 store.setDefault(key, proxyPassWord);
449 store.setDefault(key, System.getProperty(key, ""));
451 } catch (Exception e) {
452 store.setDefault(key, System.getProperty(key, ""));
455 } catch (Exception e) {
461 * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
462 * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
464 private void initializeImages() {
465 URL baseURL = getDescriptor().getInstallURL();
468 createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
469 createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
470 createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
471 createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
472 createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
473 createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
474 createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
475 createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
479 public void log(String message) {
480 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
483 public void log(String message, Exception e) {
484 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
487 public void logAndReport(String title, String message, Exception e) {
489 reportError(title, message);
492 private void privateCreateImageDescriptor(String id, URL baseURL) {
495 url = new URL(baseURL, ICON_PATH + id);
496 } catch (MalformedURLException e) {
498 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
499 imageDescriptors.put(id, desc);
502 private ImageDescriptor privateGetImageDescriptor(String id) {
503 if (!imageDescriptors.containsKey(id)) {
504 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
505 createImageDescriptor(getDefault(), id, baseURL);
507 return (ImageDescriptor) imageDescriptors.get(id);
510 public void reportError(String title, String message) {
512 // Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
513 MessageDialog.openError(null, title, message);
514 } catch (RuntimeException e) {
515 log(e.getLocalizedMessage(), e);
519 // public void startup() throws CoreException {
524 * Returns the translated String found with the given key.
526 * @return java.lang.String
530 public static String getResource(String key) {
532 return Platform.getResourceString(getDefault().getBundle(), key);
533 } catch (Exception e) {
539 * Returns the translated String found with the given key, and formatted with the given object.
545 * @return java.lang.String
547 public static String getResource(String key, Object[] obj) {
549 return MessageFormat.format(getResource(key), obj);
550 } catch (Exception e) {
556 * Returns the translated String found with the given key, and formatted with the given object.
562 * @return java.lang.String
564 public static String getResource(String key, String s) {
566 return MessageFormat.format(getResource(key), new String[] { s });
567 } catch (Exception e) {
573 * Return a list of all the existing configurations.
575 * @return java.util.List
577 public static List getConfigurations() {
578 return manager.getConfigurations();
582 * Create a new monitor.
584 * @return working copy
586 public static IConfigurationWorkingCopy createConfiguration() {
587 return manager.createConfiguration();
590 public static ArrayList getTypes() {
591 return CONFIGURATION_TYPES;
594 public static void addType(String type) {
595 CONFIGURATION_TYPES.add(type);
599 * (non - Javadoc) Method declared in Plugin
601 public void start(BundleContext context) throws Exception {
602 super.start(context);
606 * @see org.eclipse.core.runtime.Plugin#stop
608 public void stop(BundleContext context) throws Exception {
610 if (fWikiDB != null) {
613 } catch (SQLException e) {
621 public static void log(int severity, String message) {
622 Status status = new Status(severity, PLUGIN_ID, IStatus.OK, message, null);
626 public static void log(IStatus status) {
627 getDefault().getLog().log(status);
630 public static void log(Throwable e) {
631 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, "PHPeclipsePlugin.internalErrorOccurred", e)); //$NON-NLS-1$
635 * Get a IWikpedia singleton instance through reflection (i.e. Method#invoke() )
639 * @throws NoSuchMethodException
640 * @throws IllegalAccessException
641 * @throws ClassNotFoundException
642 * @throws InvocationTargetException
644 public static IWikipedia getWikiInstance(String wikiLocale) throws NoSuchMethodException, IllegalAccessException,
645 ClassNotFoundException, InvocationTargetException {
646 String className = "net.sourceforge.phpeclipse.wiki.actions.mediawiki.config." + wikiLocale;
647 Class cls = Class.forName(className);
648 Method method = cls.getMethod("getInstance", new Class[0]);
649 return (IWikipedia) method.invoke(null, new Object[0]);
653 * Returns the workspace instance.
655 public static IWorkspace getWorkspace() {
656 return ResourcesPlugin.getWorkspace();
660 // public static void main(String[] args) {
662 // getWikiInstance("WikipediaEN");
663 // } catch (NoSuchMethodException e) {
664 // // TODO Auto-generated catch block
665 // e.printStackTrace();
666 // } catch (IllegalAccessException e) {
667 // // TODO Auto-generated catch block
668 // e.printStackTrace();
669 // } catch (ClassNotFoundException e) {
670 // // TODO Auto-generated catch block
671 // e.printStackTrace();
672 // } catch (InvocationTargetException e) {
673 // // TODO Auto-generated catch block
674 // e.printStackTrace();