Fixes for the category.xml file to have each feature in the correct
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / ui / WebUI.java
1 /*
2  * Copyright (c) 2004 Christopher Lenz and others
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  *     Christopher Lenz - initial implementation
10  *
11  * $Id: WebUI.java,v 1.11 2007-11-08 01:37:06 scorphus Exp $
12  */
13
14 package net.sourceforge.phpeclipse.ui;
15
16 import java.io.IOException;
17 import java.net.URL;
18
19 import net.sourceforge.phpeclipse.ui.templates.template.HTMLTemplateContextType;
20 import net.sourceforge.phpeclipse.ui.templates.template.JSTemplateContextType;
21 import net.sourceforge.phpeclipse.ui.templates.template.SmartyTemplateContextType;
22 import net.sourceforge.phpeclipse.ui.templates.template.XMLTemplateContextType;
23
24 import org.eclipse.core.resources.IWorkspace;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.jface.preference.IPreferenceStore;
29 import org.eclipse.jface.resource.ImageDescriptor;
30 import org.eclipse.jface.resource.ImageRegistry;
31 import org.eclipse.jface.text.templates.ContextTypeRegistry;
32 import org.eclipse.jface.text.templates.persistence.TemplateStore;
33 import org.eclipse.swt.widgets.Shell;
34 import org.eclipse.ui.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
37 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
38 import org.eclipse.ui.plugin.AbstractUIPlugin;
39
40 /**
41  * The web development tools UI plugin.
42  */
43 public class WebUI extends AbstractUIPlugin implements IPreferenceConstants {
44
45         public static final String PLUGIN_ID = "net.sourceforge.phpeclipse.ui";
46
47         private static final String CUSTOM_TEMPLATES_KEY = "net.sourceforge.phpeclipse.ui.templates"; //$NON-NLS-1$
48
49         // Constants ---------------------------------------------------------------
50
51         public static final String ICON_OVERLAY_ERROR = "full/ovr16/error_co.gif"; //$NON-NLS-1$
52
53         public static final String ICON_OVERLAY_WARNING = "full/ovr16/warning_co.gif"; //$NON-NLS-1$
54
55         // Instance Variables ------------------------------------------------------
56
57         /** The shared instance. */
58         private static WebUI plugin;
59
60         public static IWorkbenchPage getActivePage() {
61                 return getDefault().internalGetActivePage();
62         }
63
64         private IWorkbenchPage internalGetActivePage() {
65                 return getWorkbench().getActiveWorkbenchWindow().getActivePage();
66         }
67
68         public static Shell getActiveWorkbenchShell() {
69                 return getActiveWorkbenchWindow().getShell();
70         }
71
72         public static IWorkbenchWindow getActiveWorkbenchWindow() {
73                 return getDefault().getWorkbench().getActiveWorkbenchWindow();
74         }
75
76         // Public Methods ----------------------------------------------------------
77
78         /**
79          * Returns the shared instance.
80          */
81         public static WebUI getDefault() {
82                 return plugin;
83         }
84
85         /**
86          * Returns the workspace instance.
87          */
88         public static IWorkspace getWorkspace() {
89                 return ResourcesPlugin.getWorkspace();
90         }
91
92         /** The context type registry. */
93         private ContributionContextTypeRegistry fRegistry;
94
95         /** The template store. */
96         private TemplateStore fStore;
97
98         // Constructors ------------------------------------------------------------
99
100         /**
101          * The constructor.
102          */
103         public WebUI() {
104                 plugin = this;
105         }
106
107         /**
108          * Returns this plug-in's context type registry.
109          * 
110          * @return the context type registry for this plug-in instance
111          */
112         public ContextTypeRegistry getContextTypeRegistry() {
113                 if (fRegistry == null) {
114                         // create an configure the contexts available in the editor
115                         fRegistry = new ContributionContextTypeRegistry();
116                         fRegistry.addContextType(XMLTemplateContextType.XML_CONTEXT_TYPE);
117                         fRegistry.addContextType(HTMLTemplateContextType.HTML_CONTEXT_TYPE);
118                         fRegistry.addContextType(SmartyTemplateContextType.SMARTY_CONTEXT_TYPE);
119                         fRegistry.addContextType(JSTemplateContextType.JS_CONTEXT_TYPE);
120                 }
121                 return fRegistry;
122         }
123
124         // Private Methods ---------------------------------------------------------
125
126         /**
127          * Returns an image descriptor for the image corresponding to the specified
128          * key (which is the name of the image file).
129          * 
130          * @param key
131          *            The key of the image
132          * @return The descriptor for the requested image, or <code>null</code> if
133          *         the image could not be found
134          */
135         private ImageDescriptor getImageDescriptor(String key) {
136                 try {
137                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
138                         return ImageDescriptor.createFromURL(url);
139                 } catch (IllegalStateException e) {
140                         return null;
141                 }
142         }
143
144         /**
145          * Returns this plug-in's template store.
146          * 
147          * @return the template store of this plug-in instance
148          */
149         public TemplateStore getTemplateStore() {
150                 if (fStore == null) {
151                         fStore = new ContributionTemplateStore(getContextTypeRegistry(),
152                                         getDefault().getPreferenceStore(), CUSTOM_TEMPLATES_KEY);
153                         try {
154                                 fStore.load();
155                         } catch (IOException e) {
156                                 WebUI
157                                                 .getDefault()
158                                                 .getLog()
159                                                 .log(
160                                                                 new Status(
161                                                                                 IStatus.ERROR,
162                                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$
163                         }
164                 }
165                 return fStore;
166         }
167
168         protected void initializeDefaultPreferences(IPreferenceStore store) {
169                 store.setDefault(PHP_LOCALHOST_PREF, "http://localhost");
170                 store.setDefault(PHP_DOCUMENTROOT_PREF, getWorkspace().getRoot()
171                                 .getFullPath().toString());
172                 // store.setDefault(PHP_BOOKMARK_DEFAULT, "");
173
174                 store.setDefault(PHP_AUTO_PREVIEW_DEFAULT, "false");
175                 store.setDefault(PHP_BRING_TO_TOP_PREVIEW_DEFAULT, "false");
176                 store.setDefault(PHP_STICKY_BROWSER_URL_DEFAULT, "false");
177                 // store.setDefault(PHP_SHOW_HTML_FILES_LOCAL, "true");
178                 // store.setDefault(PHP_SHOW_XML_FILES_LOCAL, "false");
179         }
180
181         /*
182          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
183          */
184         protected void initializeImageRegistry(ImageRegistry reg) {
185                 reg.put(ICON_OVERLAY_ERROR, getImageDescriptor(ICON_OVERLAY_ERROR));
186                 reg.put(ICON_OVERLAY_WARNING, getImageDescriptor(ICON_OVERLAY_WARNING));
187         }
188
189         // private IWorkbenchPage internalGetActivePage() {
190         // IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
191         // if (window != null)
192         // return window.getActivePage();
193         // return null;
194         // }
195
196         public static void log(IStatus status) {
197                 getDefault().getLog().log(status);
198         }
199
200         public static void log(Throwable e) {
201                 log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.ERROR, ""
202                                 + e.getLocalizedMessage(), e));
203         }
204 }