001: /*******************************************************************************
002: * Copyright (c) 2000, 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.ide.registry;
011:
012: import java.net.URL;
013: import java.util.ArrayList;
014:
015: import org.eclipse.core.resources.IMarker;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IConfigurationElement;
018: import org.eclipse.core.runtime.IStatus;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.core.runtime.Platform;
021: import org.eclipse.core.runtime.Status;
022: import org.eclipse.jface.resource.ImageDescriptor;
023: import org.eclipse.ui.PlatformUI;
024: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
025: import org.eclipse.ui.internal.ide.IMarkerImageProvider;
026: import org.osgi.framework.Bundle;
027:
028: /**
029: * Implementation of a marker image registry which maps either
030: * a marker type to a provider or to a static image.
031: */
032: public class MarkerImageProviderRegistry {
033: private static final String ATT_PROVIDER_CLASS = "class";//$NON-NLS-1$
034:
035: private static final String ATT_ICON = "icon";//$NON-NLS-1$
036:
037: private static final String ATT_MARKER_TYPE = "markertype";//$NON-NLS-1$
038:
039: private static final String ATT_ID = "id";//$NON-NLS-1$
040:
041: private static final String MARKER_ATT_KEY = "org.eclipse.ui.internal.registry.MarkerImageProviderRegistry";//$NON-NLS-1$
042:
043: private static final String TAG_PROVIDER = "imageprovider";//$NON-NLS-1$
044:
045: private ArrayList descriptors = new ArrayList();
046:
047: class Descriptor {
048: String id;
049:
050: String markerType;
051:
052: String className;
053:
054: String imagePath;
055:
056: ImageDescriptor imageDescriptor;
057:
058: IConfigurationElement element;
059:
060: Bundle pluginBundle;
061:
062: IMarkerImageProvider provider;
063: }
064:
065: /**
066: * Initialize this new MarkerImageProviderRegistry.
067: */
068: public MarkerImageProviderRegistry() {
069: class MarkerImageReader extends IDERegistryReader {
070: protected boolean readElement(IConfigurationElement element) {
071: if (element.getName().equals(TAG_PROVIDER)) {
072: addProvider(element);
073: return true;
074: }
075:
076: return false;
077: }
078:
079: public void readRegistry() {
080: readRegistry(Platform.getExtensionRegistry(),
081: IDEWorkbenchPlugin.IDE_WORKBENCH,
082: IDEWorkbenchPlugin.PL_MARKER_IMAGE_PROVIDER);
083: }
084: }
085:
086: new MarkerImageReader().readRegistry();
087: }
088:
089: /**
090: * Creates a descriptor for the marker provider extension
091: * and add it to the list of providers.
092: */
093: public void addProvider(IConfigurationElement element) {
094: Descriptor desc = new Descriptor();
095: desc.element = element;
096: desc.pluginBundle = Platform.getBundle(element.getNamespace());
097: desc.id = element.getAttribute(ATT_ID);
098: desc.markerType = element.getAttribute(ATT_MARKER_TYPE);
099: desc.imagePath = element.getAttribute(ATT_ICON);
100: desc.className = element.getAttribute(ATT_PROVIDER_CLASS);
101: if (desc.imagePath != null) {
102: desc.imageDescriptor = getImageDescriptor(desc);
103: }
104: if (desc.className == null) {
105: //Don't need to keep these references.
106: desc.element = null;
107: desc.pluginBundle = null;
108: }
109: descriptors.add(desc);
110: }
111:
112: /**
113: * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(Object)
114: */
115: public ImageDescriptor getImageDescriptor(IMarker marker) {
116: int size = descriptors.size();
117: for (int i = 0; i < size; i++) {
118: Descriptor desc = (Descriptor) descriptors.get(i);
119: try {
120: if (marker.isSubtypeOf(desc.markerType)) {
121: if (desc.className != null) {
122: if (desc.pluginBundle.getState() == Bundle.ACTIVE) {
123: //-- Get the image descriptor from the provider.
124: //-- Save the image descriptor url as a persistable property, so a
125: //image descriptor can be created without activating the plugin next
126: //time the workbench is started.
127: if (desc.provider == null) {
128: desc.provider = (IMarkerImageProvider) IDEWorkbenchPlugin
129: .createExtension(desc.element,
130: ATT_PROVIDER_CLASS);
131: }
132: String path = desc.provider
133: .getImagePath(marker);
134: if (path != desc.imagePath) {
135: desc.imagePath = path;
136: desc.imageDescriptor = getImageDescriptor(desc);
137: return desc.imageDescriptor;
138: }
139: return desc.imageDescriptor;
140: } else {
141: if (desc.imageDescriptor == null) {
142: //Create a image descriptor to be used until the plugin gets activated.
143: desc.imagePath = (String) marker
144: .getAttribute(MARKER_ATT_KEY);
145: desc.imageDescriptor = getImageDescriptor(desc);
146: }
147: return desc.imageDescriptor;
148: }
149: } else if (desc.imageDescriptor != null) {
150: return desc.imageDescriptor;
151: }
152: }
153: } catch (CoreException e) {
154: IDEWorkbenchPlugin
155: .getDefault()
156: .getLog()
157: .log(
158: new Status(
159: IStatus.ERROR,
160: PlatformUI.PLUGIN_ID,
161: 0,
162: "Exception creating image descriptor for: " + desc.markerType,//$NON-NLS-1$
163: e));
164: return null;
165: }
166: }
167: return null;
168: }
169:
170: /**
171: * Returns the image descriptor with the given relative path.
172: */
173: ImageDescriptor getImageDescriptor(Descriptor desc) {
174: URL url = Platform.find(desc.pluginBundle, new Path(
175: desc.imagePath));
176: return ImageDescriptor.createFromURL(url);
177: }
178: }
|