1 11 package org.eclipse.ui.internal.ide.registry; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 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 33 public class MarkerImageProviderRegistry { 34 private static final String ATT_PROVIDER_CLASS = "class"; 36 private static final String ATT_ICON = "icon"; 38 private static final String ATT_MARKER_TYPE = "markertype"; 40 private static final String ATT_ID = "id"; 42 private static final String MARKER_ATT_KEY = "org.eclipse.ui.internal.registry.MarkerImageProviderRegistry"; 44 private static final String TAG_PROVIDER = "imageprovider"; 46 private ArrayList descriptors = new ArrayList (); 47 48 class Descriptor { 49 String id; 50 51 String markerType; 52 53 String className; 54 55 String imagePath; 56 57 ImageDescriptor imageDescriptor; 58 59 IConfigurationElement element; 60 61 Bundle pluginBundle; 62 63 IMarkerImageProvider provider; 64 } 65 66 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 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 desc.element = null; 108 desc.pluginBundle = null; 109 } 110 descriptors.add(desc); 111 } 112 113 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 if (desc.provider == null) { 129 desc.provider = (IMarkerImageProvider) IDEWorkbenchPlugin 130 .createExtension(desc.element, 131 ATT_PROVIDER_CLASS); 132 } 133 String 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 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, e)); 164 return null; 165 } 166 } 167 return null; 168 } 169 170 173 ImageDescriptor getImageDescriptor(Descriptor desc) { 174 URL url = Platform.find(desc.pluginBundle, new Path(desc.imagePath)); 175 return ImageDescriptor.createFromURL(url); 176 } 177 } 178 | Popular Tags |