39a172ecb08b560bbc54374a299ae41494769bd6
[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.1 2004-09-02 18:26:49 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.XMLContextType;
22
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.jface.resource.ImageDescriptor;
26 import org.eclipse.jface.resource.ImageRegistry;
27 import org.eclipse.jface.text.templates.ContextTypeRegistry;
28 import org.eclipse.jface.text.templates.persistence.TemplateStore;
29 import org.eclipse.ui.editors.text.templates.ContributionContextTypeRegistry;
30 import org.eclipse.ui.editors.text.templates.ContributionTemplateStore;
31 import org.eclipse.ui.plugin.AbstractUIPlugin;
32
33 /**
34  * The web development tools UI plugin.
35  */
36 public class WebUI extends AbstractUIPlugin {
37   private static final String CUSTOM_TEMPLATES_KEY= "net.sourceforge.phpeclipse.ui.templates"; //$NON-NLS-1$
38
39         // Constants ---------------------------------------------------------------
40
41         public static final String ICON_OVERLAY_ERROR =
42                 "full/ovr16/error_co.gif"; //$NON-NLS-1$
43         public static final String ICON_OVERLAY_WARNING =
44                 "full/ovr16/warning_co.gif"; //$NON-NLS-1$
45
46         // Instance Variables ------------------------------------------------------
47
48         /** The shared instance. */
49         private static WebUI plugin;
50
51         /** The template store. */
52         private TemplateStore fStore;
53         /** The context type registry. */
54         private ContributionContextTypeRegistry fRegistry;
55         // Constructors ------------------------------------------------------------
56
57         /**
58          * The constructor.
59          */
60         public WebUI() {
61                 plugin = this;
62         }
63
64         // Public Methods ----------------------------------------------------------
65
66         /**
67          * Returns the shared instance.
68          */
69         public static WebUI getDefault() {
70                 return plugin;
71         }
72
73         // AbstractUIPlugin Implementation -----------------------------------------
74
75         /*
76          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
77          */
78         protected void initializeImageRegistry(ImageRegistry reg) {
79                 reg.put(ICON_OVERLAY_ERROR, getImageDescriptor(ICON_OVERLAY_ERROR));
80                 reg.put(ICON_OVERLAY_WARNING, getImageDescriptor(ICON_OVERLAY_WARNING));
81         }
82
83         // Private Methods ---------------------------------------------------------
84
85         /**
86          * Returns an image descriptor for the image corresponding to the specified
87          * key (which is the name of the image file).
88          * 
89          * @param key The key of the image
90          * @return The descriptor for the requested image, or <code>null</code> if 
91          *         the image could not be found
92          */
93         private ImageDescriptor getImageDescriptor(String key) {
94                 try {
95                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
96                         return ImageDescriptor.createFromURL(url);
97                 } catch (IllegalStateException e) {
98                         return null;
99                 }
100         }
101         
102         /**
103          * Returns this plug-in's template store.
104          * 
105          * @return the template store of this plug-in instance
106          */
107         public TemplateStore getTemplateStore() {
108                 if (fStore == null) {
109                         fStore= new ContributionTemplateStore(getContextTypeRegistry(), getDefault().getPreferenceStore(), CUSTOM_TEMPLATES_KEY);
110                         try {
111                                 fStore.load();
112                         } catch (IOException e) {
113                                 WebUI.getDefault().getLog().log(new Status(IStatus.ERROR, "net.sourceforge.phpeclipse.ui", IStatus.OK, "", e)); //$NON-NLS-1$ //$NON-NLS-2$
114                         }
115                 }
116                 return fStore;
117         }
118
119         /**
120          * Returns this plug-in's context type registry.
121          * 
122          * @return the context type registry for this plug-in instance
123          */
124         public ContextTypeRegistry getContextTypeRegistry() {
125                 if (fRegistry == null) {
126                         // create an configure the contexts available in the editor
127                         fRegistry= new ContributionContextTypeRegistry();
128                         fRegistry.addContextType(XMLContextType.XML_CONTEXT_TYPE);
129                         fRegistry.addContextType(HTMLContextType.HTML_CONTEXT_TYPE);
130                         fRegistry.addContextType(JSContextType.JS_CONTEXT_TYPE);
131                 }
132                 return fRegistry;
133         }
134 }