001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: ******************************************************************************/package org.eclipse.ui.internal.statushandlers;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.IConfigurationElement;
017: import org.eclipse.core.runtime.IExtension;
018: import org.eclipse.core.runtime.IExtensionPoint;
019: import org.eclipse.core.runtime.Platform;
020: import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
021: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
022: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.WorkbenchPlugin;
025:
026: /**
027: * The registry of status handlers extensions.
028: *
029: * @since 3.3
030: *
031: */
032: public class StatusHandlerRegistry implements IExtensionChangeHandler {
033:
034: private static final String STATUSHANDLERS_POINT_NAME = "statusHandlers"; //$NON-NLS-1$
035:
036: private static final String TAG_STATUSHANDLER = "statusHandler"; //$NON-NLS-1$
037:
038: private static final String TAG_STATUSHANDLER_PRODUCTBINDING = "statusHandlerProductBinding"; //$NON-NLS-1$
039:
040: private ArrayList statusHandlerDescriptors = new ArrayList();
041:
042: private ArrayList productBindingDescriptors = new ArrayList();
043:
044: private StatusHandlerDescriptorsMap statusHandlerDescriptorsMap;
045:
046: private StatusHandlerDescriptor defaultHandlerDescriptor;
047:
048: private static StatusHandlerRegistry instance;
049:
050: /**
051: * Creates an instance of the class.
052: */
053: private StatusHandlerRegistry() {
054: IExtensionTracker tracker = PlatformUI.getWorkbench()
055: .getExtensionTracker();
056: IExtensionPoint handlersPoint = Platform.getExtensionRegistry()
057: .getExtensionPoint(WorkbenchPlugin.PI_WORKBENCH,
058: STATUSHANDLERS_POINT_NAME);
059: IExtension[] extensions = handlersPoint.getExtensions();
060:
061: statusHandlerDescriptorsMap = new StatusHandlerDescriptorsMap();
062:
063: // initial population
064: for (int i = 0; i < extensions.length; i++) {
065: addExtension(tracker, extensions[i]);
066: }
067:
068: tracker.registerHandler(this , ExtensionTracker
069: .createExtensionPointFilter(handlersPoint));
070:
071: // registers on products ext. point to, needed
072: // for changing the default handler if product is changed
073: IExtensionPoint productsPoint = Platform.getExtensionRegistry()
074: .getExtensionPoint(Platform.PI_RUNTIME,
075: Platform.PT_PRODUCT);
076:
077: tracker.registerHandler(this , ExtensionTracker
078: .createExtensionPointFilter(productsPoint));
079: }
080:
081: /**
082: * Returns StatusHandlerRegistry singleton instance.
083: *
084: * @return StatusHandlerRegistry instance
085: */
086: public static StatusHandlerRegistry getDefault() {
087: if (instance == null) {
088: instance = new StatusHandlerRegistry();
089: }
090: return instance;
091: }
092:
093: /*
094: * (non-Javadoc)
095: *
096: * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker,
097: * org.eclipse.core.runtime.IExtension)
098: */
099: public void addExtension(IExtensionTracker tracker,
100: 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[] 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 getHandlerDescriptors(String pluginId) {
157: return statusHandlerDescriptorsMap
158: .getHandlerDescriptors(pluginId);
159: }
160:
161: /**
162: * Returns status handler descriptor for given id.
163: *
164: * @param statusHandlerId
165: * the id to get for
166: * @return the status handler descriptor
167: */
168: public StatusHandlerDescriptor getHandlerDescriptor(
169: String statusHandlerId) {
170: StatusHandlerDescriptor descriptor = null;
171: for (Iterator it = statusHandlerDescriptors.iterator(); it
172: .hasNext();) {
173: descriptor = (StatusHandlerDescriptor) it.next();
174: if (descriptor.getId().equals(statusHandlerId)) {
175: return descriptor;
176: }
177: }
178:
179: if (defaultHandlerDescriptor != null
180: && defaultHandlerDescriptor.getId().equals(
181: statusHandlerId)) {
182: return defaultHandlerDescriptor;
183: }
184:
185: return null;
186: }
187:
188: /**
189: * Disposes the registry.
190: */
191: public void dispose() {
192: PlatformUI.getWorkbench().getExtensionTracker()
193: .unregisterHandler(this );
194: }
195:
196: /**
197: * Sets the default product handler descriptor if product exists and binding
198: * is defined and creates handler descriptors tree due to the prefix policy.
199: */
200: private void buildHandlersStructure() {
201: statusHandlerDescriptorsMap.clear();
202: defaultHandlerDescriptor = null;
203:
204: String productId = Platform.getProduct() != null ? Platform
205: .getProduct().getId() : null;
206:
207: List allHandlers = new ArrayList();
208:
209: String defaultHandlerId = null;
210:
211: for (Iterator it = productBindingDescriptors.iterator(); it
212: .hasNext();) {
213: StatusHandlerProductBindingDescriptor descriptor = ((StatusHandlerProductBindingDescriptor) it
214: .next());
215:
216: if (descriptor.getProductId().equals(productId)) {
217: defaultHandlerId = descriptor.getHandlerId();
218: }
219: }
220:
221: for (Iterator it = statusHandlerDescriptors.iterator(); it
222: .hasNext();) {
223: StatusHandlerDescriptor descriptor = ((StatusHandlerDescriptor) it
224: .next());
225:
226: allHandlers.add(descriptor);
227: }
228:
229: StatusHandlerDescriptor handlerDescriptor = null;
230:
231: for (Iterator it = allHandlers.iterator(); it.hasNext();) {
232: handlerDescriptor = (StatusHandlerDescriptor) it.next();
233:
234: if (handlerDescriptor.getId().equals(defaultHandlerId)) {
235: defaultHandlerDescriptor = handlerDescriptor;
236: } else {
237: statusHandlerDescriptorsMap
238: .addHandlerDescriptor(handlerDescriptor);
239: }
240: }
241: }
242: }
|