1 11 package org.eclipse.ui.internal.decorators; 12 13 import java.util.ArrayList ; 14 import java.util.Collection ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.IExtension; 18 import org.eclipse.core.runtime.ISafeRunnable; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.core.runtime.Platform; 21 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker; 22 import org.eclipse.ui.internal.ObjectContributorManager; 23 import org.eclipse.ui.internal.WorkbenchPlugin; 24 import org.eclipse.ui.internal.misc.StatusUtil; 25 import org.eclipse.ui.internal.util.Util; 26 27 31 public class LightweightDecoratorManager extends ObjectContributorManager { 32 33 39 40 private class LightweightRunnable implements ISafeRunnable { 41 private Object element; 42 43 private DecorationBuilder decoration; 44 45 private LightweightDecoratorDefinition decorator; 46 47 void setValues(Object object, DecorationBuilder builder, 48 LightweightDecoratorDefinition definition) { 49 element = object; 50 decoration = builder; 51 decorator = definition; 52 53 } 54 55 58 public void handleException(Throwable exception) { 59 IStatus status = StatusUtil.newStatus(IStatus.ERROR, exception 60 .getMessage(), exception); 61 WorkbenchPlugin.log("Exception in Decorator", status); if (decorator != null) { 63 decorator.crashDisable(); 64 } 65 } 66 67 70 public void run() throws Exception { 71 decorator.decorate(element, decoration); 72 } 73 74 78 void clearReferences() { 79 decorator = null; 80 } 81 } 82 83 private LightweightRunnable runnable = new LightweightRunnable(); 84 85 private LightweightDecoratorDefinition[] lightweightDefinitions; 87 88 private static final LightweightDecoratorDefinition[] EMPTY_LIGHTWEIGHT_DEF = new LightweightDecoratorDefinition[0]; 89 90 private OverlayCache overlayCache = new OverlayCache(); 91 92 LightweightDecoratorManager(LightweightDecoratorDefinition[] definitions) { 93 super(); 94 lightweightDefinitions = definitions; 95 buildContributors(); 96 } 97 98 102 LightweightDecoratorDefinition[] getDefinitions() { 103 return lightweightDefinitions; 104 } 105 106 110 private void buildContributors() { 111 for (int i = 0; i < lightweightDefinitions.length; i++) { 112 LightweightDecoratorDefinition decorator = lightweightDefinitions[i]; 113 String [] types = getTargetTypes(decorator); 114 for (int j = 0; j < types.length; j++) { 115 registerContributor(decorator,types[j]); 116 } 117 } 118 } 119 120 127 public boolean addDecorator(LightweightDecoratorDefinition decorator) { 128 if (getLightweightDecoratorDefinition(decorator.getId()) == null) { 129 LightweightDecoratorDefinition[] oldDefs = lightweightDefinitions; 130 lightweightDefinitions = new LightweightDecoratorDefinition[lightweightDefinitions.length + 1]; 131 System.arraycopy(oldDefs, 0, lightweightDefinitions, 0, 132 oldDefs.length); 133 lightweightDefinitions[oldDefs.length] = decorator; 134 String [] types = getTargetTypes(decorator); 136 for (int i = 0; i < types.length; i++) { 137 registerContributor(decorator,types[i]); 138 } 139 return true; 140 } 141 return false; 142 } 143 144 149 private String [] getTargetTypes(LightweightDecoratorDefinition decorator) { 150 return decorator.getObjectClasses(); 151 } 152 153 159 public boolean removeDecorator(LightweightDecoratorDefinition decorator) { 160 int idx = getLightweightDecoratorDefinitionIdx(decorator.getId()); 161 if (idx != -1) { 162 LightweightDecoratorDefinition[] oldDefs = lightweightDefinitions; 163 Util 164 .arrayCopyWithRemoval( 165 oldDefs, 166 lightweightDefinitions = new LightweightDecoratorDefinition[lightweightDefinitions.length - 1], 167 idx); 168 String [] types = getTargetTypes(decorator); 170 for (int i = 0; i < types.length; i++) { 171 unregisterContributor(decorator,types[i]); 172 173 } 174 return true; 175 } 176 return false; 177 } 178 179 185 private LightweightDecoratorDefinition getLightweightDecoratorDefinition( 186 String decoratorId) { 187 int idx = getLightweightDecoratorDefinitionIdx(decoratorId); 188 if (idx != -1) { 189 return lightweightDefinitions[idx]; 190 } 191 return null; 192 } 193 194 201 private int getLightweightDecoratorDefinitionIdx( 202 String decoratorId) { 203 for (int i = 0; i < lightweightDefinitions.length; i++) { 204 if (lightweightDefinitions[i].getId().equals(decoratorId)) { 205 return i; 206 } 207 } 208 return -1; 209 } 210 211 215 LightweightDecoratorDefinition[] enabledDefinitions() { 216 ArrayList result = new ArrayList (); 217 for (int i = 0; i < lightweightDefinitions.length; i++) { 218 if (lightweightDefinitions[i].isEnabled()) { 219 result.add(lightweightDefinitions[i]); 220 } 221 } 222 LightweightDecoratorDefinition[] returnArray = new LightweightDecoratorDefinition[result 223 .size()]; 224 result.toArray(returnArray); 225 return returnArray; 226 } 227 228 232 boolean hasEnabledDefinitions() { 233 for (int i = 0; i < lightweightDefinitions.length; i++) { 234 if (lightweightDefinitions[i].isEnabled()) { 235 return true; 236 } 237 } 238 return false; 239 } 240 241 244 void reset() { 245 runnable.clearReferences(); 246 } 247 248 253 void shutdown() { 254 for (int i = 0; i < lightweightDefinitions.length; i++) { 257 if (lightweightDefinitions[i].isEnabled()) { 258 lightweightDefinitions[i].setEnabled(false); 259 } 260 } 261 overlayCache.disposeAll(); 262 } 263 264 269 LightweightDecoratorDefinition getDecoratorDefinition(String decoratorId) { 270 for (int i = 0; i < lightweightDefinitions.length; i++) { 271 if (lightweightDefinitions[i].getId().equals(decoratorId)) { 272 return lightweightDefinitions[i]; 273 } 274 } 275 return null; 276 } 277 278 281 LightweightDecoratorDefinition[] getDecoratorsFor(Object element) { 282 283 if (element == null) { 284 return EMPTY_LIGHTWEIGHT_DEF; 285 } 286 287 List elements = new ArrayList (1); 288 elements.add(element); 289 LightweightDecoratorDefinition[] decoratorArray = EMPTY_LIGHTWEIGHT_DEF; 290 List contributors = getContributors(elements); 291 if (!contributors.isEmpty()) { 292 Collection decorators = DecoratorManager.getDecoratorsFor(element, 293 (DecoratorDefinition[]) contributors.toArray(new DecoratorDefinition[contributors.size()])); 294 if (decorators.size() > 0) { 295 decoratorArray = new LightweightDecoratorDefinition[decorators 296 .size()]; 297 decorators.toArray(decoratorArray); 298 } 299 } 300 301 return decoratorArray; 302 } 303 304 313 public void getDecorations(Object element, DecorationBuilder decoration) { 314 315 LightweightDecoratorDefinition[] decorators = getDecoratorsFor(element); 316 317 for (int i = 0; i < decorators.length; i++) { 318 LightweightDecoratorDefinition dd = decorators[i]; 321 decoration.setCurrentDefinition(dd); 322 decorate(element, decoration, dd); 323 } 324 } 325 326 332 private void decorate(Object element, DecorationBuilder decoration, 333 LightweightDecoratorDefinition decorator) { 334 335 runnable.setValues(element, decoration, decorator); 336 Platform.run(runnable); 337 } 338 339 343 OverlayCache getOverlayCache() { 344 return overlayCache; 345 } 346 347 352 public DecorationResult getDecorationResult(Object object) { 353 DecorationBuilder builder = new DecorationBuilder(); 354 getDecorations(object, builder); 355 return builder.createResult(); 356 357 } 358 359 362 public void addExtension(IExtensionTracker tracker, IExtension extension) { 363 } 367 368 371 protected boolean canHandleExtensionTracking() { 372 return false; 373 } 374 375 } 376 | Popular Tags |