KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > registry > RegistryReader


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.registry;
12
13 import java.util.Hashtable JavaDoc;
14
15 import org.eclipse.core.runtime.*;
16 import org.eclipse.osgi.util.NLS;
17 import org.eclipse.swt.custom.BusyIndicator;
18 import org.eclipse.team.internal.ui.TeamUIMessages;
19 import org.eclipse.team.internal.ui.TeamUIPlugin;
20 import org.eclipse.team.internal.ui.Utils.Sorter;
21 import org.osgi.framework.Bundle;
22
23 public abstract class RegistryReader {
24     protected static final String JavaDoc TAG_DESCRIPTION = "description"; //$NON-NLS-1$
25
protected static Hashtable JavaDoc extensionPoints = new Hashtable JavaDoc();
26     
27     /**
28      * Creates an extension. If the extension plugin has not
29      * been loaded a busy cursor will be activated during the duration of
30      * the load.
31      *
32      * @param element the configuration element defining the extension
33      * @param classAttribute the name of the attribute carrying the class
34      * @return the extension object
35      * @throws CoreException if the extension cannot be created
36      */

37     public static Object JavaDoc createExtension(final IConfigurationElement element,
38             final String JavaDoc classAttribute) throws CoreException {
39         try {
40             // If plugin has been loaded create extension.
41
// Otherwise, show busy cursor then create extension.
42
if (isActivated(element.getDeclaringExtension()
43                     .getContributor().getName())) {
44                 return element.createExecutableExtension(classAttribute);
45             }
46             final Object JavaDoc[] ret = new Object JavaDoc[1];
47             final CoreException[] exc = new CoreException[1];
48             BusyIndicator.showWhile(null, new Runnable JavaDoc() {
49                 public void run() {
50                     try {
51                         ret[0] = element
52                                 .createExecutableExtension(classAttribute);
53                     } catch (CoreException e) {
54                         exc[0] = e;
55                     }
56                 }
57             });
58             if (exc[0] != null) {
59                 throw exc[0];
60             }
61             return ret[0];
62
63         } catch (CoreException core) {
64             throw core;
65         } catch (Exception JavaDoc e) {
66             throw new CoreException(new Status(IStatus.ERROR, TeamUIPlugin.ID,
67                     IStatus.ERROR, NLS.bind(TeamUIMessages.RegistryReader_0, element.getNamespaceIdentifier(), element.getName()),e));
68         }
69     }
70     
71     private static boolean isActivated(String JavaDoc bundleId) {
72         return isActivated(Platform.getBundle(bundleId));
73     }
74     
75     private static boolean isActivated(Bundle bundle) {
76         return bundle != null && (bundle.getState() & (Bundle.ACTIVE | Bundle.STOPPING)) != 0;
77     }
78     
79     /**
80      * The constructor.
81      */

82     protected RegistryReader() {
83     }
84     /**
85      * This method extracts description as a sub-element of the given element.
86      *
87      * @return description string if defined, or empty string if not.
88      */

89     protected String JavaDoc getDescription(IConfigurationElement config) {
90         IConfigurationElement[] children = config.getChildren(TAG_DESCRIPTION);
91         if (children.length >= 1) {
92             return children[0].getValue();
93         }
94         return ""; //$NON-NLS-1$
95
}
96     /**
97      * Logs the error in the workbench log using the provided text and the
98      * information in the configuration element.
99      */

100     protected void logError(IConfigurationElement element, String JavaDoc text) {
101         IExtension extension = element.getDeclaringExtension();
102         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
103         buf.append("Plugin " + extension.getNamespaceIdentifier() + ", extension " + extension.getExtensionPointUniqueIdentifier()); //$NON-NLS-2$//$NON-NLS-1$
104
buf.append("\n" + text); //$NON-NLS-1$
105
TeamUIPlugin.log(IStatus.ERROR, buf.toString(), null);
106     }
107     /**
108      * Logs a very common registry error when a required attribute is missing.
109      */

110     protected void logMissingAttribute(IConfigurationElement element, String JavaDoc attributeName) {
111         logError(element, "Required attribute '" + attributeName + "' not defined"); //$NON-NLS-2$//$NON-NLS-1$
112
}
113
114     /**
115      * Logs a very common registry error when a required child is missing.
116      */

117     protected void logMissingElement(IConfigurationElement element, String JavaDoc elementName) {
118         logError(element, "Required sub element '" + elementName + "' not defined"); //$NON-NLS-2$//$NON-NLS-1$
119
}
120
121     /**
122      * Logs a registry error when the configuration element is unknown.
123      */

124     protected void logUnknownElement(IConfigurationElement element) {
125         logError(element, "Unknown extension tag found: " + element.getName()); //$NON-NLS-1$
126
}
127     /**
128      * Apply a reproducible order to the list of extensions provided, such that
129      * the order will not change as extensions are added or removed.
130      */

131     protected IExtension[] orderExtensions(IExtension[] extensions) {
132         // By default, the order is based on plugin id sorted
133
// in ascending order. The order for a plugin providing
134
// more than one extension for an extension point is
135
// dependent in the order listed in the XML file.
136
Sorter sorter = new Sorter() {
137             public boolean compare(Object JavaDoc extension1, Object JavaDoc extension2) {
138                 String JavaDoc s1 = ((IExtension) extension1).getNamespaceIdentifier();
139                 String JavaDoc s2 = ((IExtension) extension2).getNamespaceIdentifier();
140                 //Return true if elementTwo is 'greater than' elementOne
141
return s2.compareToIgnoreCase(s1) > 0;
142             }
143         };
144
145         Object JavaDoc[] sorted = sorter.sort(extensions);
146         IExtension[] sortedExtension = new IExtension[sorted.length];
147         System.arraycopy(sorted, 0, sortedExtension, 0, sorted.length);
148         return sortedExtension;
149     }
150     /**
151      * Implement this method to read element's attributes. If children should
152      * also be read, then implementor is responsible for calling <code>readElementChildren</code>.
153      * Implementor is also responsible for logging missing attributes.
154      *
155      * @return true if element was recognized, false if not.
156      */

157     protected abstract boolean readElement(IConfigurationElement element);
158     /**
159      * Read the element's children. This is called by the subclass' readElement
160      * method when it wants to read the children of the element.
161      */

162     protected void readElementChildren(IConfigurationElement element) {
163         readElements(element.getChildren());
164     }
165     /**
166      * Read each element one at a time by calling the subclass implementation
167      * of <code>readElement</code>.
168      *
169      * Logs an error if the element was not recognized.
170      */

171     protected void readElements(IConfigurationElement[] elements) {
172         for (int i = 0; i < elements.length; i++) {
173             if (!readElement(elements[i]))
174                 logUnknownElement(elements[i]);
175         }
176     }
177     /**
178      * Read one extension by looping through its configuration elements.
179      */

180     protected void readExtension(IExtension extension) {
181         readElements(extension.getConfigurationElements());
182     }
183     /**
184      * Start the registry reading process using the supplied plugin ID and
185      * extension point.
186      * @param registry the registry
187      * @param pluginId the plug-in id
188      * @param extensionPoint the extension point
189      */

190     public void readRegistry(IExtensionRegistry registry, String JavaDoc pluginId, String JavaDoc extensionPoint) {
191         String JavaDoc pointId = pluginId + "-" + extensionPoint; //$NON-NLS-1$
192
IExtension[] extensions = (IExtension[]) extensionPoints.get(pointId);
193         if (extensions == null) {
194             IExtensionPoint point = registry.getExtensionPoint(pluginId, extensionPoint);
195             if (point == null)
196                 return;
197             extensions = point.getExtensions();
198             extensionPoints.put(pointId, extensions);
199         }
200         for (int i = 0; i < extensions.length; i++)
201             readExtension(extensions[i]);
202     }
203 }
204
Popular Tags