KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > decorators > DecoratorManager


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.decorators;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.core.runtime.IConfigurationElement;
21 import org.eclipse.core.runtime.IExtension;
22 import org.eclipse.core.runtime.IExtensionPoint;
23 import org.eclipse.core.runtime.IProgressMonitor;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.ListenerList;
26 import org.eclipse.core.runtime.Platform;
27 import org.eclipse.core.runtime.Status;
28 import org.eclipse.core.runtime.dynamichelpers.ExtensionTracker;
29 import org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler;
30 import org.eclipse.core.runtime.dynamichelpers.IExtensionTracker;
31 import org.eclipse.jface.util.SafeRunnable;
32 import org.eclipse.jface.viewers.DecorationContext;
33 import org.eclipse.jface.viewers.IBaseLabelProvider;
34 import org.eclipse.jface.viewers.IColorDecorator;
35 import org.eclipse.jface.viewers.IDecorationContext;
36 import org.eclipse.jface.viewers.IDelayedLabelDecorator;
37 import org.eclipse.jface.viewers.IFontDecorator;
38 import org.eclipse.jface.viewers.ILabelDecorator;
39 import org.eclipse.jface.viewers.ILabelProviderListener;
40 import org.eclipse.jface.viewers.ILightweightLabelDecorator;
41 import org.eclipse.jface.viewers.LabelDecorator;
42 import org.eclipse.jface.viewers.LabelProviderChangedEvent;
43 import org.eclipse.swt.graphics.Color;
44 import org.eclipse.swt.graphics.Font;
45 import org.eclipse.swt.graphics.Image;
46 import org.eclipse.ui.IDecoratorManager;
47 import org.eclipse.ui.PlatformUI;
48 import org.eclipse.ui.internal.IPreferenceConstants;
49 import org.eclipse.ui.internal.LegacyResourceSupport;
50 import org.eclipse.ui.internal.Workbench;
51 import org.eclipse.ui.internal.WorkbenchMessages;
52 import org.eclipse.ui.internal.WorkbenchPlugin;
53 import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
54 import org.eclipse.ui.internal.util.PrefUtil;
55 import org.eclipse.ui.internal.util.Util;
56 import org.eclipse.ui.progress.WorkbenchJob;
57
58 /**
59  * The DecoratorManager is the class that handles all of the
60  * decorators defined in the image.
61  *
62  * @since 2.0
63  */

64 public class DecoratorManager extends LabelDecorator implements IDelayedLabelDecorator,
65         ILabelProviderListener, IDecoratorManager, IFontDecorator, IColorDecorator, IExtensionChangeHandler {
66
67     private static String JavaDoc EXTENSIONPOINT_UNIQUE_ID = WorkbenchPlugin.PI_WORKBENCH + "." + IWorkbenchRegistryConstants.PL_DECORATORS; //$NON-NLS-1$
68

69     /**
70      * The family for the decorate job.
71      */

72     public static final Object JavaDoc FAMILY_DECORATE = new Object JavaDoc();
73
74     private DecorationScheduler scheduler;
75
76     private LightweightDecoratorManager lightweightManager;
77
78     //Hold onto the list of listeners to be told if a change has occured
79
private ListenerList listeners = new ListenerList();
80
81     //The full definitions read from the registry.
82
//Initalize to an empty collection as this is rarely used now.
83
private FullDecoratorDefinition[] fullDefinitions;
84
85     private FullTextDecoratorRunnable fullTextRunnable = new FullTextDecoratorRunnable();
86
87     private FullImageDecoratorRunnable fullImageRunnable = new FullImageDecoratorRunnable();
88
89     private static final FullDecoratorDefinition[] EMPTY_FULL_DEF = new FullDecoratorDefinition[0];
90
91     private final String JavaDoc PREFERENCE_SEPARATOR = ","; //$NON-NLS-1$
92

93     private final String JavaDoc VALUE_SEPARATOR = ":"; //$NON-NLS-1$
94

95     private final String JavaDoc P_TRUE = "true"; //$NON-NLS-1$
96

97     private final String JavaDoc P_FALSE = "false"; //$NON-NLS-1$
98

99     /**
100      * Create a new instance of the receiver and load the
101      * settings from the installed plug-ins.
102      */

103     public DecoratorManager() {
104         
105         scheduler = new DecorationScheduler(this);
106         IExtensionTracker tracker = PlatformUI.getWorkbench()
107                 .getExtensionTracker();
108         tracker.registerHandler(this, ExtensionTracker.createExtensionPointFilter(getExtensionPointFilter()));
109     }
110
111     /**
112      * Initalize the decorator definitions.
113      */

114     private void initializeDecoratorDefinitions() {
115         DecoratorRegistryReader reader = new DecoratorRegistryReader();
116         Collection JavaDoc values = reader
117                 .readRegistry(Platform.getExtensionRegistry());
118
119         ArrayList JavaDoc full = new ArrayList JavaDoc();
120         ArrayList JavaDoc lightweight = new ArrayList JavaDoc();
121         Iterator JavaDoc allDefinitions = values.iterator();
122         IExtensionTracker configurationElementTracker = PlatformUI
123                 .getWorkbench().getExtensionTracker();
124         while (allDefinitions.hasNext()) {
125             DecoratorDefinition nextDefinition = (DecoratorDefinition) allDefinitions
126                     .next();
127             if (nextDefinition.isFull()) {
128                 full.add(nextDefinition);
129             } else {
130                 lightweight.add(nextDefinition);
131             }
132                         
133             configurationElementTracker.registerObject(nextDefinition.getConfigurationElement().getDeclaringExtension(), nextDefinition, IExtensionTracker.REF_WEAK);
134         }
135
136         fullDefinitions = new FullDecoratorDefinition[full.size()];
137         full.toArray(fullDefinitions);
138
139         LightweightDecoratorDefinition[] lightweightDefinitions = new LightweightDecoratorDefinition[lightweight
140                 .size()];
141         lightweight.toArray(lightweightDefinitions);
142
143         lightweightManager = new LightweightDecoratorManager(
144                 lightweightDefinitions);
145         
146         applyDecoratorsPreference();
147     }
148
149     /**
150      * For dynamic UI
151      *
152      * @param definition the definition to add
153      * @since 3.0
154      */

155     public void addDecorator(DecoratorDefinition definition) {
156         if (definition.isFull()) {
157             if (getFullDecoratorDefinition(definition.getId()) == null) {
158                 FullDecoratorDefinition[] oldDefs = getFullDefinitions();
159                 fullDefinitions = new FullDecoratorDefinition[fullDefinitions.length + 1];
160                 System
161                         .arraycopy(oldDefs, 0, fullDefinitions, 0,
162                                 oldDefs.length);
163                 fullDefinitions[oldDefs.length] = (FullDecoratorDefinition) definition;
164                 clearCaches();
165                 updateForEnablementChange();
166             }
167         } else {
168             if (getLightweightManager().addDecorator(
169                     (LightweightDecoratorDefinition) definition)) {
170                 clearCaches();
171                 updateForEnablementChange();
172             }
173         }
174         ((Workbench) PlatformUI.getWorkbench())
175                 .getExtensionTracker().registerObject(
176                         definition.getConfigurationElement().getDeclaringExtension(), definition, IExtensionTracker.REF_WEAK);
177     }
178
179     /**
180      * See if the supplied decorator cache has a value for the
181      * element. If not calculate it from the enabledDefinitions and
182      * update the cache.
183      * @return Collection of DecoratorDefinition.
184      * @param element The element being tested.
185      * @param enabledDefinitions The definitions currently defined for this decorator.
186      */

187     static Collection JavaDoc getDecoratorsFor(Object JavaDoc element,
188             DecoratorDefinition[] enabledDefinitions) {
189
190         ArrayList JavaDoc decorators = new ArrayList JavaDoc();
191
192         for (int i = 0; i < enabledDefinitions.length; i++) {
193             if (enabledDefinitions[i].isEnabledFor(element)) {
194                 decorators.add(enabledDefinitions[i]);
195             }
196         }
197
198         return decorators;
199
200     }
201
202
203     /**
204      * Add the listener to the list of listeners.
205      */

206     public void addListener(ILabelProviderListener listener) {
207         listeners.add(listener);
208     }
209
210     /**
211      * Remove the listener from the list.
212      */

213     public void removeListener(ILabelProviderListener listener) {
214         listeners.remove(listener);
215         scheduler.listenerRemoved(listener);
216     }
217     
218     /**
219      * Get the list of elements listening to the receiver.
220      * @return ILabelProviderListener []
221      */

222     ILabelProviderListener [] getListeners(){
223         Object JavaDoc[] array = listeners.getListeners();
224         ILabelProviderListener [] listenerArray =
225             new ILabelProviderListener [array.length];
226         System.arraycopy(array,0,listenerArray,0,listenerArray.length);
227         return listenerArray;
228     }
229
230     /**
231      * Inform all of the listeners that require an update
232      * @param listener The listener we are updating.
233      * @param event
234      * the event with the update details
235      */

236     void fireListener(final LabelProviderChangedEvent event, final ILabelProviderListener listener) {
237         Platform.run(new SafeRunnable() {
238             public void run() {
239                 listener.labelProviderChanged(event);
240             }
241         });
242
243     }
244     
245     /**
246      * Inform all of the listeners that require an update
247      * @param event the event with the update details
248      */

249     void fireListeners(final LabelProviderChangedEvent event) {
250         Object JavaDoc[] array = listeners.getListeners();
251         for (int i = 0; i < array.length; i++) {
252             final ILabelProviderListener l = (ILabelProviderListener) array[i];
253             Platform.run(new SafeRunnable() {
254                 public void run() {
255                     l.labelProviderChanged(event);
256                 }
257             });
258         }
259     }
260
261     /**
262      * Fire any listeners from the UIThread. Used for cases where this
263      * may be invoked outside of the UI by the public API.
264      * @param event the event with the update details
265      */

266     void fireListenersInUIThread(final LabelProviderChangedEvent event) {
267
268         //No updates if there is no UI
269
if (!PlatformUI.isWorkbenchRunning()) {
270             return;
271         }
272
273         //Only bother with the job if in the UI Thread
274
if (Thread.currentThread() == PlatformUI.getWorkbench().getDisplay()
275                 .getThread()) {
276             fireListeners(event);
277             return;
278         }
279
280         WorkbenchJob updateJob = new WorkbenchJob(WorkbenchMessages.DecorationScheduler_UpdateJobName) {
281             /*
282              * (non-Javadoc)
283              *
284              * @see org.eclipse.ui.progress.UIJob#runInUIThread(org.eclipse.core.runtime.IProgressMonitor)
285              */

286             public IStatus runInUIThread(IProgressMonitor monitor) {
287                 fireListeners(event);
288                 return Status.OK_STATUS;
289             }
290             
291             /* (non-Javadoc)
292              * @see org.eclipse.core.runtime.jobs.Job#belongsTo(java.lang.Object)
293              */

294             public boolean belongsTo(Object JavaDoc family) {
295                 return FAMILY_DECORATE == family;
296             }
297         };
298         updateJob.setSystem(true);
299         updateJob.schedule();
300
301     }
302
303     /* (non-Javadoc)
304      * @see org.eclipse.jface.viewers.ILabelDecorator2#decorateText(java.lang.String, java.lang.Object, org.eclipse.jface.viewers.IDecorationContext)
305      */

306     public String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element, IDecorationContext context) {
307         //Get any adaptations to IResource
308
Object JavaDoc adapted = getResourceAdapter(element);
309         String JavaDoc result = scheduler.decorateWithText(text, element, adapted, context);
310         FullDecoratorDefinition[] decorators = getDecoratorsFor(element);
311         for (int i = 0; i < decorators.length; i++) {
312             if (decorators[i].isEnabledFor(element)) {
313                 String JavaDoc newResult = safeDecorateText(element, result,
314                         decorators[i]);
315                 if (newResult != null) {
316                     result = newResult;
317                 }
318             }
319         }
320
321         if (adapted != null) {
322             decorators = getDecoratorsFor(adapted);
323             for (int i = 0; i < decorators.length; i++) {
324                 if (decorators[i].isAdaptable()
325                         && decorators[i].isEnabledFor(adapted)) {
326                     String JavaDoc newResult = safeDecorateText(adapted, result,
327                             decorators[i]);
328                     if (newResult != null) {
329                         result = newResult;
330                     }
331                 }
332             }
333         }
334
335         return result;
336     }
337     
338     /*
339      * (non-Javadoc)
340      * @see org.eclipse.jface.viewers.ILabelDecorator#decorateText(java.lang.String, java.lang.Object)
341      */

342     public String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element) {
343         return decorateText(text, element, DecorationContext.DEFAULT_CONTEXT);
344     }
345
346     /**
347      * Decorate the text in a SafeRunnable.
348      * @param element The element we are decorating
349      * @param start The currently decorated String
350      * @param decorator The decorator to run.
351      * @param context the decoration context
352      * @return String
353      */

354     private String JavaDoc safeDecorateText(Object JavaDoc element, String JavaDoc start,
355             FullDecoratorDefinition decorator) {
356         fullTextRunnable.setValues(start, element, decorator);
357         Platform.run(fullTextRunnable);
358         String JavaDoc newResult = fullTextRunnable.getResult();
359         return newResult;
360     }
361
362     /* (non-Javadoc)
363      * @see org.eclipse.jface.viewers.ILabelDecorator2#decorateImage(org.eclipse.swt.graphics.Image, java.lang.Object, org.eclipse.jface.viewers.IDecorationContext)
364      */

365     public Image decorateImage(Image image, Object JavaDoc element, IDecorationContext context) {
366         Object JavaDoc adapted = getResourceAdapter(element);
367         Image result = scheduler.decorateWithOverlays(image, element, adapted, context);
368         FullDecoratorDefinition[] decorators = getDecoratorsFor(element);
369
370         for (int i = 0; i < decorators.length; i++) {
371             if (decorators[i].isEnabledFor(element)) {
372                 Image newResult = safeDecorateImage(element, result,
373                         decorators[i]);
374                 if (newResult != null) {
375                     result = newResult;
376                 }
377             }
378         }
379
380         //Get any adaptations to IResource
381

382         if (adapted != null) {
383             decorators = getDecoratorsFor(adapted);
384             for (int i = 0; i < decorators.length; i++) {
385                 if (decorators[i].isAdaptable()
386                         && decorators[i].isEnabledFor(adapted)) {
387                     Image newResult = safeDecorateImage(adapted, result,
388                             decorators[i]);
389                     if (newResult != null) {
390                         result = newResult;
391                     }
392                 }
393             }
394         }
395
396         return result;
397     }
398
399     /*
400      * (non-Javadoc)
401      * @see org.eclipse.jface.viewers.ILabelDecorator#decorateImage(org.eclipse.swt.graphics.Image, java.lang.Object)
402      */

403     public Image decorateImage(Image image, Object JavaDoc element) {
404         return decorateImage(image, element, DecorationContext.DEFAULT_CONTEXT);
405     }
406
407     /**
408      * Decorate the image in a SafeRunnable.
409      * @param element The element we are decorating
410      * @param start The currently decorated Image
411      * @param decorator The decorator to run.
412      * @param context The decoration context
413      * @return Image
414      */

415     private Image safeDecorateImage(Object JavaDoc element, Image start,
416             FullDecoratorDefinition decorator) {
417         fullImageRunnable.setValues(start, element, decorator);
418         Platform.run(fullImageRunnable);
419         Image newResult = fullImageRunnable.getResult();
420         return newResult;
421     }
422
423     /**
424      * Get the resource adapted object for the supplied
425      * element. Return <code>null</code>. if there isn't one.
426      * @param element
427      * @return Object or <code>null</code>.
428      */

429     private Object JavaDoc getResourceAdapter(Object JavaDoc element) {
430         Object JavaDoc adapted = LegacyResourceSupport.getAdaptedContributorResource(element);
431         if (adapted != element) {
432             return adapted; //Avoid applying decorator twice
433
}
434         return null;
435     }
436
437     /**
438      * Return whether or not the decorator registered for element
439      * has a label property called property name.
440      */

441     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
442         return isLabelProperty(element, property, true);
443     }
444
445     /**
446      * Return whether or not the decorator registered for element
447      * has a label property called property name.
448      * Check for an adapted resource if checkAdapted is true.
449      * @param element
450      * @param property
451      * @param checkAdapted
452      * @return boolean <code>true</code> if there is a label property
453      * for element or its adapted value
454      */

455     public boolean isLabelProperty(Object JavaDoc element, String JavaDoc property,
456             boolean checkAdapted) {
457         boolean fullCheck = isLabelProperty(element, property,
458                 getDecoratorsFor(element));
459
460         if (fullCheck) {
461             return fullCheck;
462         }
463
464         boolean lightweightCheck = isLabelProperty(element, property,
465                 getLightweightManager().getDecoratorsFor(element));
466
467         if (lightweightCheck) {
468             return true;
469         }
470
471         if (checkAdapted) {
472             //Get any adaptions to IResource
473
Object JavaDoc adapted = getResourceAdapter(element);
474             if (adapted == null || adapted == element) {
475                 return false;
476             }
477
478             fullCheck = isLabelProperty(adapted, property,
479                     getDecoratorsFor(adapted));
480             if (fullCheck) {
481                 return fullCheck;
482             }
483
484             return isLabelProperty(adapted, property, lightweightManager
485                     .getDecoratorsFor(adapted));
486         }
487         return false;
488     }
489
490     private boolean isLabelProperty(Object JavaDoc element, String JavaDoc property,
491             DecoratorDefinition[] decorators) {
492         for (int i = 0; i < decorators.length; i++) {
493             if (decorators[i].isEnabledFor(element)
494                     && decorators[i].isLabelProperty(element, property)) {
495                 return true;
496             }
497         }
498
499         return false;
500     }
501
502     /**
503      * Return the enabled full decorator definitions.
504      * @return FullDecoratorDefinition[]
505      */

506     private FullDecoratorDefinition[] enabledFullDefinitions() {
507       
508         FullDecoratorDefinition[] full = getFullDefinitions();
509         //As this are a deprecated data type optimize for
510
//the undefined case.
511
if(full.length == 0) {
512             return full;
513         }
514         ArrayList JavaDoc result = new ArrayList JavaDoc();
515         for (int i = 0; i < full.length; i++) {
516             if (full[i].isEnabled()) {
517                 result.add(full[i]);
518             }
519         }
520         FullDecoratorDefinition[] returnArray = new FullDecoratorDefinition[result
521                 .size()];
522         result.toArray(returnArray);
523         return returnArray;
524     }
525
526     /*
527      * @see IBaseLabelProvider#dispose()
528      */

529     public void dispose() {
530        //do nothing
531
}
532
533     /**
534      * Clear the caches in the manager. This is required
535      * to avoid updates that may occur due to changes in
536      * enablement.
537      */

538     public void clearCaches() {
539         getLightweightManager().reset();
540         fullTextRunnable.clearReferences();
541         fullImageRunnable.clearReferences();
542     }
543
544     /**
545      * Enablement had changed. Fire the listeners and write
546      * the preference.
547      */

548     public void updateForEnablementChange() {
549         //Clear any results that may be around as all labels have changed
550
scheduler.clearResults();
551         fireListenersInUIThread(new LabelProviderChangedEvent(this));
552         writeDecoratorsPreference();
553     }
554
555     /**
556      * Get the DecoratorDefinitions defined on the receiver.
557      * @return DecoratorDefinition[]
558      */

559     public DecoratorDefinition[] getAllDecoratorDefinitions() {
560         LightweightDecoratorDefinition[] lightweightDefinitions = getLightweightManager()
561                 .getDefinitions();
562         DecoratorDefinition[] returnValue = new DecoratorDefinition[fullDefinitions.length
563                 + lightweightDefinitions.length];
564         System.arraycopy(fullDefinitions, 0, returnValue, 0,
565                 fullDefinitions.length);
566         System.arraycopy(lightweightDefinitions, 0, returnValue,
567                 fullDefinitions.length, lightweightDefinitions.length);
568         return returnValue;
569     }
570
571     /*
572      * @see ILabelProviderListener#labelProviderChanged(LabelProviderChangedEvent)
573      */

574     public void labelProviderChanged(LabelProviderChangedEvent event) {
575         Object JavaDoc[] elements = event.getElements();
576         scheduler.clearResults();
577         //If the elements are not specified send out a general update
578
if (elements == null) {
579             fireListeners(event);
580         } else {
581             //Assume that someone is going to care about the
582
//decoration result and just start it right away
583
for (int i = 0; i < elements.length; i++) {
584                 Object JavaDoc adapted = getResourceAdapter(elements[i]);
585                 //Force an update in case full decorators are the only ones enabled
586
scheduler.queueForDecoration(elements[i], adapted, true, null, DecorationContext.DEFAULT_CONTEXT);
587             }
588         }
589     }
590
591     /**
592      * Store the currently enabled decorators in
593      * preference store.
594      */

595     private void writeDecoratorsPreference() {
596         StringBuffer JavaDoc enabledIds = new StringBuffer JavaDoc();
597         writeDecoratorsPreference(enabledIds, getFullDefinitions());
598         writeDecoratorsPreference(enabledIds, getLightweightManager()
599                 .getDefinitions());
600
601         WorkbenchPlugin.getDefault().getPreferenceStore().setValue(
602                 IPreferenceConstants.ENABLED_DECORATORS, enabledIds.toString());
603         PrefUtil.savePrefs();
604     }
605
606     private void writeDecoratorsPreference(StringBuffer JavaDoc enabledIds,
607             DecoratorDefinition[] definitions) {
608         for (int i = 0; i < definitions.length; i++) {
609             enabledIds.append(definitions[i].getId());
610             enabledIds.append(VALUE_SEPARATOR);
611             if (definitions[i].isEnabled()) {
612                 enabledIds.append(P_TRUE);
613             } else {
614                 enabledIds.append(P_FALSE);
615             }
616
617             enabledIds.append(PREFERENCE_SEPARATOR);
618         }
619     }
620
621     /**
622      * Get the currently enabled decorators in
623      * preference store and set the state of the
624      * current definitions accordingly.
625      */

626     public void applyDecoratorsPreference() {
627
628         String JavaDoc preferenceValue = WorkbenchPlugin.getDefault()
629                 .getPreferenceStore().getString(
630                         IPreferenceConstants.ENABLED_DECORATORS);
631
632         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(preferenceValue,
633                 PREFERENCE_SEPARATOR);
634         Set JavaDoc enabledIds = new HashSet JavaDoc();
635         Set JavaDoc disabledIds = new HashSet JavaDoc();
636         while (tokenizer.hasMoreTokens()) {
637             String JavaDoc nextValuePair = tokenizer.nextToken();
638
639             //Strip out the true or false to get the id
640
String JavaDoc id = nextValuePair.substring(0, nextValuePair
641                     .indexOf(VALUE_SEPARATOR));
642             if (nextValuePair.endsWith(P_TRUE)) {
643                 enabledIds.add(id);
644             } else {
645                 disabledIds.add(id);
646             }
647         }
648
649         FullDecoratorDefinition[] full = getFullDefinitions();
650         for (int i = 0; i < full.length; i++) {
651             String JavaDoc id = full[i].getId();
652             if (enabledIds.contains(id)) {
653                 full[i].setEnabled(true);
654             } else {
655                 if (disabledIds.contains(id)) {
656                     full[i].setEnabled(false);
657                 }
658             }
659         }
660
661         LightweightDecoratorDefinition[] lightweightDefinitions = getLightweightManager()
662                 .getDefinitions();
663         for (int i = 0; i < lightweightDefinitions.length; i++) {
664             String JavaDoc id = lightweightDefinitions[i].getId();
665             if (enabledIds.contains(id)) {
666                 lightweightDefinitions[i].setEnabled(true);
667             } else {
668                 if (disabledIds.contains(id)) {
669                     lightweightDefinitions[i].setEnabled(false);
670                 }
671             }
672         }
673
674     }
675
676     /**
677      * Shutdown the decorator manager by disabling all
678      * of the decorators so that dispose() will be called
679      * on them.
680      */

681     public void shutdown() {
682         //Disable all of the enabled decorators
683
//so as to force a dispose of thier decorators
684
FullDecoratorDefinition[] full = getFullDefinitions();
685         for (int i = 0; i < full.length; i++) {
686             if (full[i].isEnabled()) {
687                 full[i].setEnabled(false);
688             }
689         }
690         if(lightweightManager != null) {
691             getLightweightManager().shutdown();
692         }
693         scheduler.shutdown();
694         dispose();
695     }
696
697     
698     /* (non-Javadoc)
699      * @see org.eclipse.ui.IDecoratorManager#getEnabled(java.lang.String)
700      */

701     public boolean getEnabled(String JavaDoc decoratorId) {
702         DecoratorDefinition definition = getDecoratorDefinition(decoratorId);
703         if (definition == null) {
704             return false;
705         }
706         return definition.isEnabled();
707     }
708
709     /**
710      * @see IDecoratorManager#getLabelDecorator()
711      */

712     public ILabelDecorator getLabelDecorator() {
713         return this;
714     }
715
716     /**
717      * @see IDecoratorManager#setEnabled(String, boolean)
718      */

719     public void setEnabled(String JavaDoc decoratorId, boolean enabled) {
720         DecoratorDefinition definition = getDecoratorDefinition(decoratorId);
721         if (definition != null) {
722             definition.setEnabled(enabled);
723             clearCaches();
724             updateForEnablementChange();
725         }
726     }
727
728     /*
729      * @see IDecoratorManager#getBaseLabelProvider(String)
730      */

731     public IBaseLabelProvider getBaseLabelProvider(String JavaDoc decoratorId) {
732         IBaseLabelProvider fullProvider = getLabelDecorator(decoratorId);
733         if (fullProvider == null) {
734             return getLightweightLabelDecorator(decoratorId);
735         }
736         return fullProvider;
737     }
738
739     /*
740      * @see IDecoratorManager#getLabelDecorator(String)
741      */

742     public ILabelDecorator getLabelDecorator(String JavaDoc decoratorId) {
743         FullDecoratorDefinition definition = getFullDecoratorDefinition(decoratorId);
744
745         //Do not return for a disabled decorator
746
if (definition != null && definition.isEnabled()) {
747             return definition.getDecorator();
748         }
749         return null;
750     }
751
752     /*
753      * @see IDecoratorManager#getLightweightLabelDecorator(String)
754      */

755     public ILightweightLabelDecorator getLightweightLabelDecorator(
756             String JavaDoc decoratorId) {
757         LightweightDecoratorDefinition definition = getLightweightManager()
758                 .getDecoratorDefinition(decoratorId);
759         //Do not return for a disabled decorator
760
if (definition != null && definition.isEnabled()) {
761             return definition.getDecorator();
762         }
763         return null;
764     }
765
766     /**
767      * Get the DecoratorDefinition with the supplied id
768      * @return DecoratorDefinition or <code>null</code> if it is not found
769      * @param decoratorId String
770      */

771     private DecoratorDefinition getDecoratorDefinition(String JavaDoc decoratorId) {
772         DecoratorDefinition returnValue = getFullDecoratorDefinition(decoratorId);
773         if (returnValue == null) {
774             return getLightweightManager().getDecoratorDefinition(decoratorId);
775         }
776         return returnValue;
777     }
778
779     /**
780      * Get the FullDecoratorDefinition with the supplied id
781      * @return FullDecoratorDefinition or <code>null</code> if it is not found
782      * @param decoratorId the id
783      */

784     private FullDecoratorDefinition getFullDecoratorDefinition(
785             String JavaDoc decoratorId) {
786         int idx = getFullDecoratorDefinitionIdx(decoratorId);
787         if (idx != -1) {
788             return getFullDefinitions()[idx];
789         }
790         return null;
791     }
792     
793     /**
794      * Return the index of the definition in the array.
795      *
796      * @param decoratorId the id
797      * @return the index of the definition in the array or <code>-1</code>
798      * @since 3.1
799      */

800     private int getFullDecoratorDefinitionIdx(
801             String JavaDoc decoratorId) {
802         FullDecoratorDefinition[] full = getFullDefinitions();
803         for (int i = 0; i < full.length; i++) {
804             if (full[i].getId().equals(decoratorId)) {
805                 return i;
806             }
807         }
808         return -1;
809     }
810             
811
812     /**
813      * Get the full decorator definitions registered for elements of this type.
814      * @param element The element to look up
815      * @return FullDecoratorDefinition[]
816      */

817     private FullDecoratorDefinition[] getDecoratorsFor(Object JavaDoc element) {
818
819         if (element == null) {
820             return EMPTY_FULL_DEF;
821         }
822
823           Collection JavaDoc decorators = getDecoratorsFor(element,
824                 enabledFullDefinitions());
825         FullDecoratorDefinition[] decoratorArray = EMPTY_FULL_DEF;
826         if (decorators.size() > 0){
827             decoratorArray = new FullDecoratorDefinition[decorators.size()];
828             decorators.toArray(decoratorArray);
829         }
830
831         return decoratorArray;
832     }
833
834     /**
835      * Returns the lightweightManager. This method is
836      * public for use by test cases. No other classes outside of
837      * this package should use this method.
838      * @return LightweightDecoratorManager
839      */

840     public LightweightDecoratorManager getLightweightManager() {
841         if(lightweightManager == null) {
842             initializeDecoratorDefinitions();
843         }
844         return lightweightManager;
845     }
846
847     /**
848      * @see org.eclipse.ui.IDecoratorManager#update(java.lang.String)
849      */

850     public void update(String JavaDoc decoratorId) {
851
852         IBaseLabelProvider provider = getBaseLabelProvider(decoratorId);
853         if (provider != null) {
854             scheduler.clearResults();
855             fireListeners(new LabelProviderChangedEvent(provider));
856         }
857
858     }
859     
860     public boolean prepareDecoration(Object JavaDoc element, String JavaDoc originalText, IDecorationContext context) {
861         // Check if there is a decoration ready or if there is no lightweight decorators to be applied
862
if (scheduler.isDecorationReady(element, context)
863                 || !getLightweightManager().hasEnabledDefinitions()) {
864             return true;
865         }
866
867         // Force an update if there is a text already
868
boolean force = true;
869         //If not then do not force as the undecorated value is fine
870
if(originalText == null || originalText.length() == 0) {
871             force = false;
872         }
873         
874         // Queue the decoration.
875
scheduler.queueForDecoration(element, getResourceAdapter(element),
876                 force, originalText, context);
877
878         //If all that is there is deferred ones then defer decoration.
879
//For the sake of efficiency we do not test for enablement at this
880
//point and just abandon deferment if there are any to run right
881
//away
882
return getFullDefinitions().length > 0;
883     }
884
885     /* (non-Javadoc)
886      * @see org.eclipse.jface.viewers.IDelayedLabelDecorator#prepareDecoration(java.lang.Object, java.lang.String)
887      */

888     public boolean prepareDecoration(Object JavaDoc element, String JavaDoc originalText) {
889         return prepareDecoration(element, originalText, DecorationContext.DEFAULT_CONTEXT);
890     }
891     
892     /* (non-Javadoc)
893      * @see org.eclipse.jface.viewers.IFontDecorator#decorateFont(java.lang.Object)
894      */

895     public Font decorateFont(Object JavaDoc element) {
896         return scheduler.getFont(element, getResourceAdapter(element));
897     }
898     /* (non-Javadoc)
899      * @see org.eclipse.jface.viewers.IColorDecorator#decorateBackground(java.lang.Object)
900      */

901     public Color decorateBackground(Object JavaDoc element) {
902         return scheduler.getBackgroundColor(element, getResourceAdapter(element));
903     }
904     /* (non-Javadoc)
905      * @see org.eclipse.jface.viewers.IColorDecorator#decorateForeground(java.lang.Object)
906      */

907     public Color decorateForeground(Object JavaDoc element) {
908         return scheduler.getForegroundColor(element, getResourceAdapter(element));
909     }
910     /**
911      * Get all of the defined fullDefinitions. Initalize if
912      * required
913      * @return FullDecoratorDefinition[]
914      */

915     private FullDecoratorDefinition[] getFullDefinitions() {
916         if(fullDefinitions == null) {
917             initializeDecoratorDefinitions();
918         }
919         return fullDefinitions;
920     }
921
922     private IExtensionPoint getExtensionPointFilter() {
923         return Platform.getExtensionRegistry().getExtensionPoint(EXTENSIONPOINT_UNIQUE_ID);
924     }
925
926     /* (non-Javadoc)
927      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamicHelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
928      */

929     public void addExtension(IExtensionTracker tracker, IExtension addedExtension) {
930         IConfigurationElement addedElements[] = addedExtension.getConfigurationElements();
931         for (int i = 0; i < addedElements.length; i++) {
932             DecoratorRegistryReader reader = new DecoratorRegistryReader();
933             reader.readElement(addedElements[i]);
934             for (Iterator JavaDoc j = reader.getValues().iterator(); j.hasNext();) {
935                 addDecorator((DecoratorDefinition) j.next());
936             }
937         }
938     }
939
940     /* (non-Javadoc)
941      * @see org.eclipse.core.runtime.dynamicHelpers.IExtensionChangeHandler#removeExtension(org.eclipse.core.runtime.IExtension, java.lang.Object[])
942      */

943     public void removeExtension(IExtension source, Object JavaDoc[] objects) {
944         
945         boolean shouldClear = false;
946         for (int i = 0; i < objects.length; i++) {
947             if (objects[i] instanceof DecoratorDefinition) {
948                 DecoratorDefinition definition = (DecoratorDefinition) objects[i];
949                 if (definition.isFull()) {
950                     int idx = getFullDecoratorDefinitionIdx(definition.getId());
951                     if (idx != -1) {
952                         FullDecoratorDefinition[] oldDefs = getFullDefinitions();
953                         Util
954                                 .arrayCopyWithRemoval(
955                                         oldDefs,
956                                         fullDefinitions = new FullDecoratorDefinition[fullDefinitions.length - 1],
957                                         idx);
958                         shouldClear = true;
959                     }
960                 } else {
961                     shouldClear |= getLightweightManager().removeDecorator(
962                             (LightweightDecoratorDefinition) definition);
963                 }
964             }
965         }
966         
967         if(shouldClear){
968               clearCaches();
969               updateForEnablementChange();
970         }
971         
972     }
973
974 }
975
Popular Tags