Adding new code for feature save on unfocus. This is from ticket #542. It also adds...
[phpeclipse.git] / net.sourceforge.phpeclipse.xml.ui / src / net / sourceforge / phpeclipse / xml / ui / XMLPlugin.java
1 /*
2  * Copyright (c) 2002-2004 Widespace, OU 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  *     Igor Malinin - initial contribution
10  *
11  * $Id: XMLPlugin.java,v 1.2 2006-10-21 23:14:13 pombredanne Exp $
12  */
13
14 package net.sourceforge.phpeclipse.xml.ui;
15
16 import java.net.URL;
17 import java.util.MissingResourceException;
18 import java.util.ResourceBundle;
19
20 import net.sourceforge.phpeclipse.xml.ui.text.DTDTextTools;
21 import net.sourceforge.phpeclipse.xml.ui.text.XMLTextTools;
22
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.jface.resource.ImageRegistry;
25 import org.eclipse.ui.plugin.AbstractUIPlugin;
26 import org.osgi.framework.BundleContext;
27
28 /**
29  * The main plugin class to be used in the desktop.
30  */
31 public class XMLPlugin extends AbstractUIPlugin {
32         public static final String ICON_ELEMENT = "element_obj.gif"; //$NON-NLS-1$
33
34         // The shared instance.
35         private static XMLPlugin plugin;
36
37         // Resource bundle.
38         private ResourceBundle resources;
39
40         private XMLTextTools xmlTextTools;
41
42         private DTDTextTools dtdTextTools;
43
44         /**
45          * The constructor.
46          */
47         public XMLPlugin() {
48                 plugin = this;
49
50                 try {
51                         resources = ResourceBundle
52                                         .getBundle("net.sourceforge.phpeclipse.xml.ui.XMLPluginResources"); //$NON-NLS-1$
53                 } catch (MissingResourceException x) {
54                 }
55         }
56
57         /*
58          * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
59          */
60         public void stop(BundleContext context) throws Exception {
61                 try {
62                         if (xmlTextTools != null) {
63                                 xmlTextTools.dispose();
64                                 xmlTextTools = null;
65                         }
66
67                         if (dtdTextTools != null) {
68                                 dtdTextTools.dispose();
69                                 dtdTextTools = null;
70                         }
71                 } finally {
72                         super.stop(context);
73                 }
74         }
75
76         /**
77          * Returns the shared instance.
78          */
79         public static XMLPlugin getDefault() {
80                 return plugin;
81         }
82
83         /**
84          * Returns the string from the plugin's resource bundle, or 'key' if not
85          * found.
86          */
87         public static String getResourceString(String key) {
88                 ResourceBundle bundle = XMLPlugin.getDefault().getResourceBundle();
89                 try {
90                         return bundle.getString(key);
91                 } catch (MissingResourceException e) {
92                         return key;
93                 }
94         }
95
96         /**
97          * Returns the plugin's resource bundle.
98          */
99         public ResourceBundle getResourceBundle() {
100                 return resources;
101         }
102
103         /**
104          * Returns instance of text tools for XML.
105          */
106         public XMLTextTools getXMLTextTools() {
107                 if (xmlTextTools == null) {
108                         xmlTextTools = new XMLTextTools(getPreferenceStore());
109                 }
110                 return xmlTextTools;
111         }
112
113         /**
114          * Returns instance of text tools for DTD.
115          */
116         public DTDTextTools getDTDTextTools() {
117                 if (dtdTextTools == null) {
118                         dtdTextTools = new DTDTextTools(getPreferenceStore());
119                 }
120                 return dtdTextTools;
121         }
122
123         /*
124          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
125          */
126         protected void initializeImageRegistry(ImageRegistry reg) {
127                 reg.put(ICON_ELEMENT, getImageDescriptor(ICON_ELEMENT));
128         }
129
130         /**
131          * Returns an image descriptor for the image corresponding to the specified
132          * key (which is the name of the image file).
133          * 
134          * @param key
135          *            The key of the image
136          * @return The descriptor for the requested image, or <code>null</code> if
137          *         the image could not be found
138          */
139         private ImageDescriptor getImageDescriptor(String key) {
140                 try {
141                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
142                         return ImageDescriptor.createFromURL(url);
143                 } catch (IllegalStateException e) {
144                         return null;
145                 }
146         }
147
148 }