1 11 package org.eclipse.ui.internal.decorators; 12 13 import org.eclipse.core.runtime.CoreException; 14 import org.eclipse.core.runtime.IConfigurationElement; 15 import org.eclipse.core.runtime.ISafeRunnable; 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.jface.viewers.IBaseLabelProvider; 18 import org.eclipse.jface.viewers.IDecoration; 19 import org.eclipse.jface.viewers.ILightweightLabelDecorator; 20 import org.eclipse.ui.internal.ActionExpression; 21 import org.eclipse.ui.internal.IObjectContributor; 22 import org.eclipse.ui.internal.LegacyResourceSupport; 23 import org.eclipse.ui.internal.WorkbenchPlugin; 24 25 30 class LightweightDecoratorDefinition extends DecoratorDefinition implements 31 IObjectContributor { 32 33 private static final String ATT_LOCATION = "location"; 35 static final String ATT_ICON = "icon"; 37 41 public static final int TOP_LEFT = 0; 42 43 46 public static final int TOP_RIGHT = 1; 47 48 51 public static final int BOTTOM_LEFT = 2; 52 53 56 public static final int BOTTOM_RIGHT = 3; 57 58 61 public static final int UNDERLAY = 4; 62 63 private static final String UNDERLAY_STRING = "UNDERLAY"; 65 private static final String ATT_QUADRANT = "quadrant"; 67 private static final String TOP_LEFT_STRING = "TOP_LEFT"; 70 private static final String TOP_RIGHT_STRING = "TOP_RIGHT"; 72 private static final String BOTTOM_LEFT_STRING = "BOTTOM_LEFT"; 74 78 private ILightweightLabelDecorator decorator; 79 80 private int quadrant; 81 82 private boolean hasReadQuadrant; 83 84 private String [] objectClasses; 85 86 LightweightDecoratorDefinition(String identifier, 87 IConfigurationElement element) { 88 super(identifier, element); 89 } 90 91 98 protected ILightweightLabelDecorator internalGetDecorator() 99 throws CoreException { 100 if (labelProviderCreationFailed) { 101 return null; 102 } 103 104 final CoreException[] exceptions = new CoreException[1]; 105 106 if (decorator == null) { 107 108 if (isDeclarative()) { 109 decorator = new DeclarativeDecorator(definingElement, 110 getIconLocation()); 111 } else { 112 113 Platform.run(new ISafeRunnable() { 114 public void run() { 115 try { 116 decorator = (ILightweightLabelDecorator) WorkbenchPlugin 117 .createExtension(definingElement, 118 DecoratorDefinition.ATT_CLASS); 119 decorator.addListener(WorkbenchPlugin.getDefault() 120 .getDecoratorManager()); 121 } catch (CoreException exception) { 122 exceptions[0] = exception; 123 } 124 } 125 126 129 public void handleException(Throwable e) { 130 } 132 }); 133 } 134 } else { 135 return decorator; 136 } 137 138 if (decorator == null) { 139 this.labelProviderCreationFailed = true; 140 setEnabled(false); 141 } 142 143 if (exceptions[0] != null) { 144 throw exceptions[0]; 145 } 146 147 return decorator; 148 } 149 150 155 private boolean isDeclarative() { 156 return definingElement.getAttribute(DecoratorDefinition.ATT_CLASS) == null; 157 } 158 159 164 private String getIconLocation() { 165 return definingElement.getAttribute(ATT_ICON); 166 } 167 168 173 protected IBaseLabelProvider internalGetLabelProvider() 174 throws CoreException { 175 return internalGetDecorator(); 176 } 177 178 183 public boolean isFull() { 184 return false; 185 } 186 187 194 public int getQuadrant() { 195 if (!hasReadQuadrant) { 196 hasReadQuadrant = true; 197 quadrant = getLocationConstant(definingElement 198 .getAttribute(ATT_LOCATION), definingElement); 199 } 200 return quadrant; 201 } 202 203 209 private int getLocationConstant(String locationDefinition, 210 IConfigurationElement element) { 211 212 if (locationDefinition == null) { 214 locationDefinition = element.getAttribute(ATT_QUADRANT); 215 } 216 217 if (TOP_RIGHT_STRING.equals(locationDefinition)) { 218 return TOP_RIGHT; 219 } 220 if (TOP_LEFT_STRING.equals(locationDefinition)) { 221 return TOP_LEFT; 222 } 223 if (BOTTOM_LEFT_STRING.equals(locationDefinition)) { 224 return BOTTOM_LEFT; 225 } 226 if (UNDERLAY_STRING.equals(locationDefinition)) { 227 return UNDERLAY; 228 } 229 return BOTTOM_RIGHT; 230 231 } 232 233 238 public void decorate(Object element, IDecoration decoration) { 239 try { 240 ILightweightLabelDecorator currentDecorator = internalGetDecorator(); 242 if(currentDecorator == null) { 243 return; 244 } 245 246 if (isAdaptable()) { 247 String [] classes = getObjectClasses(); 248 for (int i = 0; i < classes.length; i++) { 249 String className = classes[i]; 250 Object adapted = LegacyResourceSupport.getAdapter(element, 251 className); 252 if (adapted != null) { 253 currentDecorator.decorate(adapted, decoration); 254 } 255 } 256 } 257 else{ 258 if (currentDecorator != null && element != null) { 259 currentDecorator.decorate(element, decoration); 260 } 261 } 262 } catch (CoreException exception) { 263 handleCoreException(exception); 264 } 265 266 } 267 268 273 public ILightweightLabelDecorator getDecorator() { 274 return decorator; 275 } 276 277 282 protected void refreshDecorator() { 283 if (!this.enabled && decorator != null) { 286 IBaseLabelProvider cached = decorator; 287 decorator = null; 288 disposeCachedDecorator(cached); 289 } 290 } 291 292 297 public boolean isApplicableTo(Object object) { 298 return isEnabledFor(object); 299 } 300 301 306 public boolean canAdapt() { 307 return isAdaptable(); 308 } 309 310 316 public String [] getObjectClasses() { 317 if (objectClasses == null) { 318 getEnablement(); 319 } 320 return objectClasses; 321 } 322 323 328 protected void initializeEnablement() { 329 super.initializeEnablement(); 330 ActionExpression expression = getEnablement(); 331 if (expression != null) { 332 objectClasses = expression.extractObjectClasses(); 333 } 334 335 if (objectClasses == null) { 337 objectClasses = new String [] {Object .class.getName()}; 338 } 339 } 340 341 } 342 | Popular Tags |