KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > model > loader > BaseExtensionPointManager


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 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
12 package org.eclipse.ui.internal.intro.impl.model.loader;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Hashtable JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Vector JavaDoc;
18
19 import org.eclipse.core.runtime.IConfigurationElement;
20 import org.eclipse.core.runtime.IExtensionRegistry;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroIdElement;
23 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
24 import org.eclipse.ui.internal.intro.impl.util.Log;
25 import org.eclipse.ui.internal.intro.impl.util.Util;
26
27 /**
28  * Base class for handling Intro Extensions.
29  */

30 public class BaseExtensionPointManager {
31
32     // the config extension id
33
protected static final String JavaDoc CONFIG = "org.eclipse.ui.intro.config"; //$NON-NLS-1$
34

35     // the configExtension extension id
36
protected static final String JavaDoc CONFIG_EXTENSION = "org.eclipse.ui.intro.configExtension"; //$NON-NLS-1$
37

38     // the attribute in the config element to specify the intro part id.
39
protected static final String JavaDoc ATT_CONFIG_INTRO_ID = "introId"; //$NON-NLS-1$
40

41     // the attribute in the config element to specify the intro part id.
42
protected static final String JavaDoc ATT_CONFIG_EXTENSION_CONFIG_ID = "configId"; //$NON-NLS-1$
43

44     // the id attribute in any intro element.
45
protected static final String JavaDoc ATT_ID = AbstractIntroIdElement.ATT_ID;
46
47
48     protected Hashtable JavaDoc introModels = new Hashtable JavaDoc();
49     protected IExtensionRegistry registry;
50     protected SharedConfigExtensionsManager sharedConfigExtensionsManager;
51     private String JavaDoc extensionFilter;
52
53     /*
54      * Prevent creation.
55      */

56     protected BaseExtensionPointManager() {
57         registry = Platform.getExtensionRegistry();
58     }
59
60     protected IntroModelRoot loadModel(String JavaDoc attributeName,
61             String JavaDoc attributeValue) {
62
63         long start = 0;
64         if (Log.logPerformance)
65             start = System.currentTimeMillis();
66
67         // get all Config extension point contributions. There could be more
68
// than one config contribution, but there should only be one that maps
69
// to the cached intro part id.
70
IConfigurationElement introConfig = getIntroConfig(attributeName,
71             attributeValue);
72
73         // load model with the config elements of the correct contribution. If
74
// there are no valid contribution, model stays null.
75
if (introConfig != null) {
76             // we found matching config. Get all configExtension contributed to
77
// this config and pass to model. Load generic config extensions as
78
// well.
79
String JavaDoc configId = introConfig.getAttribute(ATT_ID);
80             IConfigurationElement[] introConfigExtensions = null;
81             if (configId == null)
82                 // if id of config is null, pass empty array.
83
introConfigExtensions = new IConfigurationElement[0];
84             else
85                 introConfigExtensions = getIntroConfigExtensions(
86                     ATT_CONFIG_EXTENSION_CONFIG_ID, configId);
87
88             if (Log.logPerformance)
89                 Util.logPerformanceTime(
90                     "BEGIN: quering registry for configs took: ", start); //$NON-NLS-1$
91

92
93             IntroModelRoot model = new IntroModelRoot(introConfig,
94                 introConfigExtensions);
95             model.loadModel();
96             // add the current model to the hash table of models, only if it is
97
// not null. They key is the model id, which is the id of the
98
// config that defined this model.
99
addCachedModel(model.getId(), model);
100
101             // now load all generic config extension. ie: standbyPart and
102
// command contributions.
103
loadSharedConfigExtensions();
104
105             if (Log.logPerformance)
106                 Util
107                     .logPerformanceTime(
108                         "loading Intro Model (quering registry/creating & resolving model) took: ", //$NON-NLS-1$
109
start);
110
111             return model;
112         }
113         return null;
114     }
115
116     /**
117      * Go through all the config elements and only return the correct config
118      * that maps to the correct intro part id. If there is more than one config
119      * thats maps to the same intro part id, log the fact, and return the first
120      * one. If there are non, return null.
121      *
122      * @param configElements
123      * @return
124      */

125     protected IConfigurationElement getIntroConfig(String JavaDoc attrributeName,
126             String JavaDoc attributeValue) {
127
128         IConfigurationElement[] configElements = registry
129             .getConfigurationElementsFor(CONFIG);
130
131         IConfigurationElement config = getConfigurationFromAttribute(
132             configElements, attrributeName, attributeValue);
133
134         if (config == null)
135             // if there is no valid config, log the fact.
136
Log.warning("No Intro configuration found with " + attrributeName //$NON-NLS-1$
137
+ " of value = " + attributeValue); //$NON-NLS-1$
138

139         return config;
140     }
141
142     /**
143      * Go through all the configExtension elements and return an array of all
144      * extensions that match the attribute and its value. If there are non,
145      * return empty array. This also loads all standby contributions.
146      */

147     protected IConfigurationElement[] getIntroConfigExtensions(
148             String JavaDoc attrributeName, String JavaDoc attributeValue) {
149
150         IConfigurationElement[] configExtensionElements = registry
151             .getConfigurationElementsFor(CONFIG_EXTENSION);
152         
153         /*
154          * Extension filter is used for performance testing to only load contributions
155          * from a specific plug-in (fixed data set).
156          */

157         if (extensionFilter != null) {
158             List JavaDoc filtered = new ArrayList JavaDoc();
159             for (int i=0;i<configExtensionElements.length;++i) {
160                 if (extensionFilter.equals(configExtensionElements[i].getContributor().getName())) {
161                     filtered.add(configExtensionElements[i]);
162                 }
163             }
164             configExtensionElements = (IConfigurationElement[])filtered.toArray(new IConfigurationElement[filtered.size()]);
165         }
166
167         IConfigurationElement[] configExtensions = getConfigurationsFromAttribute(
168             configExtensionElements, attrributeName, attributeValue);
169
170         return configExtensions;
171     }
172
173     /**
174      * Add a model to the cache. This method is private because only this
175      * manager class knows how to load an intro model.
176      *
177      * @param modelId
178      * @param model
179      */

180     protected void addCachedModel(String JavaDoc modelId, IntroModelRoot model) {
181         introModels.put(modelId, model);
182     }
183
184     /**
185      * Gets the given model from the cache.
186      *
187      * @param modelId
188      */

189     protected IntroModelRoot getCachedModel(String JavaDoc configId) {
190         return (IntroModelRoot) introModels.get(configId);
191     }
192
193     /**
194      * Go through all the config elements and only return the correct config
195      * with an attribute of the given value. If there is more than one
196      * configuration element that maps to the attribute value log the fact, and
197      * return the first one. If there are non, return null.
198      *
199      * @param configElements
200      * @return
201      */

202     protected IConfigurationElement getConfigurationFromAttribute(
203             IConfigurationElement[] configElements, String JavaDoc attributeName,
204             String JavaDoc attributeValue) {
205
206         // find all configs with given attribute and attibute value.
207
IConfigurationElement[] filteredConfigElements = getConfigurationsFromAttribute(
208             configElements, attributeName, attributeValue);
209         // now validate that we got only one.
210
IConfigurationElement config = ModelLoaderUtil
211             .validateSingleContribution(filteredConfigElements, attributeName);
212         return config;
213     }
214
215     /**
216      * Go through all the config elements and return an array of matching
217      * configs with an attribute of the given value. If there are non, return
218      * empty array.
219      */

220     protected IConfigurationElement[] getConfigurationsFromAttribute(
221             IConfigurationElement[] configElements, String JavaDoc attributeName,
222             String JavaDoc attributeValue) {
223
224         // find all configs with given attribute and attibute value.
225
Vector JavaDoc elements = new Vector JavaDoc();
226         for (int i = 0; i < configElements.length; i++) {
227             String JavaDoc currentAttributeValue = configElements[i]
228                 .getAttribute(attributeName);
229             if (currentAttributeValue != null
230                     && currentAttributeValue.equals(attributeValue))
231                 elements.add(configElements[i]);
232         }
233
234         // now return array.
235
IConfigurationElement[] filteredConfigElements = new IConfigurationElement[elements
236             .size()];
237         elements.copyInto(filteredConfigElements);
238
239         return filteredConfigElements;
240     }
241
242     /**
243      * Loads all shared config extennsions (ie: standby parts and commands).
244      */

245     protected void loadSharedConfigExtensions() {
246         sharedConfigExtensionsManager = new SharedConfigExtensionsManager(
247             registry);
248         sharedConfigExtensionsManager.loadSharedConfigExtensions();
249     }
250
251
252     /**
253      * @return Returns the sharedConfigExtensionsManager.
254      */

255     public SharedConfigExtensionsManager getSharedConfigExtensionsManager() {
256         return sharedConfigExtensionsManager;
257     }
258
259     /**
260      * @return Returns the cached introModels.
261      */

262     public Hashtable JavaDoc getIntroModels() {
263         return introModels;
264     }
265     
266     /*
267      * Internal test hook for restricting which extensions to load.
268      */

269     public void setExtensionFilter(String JavaDoc pluginId) {
270         extensionFilter = pluginId;
271     }
272 }
273
Popular Tags