001: /*******************************************************************************
002: * Copyright (c) 2004, 2006 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.activities.ws;
011:
012: import org.eclipse.core.runtime.IConfigurationElement;
013: import org.eclipse.core.runtime.IExtension;
014: import org.eclipse.core.runtime.IExtensionPoint;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
017: import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
018: import org.eclipse.jface.resource.ImageDescriptor;
019: import org.eclipse.jface.resource.ImageRegistry;
020: import org.eclipse.ui.PlatformUI;
021: import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
022: import org.eclipse.ui.plugin.AbstractUIPlugin;
023:
024: /**
025: * @since 3.1
026: */
027: public class ImageBindingRegistry implements IExtensionChangeHandler {
028: private String tag;
029: private ImageRegistry registry = new ImageRegistry();
030:
031: /**
032: * @param tag
033: *
034: */
035: public ImageBindingRegistry(String tag) {
036: super ();
037: this .tag = tag;
038: IExtension[] extensions = getExtensionPointFilter()
039: .getExtensions();
040: for (int i = 0; i < extensions.length; i++) {
041: addExtension(PlatformUI.getWorkbench()
042: .getExtensionTracker(), extensions[i]);
043: }
044: }
045:
046: /* (non-Javadoc)
047: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
048: */
049: public void addExtension(IExtensionTracker tracker,
050: IExtension extension) {
051: IConfigurationElement[] elements = extension
052: .getConfigurationElements();
053: for (int i = 0; i < elements.length; i++) {
054: IConfigurationElement element = elements[i];
055: if (element.getName().equals(tag)) {
056: String id = element
057: .getAttribute(IWorkbenchRegistryConstants.ATT_ID);
058: String file = element
059: .getAttribute(IWorkbenchRegistryConstants.ATT_ICON);
060: if (file == null || id == null) {
061: continue; //ignore - malformed
062: }
063: if (registry.getDescriptor(id) == null) { // first come, first serve
064: ImageDescriptor descriptor = AbstractUIPlugin
065: .imageDescriptorFromPlugin(element
066: .getNamespace(), file);
067: if (descriptor != null) {
068: registry.put(id, descriptor);
069: tracker.registerObject(extension, id,
070: IExtensionTracker.REF_WEAK);
071: }
072: }
073: }
074: }
075:
076: }
077:
078: /**
079: * Return the activity support extension point that this registry is interested in.
080: *
081: * @return the extension point
082: */
083: public IExtensionPoint getExtensionPointFilter() {
084: return Platform.getExtensionRegistry().getExtensionPoint(
085: PlatformUI.PLUGIN_ID,
086: IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT);
087: }
088:
089: /* (non-Javadoc)
090: * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
091: */
092: public void removeExtension(IExtension extension, Object[] objects) {
093: for (int i = 0; i < objects.length; i++) {
094: if (objects[i] instanceof String) {
095: registry.remove((String) objects[i]);
096: }
097: }
098: }
099:
100: /**
101: * Get the ImageDescriptor for the given id.
102: *
103: * @param id the id
104: * @return the descriptor
105: */
106: public ImageDescriptor getImageDescriptor(String id) {
107: return registry.getDescriptor(id);
108: }
109:
110: /**
111: * Dispose of this registry.
112: */
113: void dispose() {
114: registry.dispose();
115: }
116:
117: }
|