Avoid NPE for "mark occurences"
[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.1 2004-09-02 18:28:04 jsurfer 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         private DTDTextTools dtdTextTools;
42
43         /**
44          * The constructor.
45          */
46         public XMLPlugin() {
47                 plugin = this;
48
49                 try {
50                         resources = ResourceBundle.getBundle(
51                                 "net.sourceforge.phpeclipse.xml.ui.XMLPluginResources"); //$NON-NLS-1$
52                 } catch (MissingResourceException x) {
53                 }
54         }
55
56         /*
57          * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
58          */
59         public void stop(BundleContext context) throws Exception {
60                 try {
61                         if (xmlTextTools != null) {
62                                 xmlTextTools.dispose();
63                                 xmlTextTools = null;
64                         }
65
66                         if (dtdTextTools != null) {
67                                 dtdTextTools.dispose();
68                                 dtdTextTools = null;
69                         }
70                 } finally {
71                         super.stop(context);
72                 }
73         }
74
75         /**
76          * Returns the shared instance.
77          */
78         public static XMLPlugin getDefault() {
79                 return plugin;
80         }
81
82         /**
83          * Returns the string from the plugin's resource bundle, or 'key' if not
84          * found.
85          */
86         public static String getResourceString(String key) {
87                 ResourceBundle bundle = XMLPlugin.getDefault().getResourceBundle();
88                 try {
89                         return bundle.getString(key);
90                 } catch (MissingResourceException e) {
91                         return key;
92                 }
93         }
94
95         /**
96          * Returns the plugin's resource bundle.
97          */
98         public ResourceBundle getResourceBundle() {
99                 return resources;
100         }
101
102         /**
103          * Returns instance of text tools for XML.
104          */
105         public XMLTextTools getXMLTextTools() {
106                 if (xmlTextTools == null) {
107                         xmlTextTools = new XMLTextTools(getPreferenceStore());
108                 }
109                 return xmlTextTools;
110         }
111
112         /**
113          * Returns instance of text tools for DTD.
114          */
115         public DTDTextTools getDTDTextTools() {
116                 if (dtdTextTools == null) {
117                         dtdTextTools = new DTDTextTools(getPreferenceStore());
118                 }
119                 return dtdTextTools;
120         }
121
122         /*
123          * @see AbstractUIPlugin#initializeImageRegistry(ImageRegistry)
124          */
125         protected void initializeImageRegistry(ImageRegistry reg) {
126                 reg.put(ICON_ELEMENT, getImageDescriptor(ICON_ELEMENT));
127         }
128
129         /**
130          * Returns an image descriptor for the image corresponding to the specified
131          * key (which is the name of the image file).
132          * 
133          * @param key The key of the image
134          * @return The descriptor for the requested image, or <code>null</code> if 
135          *         the image could not be found
136          */
137         private ImageDescriptor getImageDescriptor(String key) {
138                 try {
139                         URL url = getBundle().getEntry("/icons/" + key); //$NON-NLS-1$
140                         return ImageDescriptor.createFromURL(url);
141                 } catch (IllegalStateException e) {
142                         return null;
143                 }
144         }
145         
146         
147
148 }