KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > registry > MarkerImageProviderRegistry


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 package org.eclipse.ui.internal.ide.registry;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IMarker;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IConfigurationElement;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.core.runtime.Platform;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.resource.ImageDescriptor;
24 import org.eclipse.ui.PlatformUI;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
26 import org.eclipse.ui.internal.ide.IMarkerImageProvider;
27 import org.osgi.framework.Bundle;
28
29 /**
30  * Implementation of a marker image registry which maps either
31  * a marker type to a provider or to a static image.
32  */

33 public class MarkerImageProviderRegistry {
34     private static final String JavaDoc ATT_PROVIDER_CLASS = "class";//$NON-NLS-1$
35

36     private static final String JavaDoc ATT_ICON = "icon";//$NON-NLS-1$
37

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

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

42     private static final String JavaDoc MARKER_ATT_KEY = "org.eclipse.ui.internal.registry.MarkerImageProviderRegistry";//$NON-NLS-1$
43

44     private static final String JavaDoc TAG_PROVIDER = "imageprovider";//$NON-NLS-1$
45

46     private ArrayList JavaDoc descriptors = new ArrayList JavaDoc();
47
48     class Descriptor {
49         String JavaDoc id;
50
51         String JavaDoc markerType;
52
53         String JavaDoc className;
54
55         String JavaDoc imagePath;
56
57         ImageDescriptor imageDescriptor;
58
59         IConfigurationElement element;
60
61         Bundle pluginBundle;
62
63         IMarkerImageProvider provider;
64     }
65
66     /**
67      * Initialize this new MarkerImageProviderRegistry.
68      */

69     public MarkerImageProviderRegistry() {
70         class MarkerImageReader extends IDERegistryReader {
71             protected boolean readElement(IConfigurationElement element) {
72                 if (element.getName().equals(TAG_PROVIDER)) {
73                     addProvider(element);
74                     return true;
75                 }
76
77                 return false;
78             }
79
80             public void readRegistry() {
81                 readRegistry(Platform.getExtensionRegistry(),
82                         IDEWorkbenchPlugin.IDE_WORKBENCH,
83                         IDEWorkbenchPlugin.PL_MARKER_IMAGE_PROVIDER);
84             }
85         }
86
87         new MarkerImageReader().readRegistry();
88     }
89
90     /**
91      * Creates a descriptor for the marker provider extension
92      * and add it to the list of providers.
93      */

94     public void addProvider(IConfigurationElement element) {
95         Descriptor desc = new Descriptor();
96         desc.element = element;
97         desc.pluginBundle = Platform.getBundle(element.getNamespace());
98         desc.id = element.getAttribute(ATT_ID);
99         desc.markerType = element.getAttribute(ATT_MARKER_TYPE);
100         desc.imagePath = element.getAttribute(ATT_ICON);
101         desc.className = element.getAttribute(ATT_PROVIDER_CLASS);
102         if (desc.imagePath != null) {
103             desc.imageDescriptor = getImageDescriptor(desc);
104         }
105         if (desc.className == null) {
106             //Don't need to keep these references.
107
desc.element = null;
108             desc.pluginBundle = null;
109         }
110         descriptors.add(desc);
111     }
112
113     /**
114      * @see org.eclipse.ui.model.IWorkbenchAdapter#getImageDescriptor(Object)
115      */

116     public ImageDescriptor getImageDescriptor(IMarker marker) {
117         int size = descriptors.size();
118         for (int i = 0; i < size; i++) {
119             Descriptor desc = (Descriptor) descriptors.get(i);
120             try {
121                 if (marker.isSubtypeOf(desc.markerType)) {
122                     if (desc.className != null) {
123                         if (desc.pluginBundle.getState()==Bundle.ACTIVE) {
124                             //-- Get the image descriptor from the provider.
125
//-- Save the image descriptor url as a persistable property, so a
126
//image descriptor can be created without activating the plugin next
127
//time the workbench is started.
128
if (desc.provider == null) {
129                                 desc.provider = (IMarkerImageProvider) IDEWorkbenchPlugin
130                                         .createExtension(desc.element,
131                                                 ATT_PROVIDER_CLASS);
132                             }
133                             String JavaDoc path = desc.provider.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 JavaDoc) 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 JavaDoc url = Platform.find(desc.pluginBundle, new Path(desc.imagePath));
175        return ImageDescriptor.createFromURL(url);
176     }
177 }
178
Popular Tags