KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > statushandlers > StatusHandlerRegistry


1 /*******************************************************************************
2  * Copyright (c) 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
12 package org.eclipse.ui.internal.statushandlers;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IExtension;
20 import org.eclipse.core.runtime.IExtensionPoint;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
23 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
24 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
25 import org.eclipse.ui.PlatformUI;
26 import org.eclipse.ui.internal.WorkbenchPlugin;
27
28 /**
29  * The registry of status handlers extensions.
30  *
31  * @since 3.3
32  *
33  */

34 public class StatusHandlerRegistry implements IExtensionChangeHandler {
35
36     private static final String JavaDoc STATUSHANDLERS_POINT_NAME = "statusHandlers"; //$NON-NLS-1$
37

38     private static final String JavaDoc TAG_STATUSHANDLER = "statusHandler"; //$NON-NLS-1$
39

40     private static final String JavaDoc TAG_STATUSHANDLER_PRODUCTBINDING = "statusHandlerProductBinding"; //$NON-NLS-1$
41

42     private ArrayList JavaDoc statusHandlerDescriptors = new ArrayList JavaDoc();
43
44     private ArrayList JavaDoc productBindingDescriptors = new ArrayList JavaDoc();
45
46     private StatusHandlerDescriptorsMap statusHandlerDescriptorsMap;
47
48     private StatusHandlerDescriptor defaultHandlerDescriptor;
49
50     private static StatusHandlerRegistry instance;
51
52     /**
53      * Creates an instance of the class.
54      */

55     private StatusHandlerRegistry() {
56         IExtensionTracker tracker = PlatformUI.getWorkbench()
57                 .getExtensionTracker();
58         IExtensionPoint handlersPoint = Platform.getExtensionRegistry()
59                 .getExtensionPoint(WorkbenchPlugin.PI_WORKBENCH,
60                         STATUSHANDLERS_POINT_NAME);
61         IExtension[] extensions = handlersPoint.getExtensions();
62
63         statusHandlerDescriptorsMap = new StatusHandlerDescriptorsMap();
64
65         // initial population
66
for (int i = 0; i < extensions.length; i++) {
67             addExtension(tracker, extensions[i]);
68         }
69
70         tracker.registerHandler(this, ExtensionTracker
71                 .createExtensionPointFilter(handlersPoint));
72
73         // registers on products ext. point to, needed
74
// for changing the default handler if product is changed
75
IExtensionPoint productsPoint = Platform.getExtensionRegistry()
76                 .getExtensionPoint(Platform.PI_RUNTIME, Platform.PT_PRODUCT);
77
78         tracker.registerHandler(this, ExtensionTracker
79                 .createExtensionPointFilter(productsPoint));
80     }
81
82     /**
83      * Returns StatusHandlerRegistry singleton instance.
84      *
85      * @return StatusHandlerRegistry instance
86      */

87     public static StatusHandlerRegistry getDefault() {
88         if (instance == null) {
89             instance = new StatusHandlerRegistry();
90         }
91         return instance;
92     }
93
94     /*
95      * (non-Javadoc)
96      *
97      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker,
98      * org.eclipse.core.runtime.IExtension)
99      */

100     public void addExtension(IExtensionTracker tracker, IExtension extension) {
101         IConfigurationElement[] configElements = extension
102                 .getConfigurationElements();
103         for (int j = 0; j < configElements.length; j++) {
104             if (configElements[j].getName().equals(TAG_STATUSHANDLER)) {
105                 StatusHandlerDescriptor descriptor = new StatusHandlerDescriptor(
106                         configElements[j]);
107                 tracker.registerObject(extension, descriptor,
108                         IExtensionTracker.REF_STRONG);
109                 statusHandlerDescriptors.add(descriptor);
110             } else if (configElements[j].getName().equals(
111                     TAG_STATUSHANDLER_PRODUCTBINDING)) {
112                 StatusHandlerProductBindingDescriptor descriptor = new StatusHandlerProductBindingDescriptor(
113                         configElements[j]);
114                 tracker.registerObject(extension, descriptor,
115                         IExtensionTracker.REF_STRONG);
116                 productBindingDescriptors.add(descriptor);
117             }
118         }
119         buildHandlersStructure();
120     }
121
122     /*
123      * (non-Javadoc)
124      *
125      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension,
126      * java.lang.Object[])
127      */

128     public void removeExtension(IExtension extension, Object JavaDoc[] objects) {
129         for (int i = 0; i < objects.length; i++) {
130             if (objects[i] instanceof StatusHandlerDescriptor) {
131                 statusHandlerDescriptors.remove(objects[i]);
132             } else if (objects[i] instanceof StatusHandlerProductBindingDescriptor) {
133                 productBindingDescriptors.remove(objects[i]);
134             }
135         }
136         buildHandlersStructure();
137     }
138
139     /**
140      * Returns the default product handler descriptor, or null if the product is
141      * not defined or there is no product binding
142      *
143      * @return the default handler
144      */

145     public StatusHandlerDescriptor getDefaultHandlerDescriptor() {
146         return defaultHandlerDescriptor;
147     }
148
149     /**
150      * Returns a list of handler descriptors which should be used for statuses
151      * with given plugin id.
152      *
153      * @param pluginId
154      * @return list of handler descriptors
155      */

156     public List JavaDoc getHandlerDescriptors(String JavaDoc pluginId) {
157         return statusHandlerDescriptorsMap.getHandlerDescriptors(pluginId);
158     }
159
160     /**
161      * Returns status handler descriptor for given id.
162      *
163      * @param statusHandlerId
164      * the id to get for
165      * @return the status handler descriptor
166      */

167     public StatusHandlerDescriptor getHandlerDescriptor(String JavaDoc statusHandlerId) {
168         StatusHandlerDescriptor descriptor = null;
169         for (Iterator JavaDoc it = statusHandlerDescriptors.iterator(); it.hasNext();) {
170             descriptor = (StatusHandlerDescriptor) it.next();
171             if (descriptor.getId().equals(statusHandlerId)) {
172                 return descriptor;
173             }
174         }
175
176         if (defaultHandlerDescriptor != null
177                 && defaultHandlerDescriptor.getId().equals(statusHandlerId)) {
178             return defaultHandlerDescriptor;
179         }
180
181         return null;
182     }
183
184     /**
185      * Disposes the registry.
186      */

187     public void dispose() {
188         PlatformUI.getWorkbench().getExtensionTracker().unregisterHandler(this);
189     }
190
191     /**
192      * Sets the default product handler descriptor if product exists and binding
193      * is defined and creates handler descriptors tree due to the prefix policy.
194      */

195     private void buildHandlersStructure() {
196         statusHandlerDescriptorsMap.clear();
197         defaultHandlerDescriptor = null;
198
199         String JavaDoc productId = Platform.getProduct() != null ? Platform
200                 .getProduct().getId() : null;
201
202         List JavaDoc allHandlers = new ArrayList JavaDoc();
203
204         String JavaDoc defaultHandlerId = null;
205
206         for (Iterator JavaDoc it = productBindingDescriptors.iterator(); it.hasNext();) {
207             StatusHandlerProductBindingDescriptor descriptor = ((StatusHandlerProductBindingDescriptor) it
208                     .next());
209
210             if (descriptor.getProductId().equals(productId)) {
211                 defaultHandlerId = descriptor.getHandlerId();
212             }
213         }
214
215         for (Iterator JavaDoc it = statusHandlerDescriptors.iterator(); it.hasNext();) {
216             StatusHandlerDescriptor descriptor = ((StatusHandlerDescriptor) it
217                     .next());
218
219             allHandlers.add(descriptor);
220         }
221
222         StatusHandlerDescriptor handlerDescriptor = null;
223
224         for (Iterator JavaDoc it = allHandlers.iterator(); it.hasNext();) {
225             handlerDescriptor = (StatusHandlerDescriptor) it.next();
226
227             if (handlerDescriptor.getId().equals(defaultHandlerId)) {
228                 defaultHandlerDescriptor = handlerDescriptor;
229             } else {
230                 statusHandlerDescriptorsMap
231                         .addHandlerDescriptor(handlerDescriptor);
232             }
233         }
234     }
235 }
236
Popular Tags