1 /******************************************************************************* 2 * Copyright (c) 2004, 2019 IBM Corporation and others. 3 * 4 * This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * which accompanies this distribution, and is available at 7 * https://www.eclipse.org/legal/epl-2.0/ 8 * 9 * SPDX-License-Identifier: EPL-2.0 10 * 11 * Contributors: 12 * IBM Corporation - initial API and implementation 13 * Alexander Fedorov <alexander.fedorov@arsysop.ru> - Bug 548799 14 *******************************************************************************/ 15 package org.eclipse.ui.internal.activities.ws; 16 17 import org.eclipse.core.runtime.IConfigurationElement; 18 import org.eclipse.core.runtime.IExtension; 19 import org.eclipse.core.runtime.IExtensionPoint; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler; 22 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.jface.resource.ImageRegistry; 25 import org.eclipse.jface.resource.ResourceLocator; 26 import org.eclipse.ui.PlatformUI; 27 import org.eclipse.ui.internal.activities.Persistence; 28 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants; 29 30 /** 31 * @since 3.1 32 */ 33 public class ImageBindingRegistry implements IExtensionChangeHandler { 34 private String tag; 35 private ImageRegistry registry = new ImageRegistry(); 36 37 /** 38 * @param tag 39 * 40 */ ImageBindingRegistry(String tag)41 public ImageBindingRegistry(String tag) { 42 super(); 43 this.tag = tag; 44 IExtension[] extensions = getExtensionPointFilter().getExtensions(); 45 for (IExtension extension : extensions) { 46 addExtension(PlatformUI.getWorkbench().getExtensionTracker(), extension); 47 } 48 } 49 50 @Override addExtension(IExtensionTracker tracker, IExtension extension)51 public void addExtension(IExtensionTracker tracker, IExtension extension) { 52 IConfigurationElement[] elements = extension.getConfigurationElements(); 53 for (IConfigurationElement element : elements) { 54 if (element.getName().equals(tag)) { 55 String id = element.getAttribute(IWorkbenchRegistryConstants.ATT_ID); 56 String file = element.getAttribute(IWorkbenchRegistryConstants.ATT_ICON); 57 if (file == null || id == null) { 58 Persistence.log(element, Persistence.ACTIVITY_IMAGE_BINDING_DESC, 59 "definition must contain icon and ID"); //$NON-NLS-1$ 60 continue; // ignore - malformed 61 } 62 if (registry.getDescriptor(id) == null) { // first come, first serve 63 ResourceLocator.imageDescriptorFromBundle(element.getContributor().getName(), file).ifPresent(d -> { 64 registry.put(id, d); 65 tracker.registerObject(extension, id, IExtensionTracker.REF_WEAK); 66 }); 67 } 68 } 69 } 70 71 } 72 73 /** 74 * Return the activity support extension point that this registry is interested 75 * in. 76 * 77 * @return the extension point 78 */ getExtensionPointFilter()79 public IExtensionPoint getExtensionPointFilter() { 80 return Platform.getExtensionRegistry().getExtensionPoint(PlatformUI.PLUGIN_ID, 81 IWorkbenchRegistryConstants.PL_ACTIVITYSUPPORT); 82 } 83 84 @Override removeExtension(IExtension extension, Object[] objects)85 public void removeExtension(IExtension extension, Object[] objects) { 86 for (Object object : objects) { 87 if (object instanceof String) { 88 registry.remove((String) object); 89 } 90 } 91 } 92 93 /** 94 * Get the ImageDescriptor for the given id. 95 * 96 * @param id the id 97 * @return the descriptor 98 */ getImageDescriptor(String id)99 public ImageDescriptor getImageDescriptor(String id) { 100 return registry.getDescriptor(id); 101 } 102 103 /** 104 * Dispose of this registry. 105 */ dispose()106 void dispose() { 107 registry.dispose(); 108 } 109 110 } 111