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