1 package net.sourceforge.phpeclipse.wiki.editor;
3 import java.io.IOException;
4 import java.net.MalformedURLException;
6 import java.text.MessageFormat;
7 import java.util.Hashtable;
9 import java.util.MissingResourceException;
10 import java.util.ResourceBundle;
12 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
13 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IExtension;
17 import org.eclipse.core.runtime.IPluginDescriptor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Platform;
20 import org.eclipse.core.runtime.Status;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.text.templates.ContextTypeRegistry;
25 import org.eclipse.jface.text.templates.persistence.TemplateStore;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IWorkbenchPage;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.PlatformUI;
32 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
33 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
34 import org.eclipse.ui.plugin.AbstractUIPlugin;
36 public class WikiEditorPlugin extends AbstractUIPlugin {
38 private static WikiEditorPlugin fgPlugin;
39 public static final String HTTP_QUERY = "HTTP Query";
40 public static final String WIKIPEDIA_GET_TEXT = "Wikipedia-Load Text";
41 public static final String WEBLOG_API_SEND = "MetaWeblog API-Post";
43 public static final String[] CONFIGURATION_TYPES = {
49 public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
51 public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
53 public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
55 public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
57 public final static String LOCAL_TEMPLATE_FILE_NAME = "__local_template_file_name";
58 public final static String EXPORT_TEMPLATE_FILE_NAME = "__export_template_file_name";
60 public final static String PREF_STRING_CONFIGURATIONS ="configurations";
61 public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
63 "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>" +
64 "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>" +
65 "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>" +
66 "<config name=\"Wikipedia-en\" type-id=\"Wikipedia-Load Text\" url=\"http://en.wikipedia.org/w/wiki.phtml?title=$text.wikiname&action=edit\"/>" +
67 "<config name=\"Wikibooks-en\" type-id=\"Wikipedia-Load Text\" url=\"http://en.wikibooks.org/w/wiki.phtml?title=$text.wikiname&action=edit\"/>" +
69 private static ConfigurationManager manager;
71 // public static final String IMG_MONITOR_ON = "monitorOn";
73 // public static final String IMG_MONITOR_OFF = "monitorOff";
76 * Creates an image and places it in the image registry.
79 * the identifier for the image
81 * the base URL for the image
83 protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
84 // Delegate to the plugin instance to avoid concurrent class loading problems
85 plugin.privateCreateImageDescriptor(id, baseURL);
88 public static WikiEditorPlugin getDefault() {
93 * Returns the image descriptor for the given image ID. Returns null if there is no such image.
96 * the identifier for the image to retrieve
97 * @return the image associated with the given ID
99 public static ImageDescriptor getImageDescriptor(String id) {
100 // Delegate to the plugin instance to avoid concurrent class loading problems
101 return getDefault().privateGetImageDescriptor(id);
105 * Convenience method to get an image descriptor for an extension
108 * the extension declaring the image
109 * @param subdirectoryAndFilename
110 * the path to the image
113 public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
114 IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
115 URL path = pluginDescriptor.getInstallURL();
116 URL fullPathString = null;
118 fullPathString = new URL(path, subdirectoryAndFilename);
119 return ImageDescriptor.createFromURL(fullPathString);
120 } catch (MalformedURLException e) {
125 public static String getResourceString(String key) {
126 ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
128 return (bundle != null) ? bundle.getString(key) : key;
129 } catch (MissingResourceException e) {
135 * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
136 * If so, this display is returned. Otherwise the method returns the default display.
138 public static Display getStandardDisplay() {
139 Display display = Display.getCurrent();
140 if (display == null) {
141 display = Display.getDefault();
146 private ContributionContextTypeRegistry fContextTypeRegistry;
148 private ResourceBundle fResourceBundle;
150 private ContributionTemplateStore fTemplateStore;
152 private Hashtable imageDescriptors = new Hashtable(20);
154 public WikiEditorPlugin(IPluginDescriptor descriptor) {
158 manager = ConfigurationManager.getInstance();
160 fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
161 } catch (MissingResourceException x) {
162 fResourceBundle = null;
167 * Creates an image and places it in the image registry.
169 protected void createImageDescriptor(String id, URL baseURL) {
172 url = new URL(baseURL, ICON_PATH + id);
173 } catch (MalformedURLException e) {
175 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
176 imageDescriptors.put(id, desc);
179 public IWorkbenchPage getActivePage() {
180 IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
182 return window.getActivePage();
186 public ContextTypeRegistry getContextTypeRegistry() {
187 if (fContextTypeRegistry == null) {
188 fContextTypeRegistry = new ContributionContextTypeRegistry();
189 fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
191 return fContextTypeRegistry;
194 public Image getImage(String key) {
195 Image image = getImageRegistry().get(key);
197 ImageDescriptor d = getImageDescriptor(key);
198 image = d.createImage();
199 getImageRegistry().put(key, image);
204 public ResourceBundle getResourceBundle() {
205 return fResourceBundle;
208 public TemplateStore getTemplateStore() {
209 if (fTemplateStore == null) {
210 fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
212 fTemplateStore.load();
213 } catch (IOException e) {
218 return fTemplateStore;
224 * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
226 protected void initializeDefaultPreferences(IPreferenceStore store) {
227 store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
231 * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
232 * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
234 private void initializeImages() {
235 URL baseURL = getDescriptor().getInstallURL();
238 createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
239 createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
240 createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
241 createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
242 createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
243 createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
244 createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
245 createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
249 public void log(String message) {
250 getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
253 public void log(String message, Exception e) {
254 getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
257 public void logAndReport(String title, String message, Exception e) {
259 reportError(title, message);
262 private void privateCreateImageDescriptor(String id, URL baseURL) {
265 url = new URL(baseURL, ICON_PATH + id);
266 } catch (MalformedURLException e) {
268 ImageDescriptor desc = ImageDescriptor.createFromURL(url);
269 imageDescriptors.put(id, desc);
272 private ImageDescriptor privateGetImageDescriptor(String id) {
273 if (!imageDescriptors.containsKey(id)) {
274 URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
275 createImageDescriptor(getDefault(), id, baseURL);
277 return (ImageDescriptor) imageDescriptors.get(id);
280 public void reportError(String title, String message) {
282 Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
283 MessageDialog.openError(shell, title, message);
284 } catch (RuntimeException e) {
285 log(e.getLocalizedMessage(), e);
289 public void startup() throws CoreException {
294 * Returns the translated String found with the given key.
296 * @return java.lang.String
300 public static String getResource(String key) {
302 return Platform.getResourceString(getDefault().getBundle(), key);
303 } catch (Exception e) {
309 * Returns the translated String found with the given key, and formatted with the given object.
315 * @return java.lang.String
317 public static String getResource(String key, Object[] obj) {
319 return MessageFormat.format(getResource(key), obj);
320 } catch (Exception e) {
326 * Returns the translated String found with the given key, and formatted with the given object.
332 * @return java.lang.String
334 public static String getResource(String key, String s) {
336 return MessageFormat.format(getResource(key), new String[] { s });
337 } catch (Exception e) {
343 * Return a list of all the existing configurations.
345 * @return java.util.List
347 public static List getConfigurations() {
348 return manager.getConfigurations();
352 * Create a new monitor.
354 * @return working copy
356 public static IConfigurationWorkingCopy createConfiguration() {
357 return manager.createConfiguration();
360 public static String[] getTypes() {
361 return CONFIGURATION_TYPES;