8404335c5c3870726fb9b968c68b03be663a8f21
[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.7 2006-10-21 23:13:54 pombredanne 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.HTMLContextType;
20 import net.sourceforge.phpeclipse.ui.templates.template.JSContextType;
21 import net.sourceforge.phpeclipse.ui.templates.template.SmartyContextType;
22 import net.sourceforge.phpeclipse.ui.templates.template.XMLContextType;
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         private static final String CUSTOM_TEMPLATES_KEY = "net.sourceforge.phpeclipse.ui.templates"; //$NON-NLS-1$
45
46         // Constants ---------------------------------------------------------------
47
48         public static final String ICON_OVERLAY_ERROR = "full/ovr16/error_co.gif"; //$NON-NLS-1$
49
50         public static final String ICON_OVERLAY_WARNING = "full/ovr16/warning_co.gif"; //$NON-NLS-1$
51
52         // Instance Variables ------------------------------------------------------
53
54         /** The shared instance. */
55         private static WebUI plugin;
56
57         public static IWorkbenchPage getActivePage() {
58                 return getDefault().internalGetActivePage();
59         }
60
61         private IWorkbenchPage internalGetActivePage() {
62                 return getWorkbench().getActiveWorkbenchWindow().getActivePage();
63         }
64
65         public static Shell getActiveWorkbenchShell() {
66                 return getActiveWorkbenchWindow().getShell();
67         }
68
69         public static IWorkbenchWindow getActiveWorkbenchWindow() {
70                 return getDefault().getWorkbench().getActiveWorkbenchWindow();
71         }
72
73         // Public Methods ----------------------------------------------------------
74
75         /**
76          * Returns the shared instance.
77          */
78         public static WebUI getDefault() {
79                 return plugin;
80         }
81
82         /**
83          * Returns the workspace instance.
84          */
85         public static IWorkspace getWorkspace() {
86                 return ResourcesPlugin.getWorkspace();
87         }
88
89         /** The context type registry. */
90         private ContributionContextTypeRegistry fRegistry;
91
92         /** The template store. */
93         private TemplateStore fStore;
94
95         // Constructors ------------------------------------------------------------
96
97         /**
98          * The constructor.
99          */
100         public WebUI() {
101                 plugin = this;
102         }
103
104         /**
105          * Returns this plug-in's context type registry.
106          * 
107          * @return the context type registry for this plug-in instance
108          */
109         public ContextTypeRegistry getContextTypeRegistry() {
110                 if (fRegistry == null) {
111                         // create an configure the contexts available in the editor
112                         fRegistry = new ContributionContextTypeRegistry();
113                         fRegistry.addContextType(XMLContextType.XML_CONTEXT_TYPE);
114                         fRegistry.addContextType(HTMLContextType.HTML_CONTEXT_TYPE);
115                         fRegistry.addContextType(SmartyContextType.SMARTY_CONTEXT_TYPE);
116                         fRegistry.addContextType(JSContextType.JS_CONTEXT_TYPE);
117                 }
118                 return fRegistry;
119         }
120
121         // Private Methods ---------------------------------------------------------
122
123         /**
124          * Returns an image descriptor for the image corresponding to the specified
125          * key (which is the name of the image file).
126          * 
127          * @param key
128          *            The key of the image
129          * @return The descriptor for the requested image, or <code>null</code> if
130          *         the image could not be found
131          */
132         private ImageDescriptor getImageDescriptor(String key) {
133                 try {
134                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
135                         return ImageDescriptor.createFromURL(url);
136                 } catch (IllegalStateException e) {
137                         return null;
138                 }
139         }
140
141         /**
142          * Returns this plug-in's template store.
143          * 
144          * @return the template store of this plug-in instance
145          */
146         public TemplateStore getTemplateStore() {
147                 if (fStore == null) {
148                         fStore = new ContributionTemplateStore(getContextTypeRegistry(),
149                                         getDefault().getPreferenceStore(), CUSTOM_TEMPLATES_KEY);
150                         try {
151                                 fStore.load();
152                         } catch (IOException e) {
153                                 WebUI
154                                                 .getDefault()
155                                                 .getLog()
156                                                 .log(
157                                                                 new Status(
158                                                                                 IStatus.ERROR,
159                                                                                 "net.sourceforge.phpeclipse.ui", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$
160                         }
161                 }
162                 return fStore;
163         }
164
165         protected void initializeDefaultPreferences(IPreferenceStore store) {
166                 store.setDefault(PHP_LOCALHOST_PREF, "http://localhost");
167                 store.setDefault(PHP_DOCUMENTROOT_PREF, getWorkspace().getRoot()
168                                 .getLocation().toString());
169                 // store.setDefault(PHP_BOOKMARK_DEFAULT, "");
170
171                 store.setDefault(PHP_AUTO_PREVIEW_DEFAULT, "true");
172                 store.setDefault(PHP_BRING_TO_TOP_PREVIEW_DEFAULT, "true");
173                 // store.setDefault(PHP_SHOW_HTML_FILES_LOCAL, "true");
174                 // store.setDefault(PHP_SHOW_XML_FILES_LOCAL, "false");
175         }
176
177         /*
178          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
179          */
180         protected void initializeImageRegistry(ImageRegistry reg) {
181                 reg.put(ICON_OVERLAY_ERROR, getImageDescriptor(ICON_OVERLAY_ERROR));
182                 reg.put(ICON_OVERLAY_WARNING, getImageDescriptor(ICON_OVERLAY_WARNING));
183         }
184         // private IWorkbenchPage internalGetActivePage() {
185         // IWorkbenchWindow window = getWorkbench().getActiveWorkbenchWindow();
186         // if (window != null)
187         // return window.getActivePage();
188         // return null;
189         // }
190 }