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