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