6e7669bbe55dea8a3eb1d7bd2dff8fb202f40f94
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / WikiEditorPlugin.java
1 package net.sourceforge.phpeclipse.wiki.editor;
2
3 import java.io.IOException;
4 import java.net.MalformedURLException;
5 import java.net.URL;
6 import java.text.MessageFormat;
7 import java.util.Hashtable;
8 import java.util.List;
9 import java.util.MissingResourceException;
10 import java.util.ResourceBundle;
11
12 import net.sourceforge.phpeclipse.wiki.internal.IConfigurationWorkingCopy;
13 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
14
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;
35
36 public class WikiEditorPlugin extends AbstractUIPlugin {
37
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";
42   
43   public static final String[] CONFIGURATION_TYPES = {
44       HTTP_QUERY,
45       WIKIPEDIA_GET_TEXT, 
46       WEBLOG_API_SEND
47   };
48   //image paths
49   public static final String ICON_PATH = "icons/full/"; //$NON-NLS-1$
50
51   public final static String PLUGIN_ID = "net.sourceforge.phpeclipse.wiki";
52
53   public final static String HTML_OUTPUT_PATH = "__static_wiki_folder";
54
55   public final static String WIKI_TEXTS_BASE_PATH = "__wiki_texts_base_path";
56   
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";
59   public final static String LOCAL_CSS_URL = "__local_css_url"; 
60   public final static String EXPORT_CSS_URL = "__export_css_url";
61   public final static String PREF_STRING_CONFIGURATIONS = "configurations";
62   public final static String CONFIG_MEMENTO = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
63                 "<configurations>"+
64 "<config name=\"Google Search\" type-id=\"HTTP Query\" url=\"http://www.google.com/search?q=$text.selection\"/>" +
65 "<config name=\"Koders.com Search\" type-id=\"HTTP Query\" url=\"http://koders.com/?s=$text.selection\"/>" +
66 "<config name=\"Leo.org Translation\" type-id=\"HTTP Query\" url=\"http://dict.leo.org/?search=$text.selection\"/>" +
67 "<config name=\"Wikipedia-en\" type-id=\"Wikipedia-Load Text\" url=\"http://en.wikipedia.org/w/wiki.phtml?title=$text.wikiname&amp;action=edit\"/>" +
68 "<config name=\"Wikibooks-en\" type-id=\"Wikipedia-Load Text\" url=\"http://en.wikibooks.org/w/wiki.phtml?title=$text.wikiname&amp;action=edit\"/>" +
69 "</configurations>";
70   private static ConfigurationManager manager;
71 //
72 //  public static final String IMG_MONITOR_ON = "monitorOn";
73 //
74 //  public static final String IMG_MONITOR_OFF = "monitorOff";
75
76   /**
77    * Creates an image and places it in the image registry.
78    * 
79    * @param id
80    *          the identifier for the image
81    * @param baseURL
82    *          the base URL for the image
83    */
84   protected static void createImageDescriptor(WikiEditorPlugin plugin, String id, URL baseURL) {
85     // Delegate to the plugin instance to avoid concurrent class loading problems
86     plugin.privateCreateImageDescriptor(id, baseURL);
87   }
88
89   public static WikiEditorPlugin getDefault() {
90     return fgPlugin;
91   }
92
93   /**
94    * Returns the image descriptor for the given image ID. Returns null if there is no such image.
95    * 
96    * @param id
97    *          the identifier for the image to retrieve
98    * @return the image associated with the given ID
99    */
100   public static ImageDescriptor getImageDescriptor(String id) {
101     // Delegate to the plugin instance to avoid concurrent class loading problems
102     return getDefault().privateGetImageDescriptor(id);
103   }
104
105   /**
106    * Convenience method to get an image descriptor for an extension
107    * 
108    * @param extension
109    *          the extension declaring the image
110    * @param subdirectoryAndFilename
111    *          the path to the image
112    * @return the image
113    */
114   public static ImageDescriptor getImageDescriptorFromExtension(IExtension extension, String subdirectoryAndFilename) {
115     IPluginDescriptor pluginDescriptor = extension.getDeclaringPluginDescriptor();
116     URL path = pluginDescriptor.getInstallURL();
117     URL fullPathString = null;
118     try {
119       fullPathString = new URL(path, subdirectoryAndFilename);
120       return ImageDescriptor.createFromURL(fullPathString);
121     } catch (MalformedURLException e) {
122     }
123     return null;
124   }
125
126   public static String getResourceString(String key) {
127     ResourceBundle bundle = WikiEditorPlugin.getDefault().getResourceBundle();
128     try {
129       return (bundle != null) ? bundle.getString(key) : key;
130     } catch (MissingResourceException e) {
131       return key;
132     }
133   }
134
135   /**
136    * Returns the standard display to be used. The method first checks, if the thread calling this method has an associated display.
137    * If so, this display is returned. Otherwise the method returns the default display.
138    */
139   public static Display getStandardDisplay() {
140     Display display = Display.getCurrent();
141     if (display == null) {
142       display = Display.getDefault();
143     }
144     return display;
145   }
146
147   private ContributionContextTypeRegistry fContextTypeRegistry;
148
149   private ResourceBundle fResourceBundle;
150
151   private ContributionTemplateStore fTemplateStore;
152
153   private Hashtable imageDescriptors = new Hashtable(20);
154
155   public WikiEditorPlugin(IPluginDescriptor descriptor) {
156     super(descriptor);
157     initializeImages();
158     fgPlugin = this;
159     manager = ConfigurationManager.getInstance();
160     try {
161       fResourceBundle = ResourceBundle.getBundle("net.sourceforge.phpeclipse.wiki.editor.WikiEditorMessages");
162     } catch (MissingResourceException x) {
163       fResourceBundle = null;
164     }
165   }
166
167   /**
168    * Creates an image and places it in the image registry.
169    */
170   protected void createImageDescriptor(String id, URL baseURL) {
171     URL url = null;
172     try {
173       url = new URL(baseURL, ICON_PATH + id);
174     } catch (MalformedURLException e) {
175     }
176     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
177     imageDescriptors.put(id, desc);
178   }
179
180   public IWorkbenchPage getActivePage() {
181     IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
182     if (window != null)
183       return window.getActivePage();
184     return null;
185   }
186
187   public ContextTypeRegistry getContextTypeRegistry() {
188     if (fContextTypeRegistry == null) {
189       fContextTypeRegistry = new ContributionContextTypeRegistry();
190       fContextTypeRegistry.addContextType("net.sourceforge.phpeclipse.wiki.editor.templates");
191     }
192     return fContextTypeRegistry;
193   }
194
195   public Image getImage(String key) {
196     Image image = getImageRegistry().get(key);
197     if (image == null) {
198       ImageDescriptor d = getImageDescriptor(key);
199       image = d.createImage();
200       getImageRegistry().put(key, image);
201     }
202     return image;
203   }
204
205   public ResourceBundle getResourceBundle() {
206     return fResourceBundle;
207   }
208
209   public TemplateStore getTemplateStore() {
210     if (fTemplateStore == null) {
211       fTemplateStore = new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), "templates");
212       try {
213         fTemplateStore.load();
214       } catch (IOException e) {
215         e.printStackTrace();
216       }
217     }
218
219     return fTemplateStore;
220   }
221
222   /*
223    * (non-Javadoc)
224    * 
225    * @see org.eclipse.ui.plugin.AbstractUIPlugin#initializeDefaultPreferences(org.eclipse.jface.preference.IPreferenceStore)
226    */
227   protected void initializeDefaultPreferences(IPreferenceStore store) {
228     store.setDefault(PREF_STRING_CONFIGURATIONS, CONFIG_MEMENTO);
229   }
230
231   /*
232    * Initializes the table of images used in this plugin. The plugin is provided because this method is called before the plugin
233    * staic variable has been set. See the comment on the getPlugin() method for a description of why this is required.
234    */
235   private void initializeImages() {
236     URL baseURL = getDescriptor().getInstallURL();
237
238     //  special
239     createImageDescriptor("glyphs/glyph1.gif", baseURL); //$NON-NLS-1$
240     createImageDescriptor("glyphs/glyph2.gif", baseURL); //$NON-NLS-1$
241     createImageDescriptor("glyphs/glyph3.gif", baseURL); //$NON-NLS-1$
242     createImageDescriptor("glyphs/glyph4.gif", baseURL); //$NON-NLS-1$
243     createImageDescriptor("glyphs/glyph5.gif", baseURL); //$NON-NLS-1$
244     createImageDescriptor("glyphs/glyph6.gif", baseURL); //$NON-NLS-1$
245     createImageDescriptor("glyphs/glyph7.gif", baseURL); //$NON-NLS-1$
246     createImageDescriptor("glyphs/glyph8.gif", baseURL); //$NON-NLS-1$
247
248   }
249
250   public void log(String message) {
251     getDefault().getLog().log(new Status(IStatus.OK, PLUGIN_ID, IStatus.OK, message, null));
252   }
253
254   public void log(String message, Exception e) {
255     getDefault().getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Caught exception", e));
256   }
257
258   public void logAndReport(String title, String message, Exception e) {
259     log(message, e);
260     reportError(title, message);
261   }
262
263   private void privateCreateImageDescriptor(String id, URL baseURL) {
264     URL url = null;
265     try {
266       url = new URL(baseURL, ICON_PATH + id);
267     } catch (MalformedURLException e) {
268     }
269     ImageDescriptor desc = ImageDescriptor.createFromURL(url);
270     imageDescriptors.put(id, desc);
271   }
272
273   private ImageDescriptor privateGetImageDescriptor(String id) {
274     if (!imageDescriptors.containsKey(id)) {
275       URL baseURL = WikiEditorPlugin.getDefault().getDescriptor().getInstallURL();
276       createImageDescriptor(getDefault(), id, baseURL);
277     }
278     return (ImageDescriptor) imageDescriptors.get(id);
279   }
280
281   public void reportError(String title, String message) {
282     try {
283       Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
284       MessageDialog.openError(shell, title, message);
285     } catch (RuntimeException e) {
286       log(e.getLocalizedMessage(), e);
287     }
288   }
289
290   public void startup() throws CoreException {
291     super.startup();
292   }
293
294   /**
295    * Returns the translated String found with the given key.
296    * 
297    * @return java.lang.String
298    * @param key
299    *          java.lang.String
300    */
301   public static String getResource(String key) {
302     try {
303       return Platform.getResourceString(getDefault().getBundle(), key);
304     } catch (Exception e) {
305       return key;
306     }
307   }
308
309   /**
310    * Returns the translated String found with the given key, and formatted with the given object.
311    * 
312    * @param key
313    *          java.lang.String
314    * @param obj
315    *          java.lang.Object[]
316    * @return java.lang.String
317    */
318   public static String getResource(String key, Object[] obj) {
319     try {
320       return MessageFormat.format(getResource(key), obj);
321     } catch (Exception e) {
322       return key;
323     }
324   }
325
326   /**
327    * Returns the translated String found with the given key, and formatted with the given object.
328    * 
329    * @param key
330    *          java.lang.String
331    * @param s
332    *          java.lang.String
333    * @return java.lang.String
334    */
335   public static String getResource(String key, String s) {
336     try {
337       return MessageFormat.format(getResource(key), new String[] { s });
338     } catch (Exception e) {
339       return key;
340     }
341   }
342
343   /**
344    * Return a list of all the existing configurations.
345    * 
346    * @return java.util.List
347    */
348   public static List getConfigurations() {
349     return manager.getConfigurations();
350   }
351
352   /**
353    * Create a new monitor.
354    * 
355    * @return working copy
356    */
357   public static IConfigurationWorkingCopy createConfiguration() {
358     return manager.createConfiguration();
359   }
360   
361   public static String[] getTypes() {
362     return CONFIGURATION_TYPES;
363   }
364 }