KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.List JavaDoc;
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 /**
28  * The LightweightDecoratorManager is a decorator manager
29  * that encapsulates the behavior for the lightweight decorators.
30  */

31 public class LightweightDecoratorManager extends ObjectContributorManager {
32
33     /**
34      * The runnable is the object used to run the decorations
35      * so that an error in someones decorator will not kill the thread.
36      * It is implemented here to prevent aborting of decoration
37      * i.e. successful decorations will still be applied.
38      */

39
40     private class LightweightRunnable implements ISafeRunnable {
41         private Object JavaDoc element;
42
43         private DecorationBuilder decoration;
44
45         private LightweightDecoratorDefinition decorator;
46
47         void setValues(Object JavaDoc object, DecorationBuilder builder,
48                 LightweightDecoratorDefinition definition) {
49             element = object;
50             decoration = builder;
51             decorator = definition;
52
53         }
54
55         /*
56          * @see ISafeRunnable.handleException(Throwable).
57          */

58         public void handleException(Throwable JavaDoc exception) {
59             IStatus status = StatusUtil.newStatus(IStatus.ERROR, exception
60                     .getMessage(), exception);
61             WorkbenchPlugin.log("Exception in Decorator", status); //$NON-NLS-1$
62
if (decorator != null) {
63                 decorator.crashDisable();
64             }
65         }
66
67         /*
68          * @see ISafeRunnable.run
69          */

70         public void run() throws Exception JavaDoc {
71             decorator.decorate(element, decoration);
72         }
73
74         /**
75          * Clear decorator references.
76          * @since 3.1
77          */

78         void clearReferences() {
79             decorator = null;
80         }
81     }
82
83     private LightweightRunnable runnable = new LightweightRunnable();
84
85     //The lightweight definitionsread from the registry
86
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     /**
99      * Get the lightweight definitions for the receiver.
100      * @return LightweightDecoratorDefinition[]
101      */

102     LightweightDecoratorDefinition[] getDefinitions() {
103         return lightweightDefinitions;
104     }
105
106     /**
107      * Register the decorators as object contributions so
108      * that adaptable lookup can occur.
109      */

110     private void buildContributors() {
111         for (int i = 0; i < lightweightDefinitions.length; i++) {
112             LightweightDecoratorDefinition decorator = lightweightDefinitions[i];
113             String JavaDoc[] types = getTargetTypes(decorator);
114             for (int j = 0; j < types.length; j++) {
115                  registerContributor(decorator,types[j]);
116             }
117         }
118     }
119     
120     /**
121      * For dynamic UI
122      *
123      * @param decorator the definition to add
124      * @return whether the definition was added
125      * @since 3.0
126      */

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             // no reset - handled in the DecoratorManager
135
String JavaDoc[] 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     /**
145      * Get the name of the types that a decorator is registered for.
146      * @param decorator
147      * @return String[]
148      */

149     private String JavaDoc[] getTargetTypes(LightweightDecoratorDefinition decorator) {
150         return decorator.getObjectClasses();
151     }
152
153     /**
154      * For dynamic-ui
155      * @param decorator the definition to remove
156      * @return whether the definition was removed
157      * @since 3.1
158      */

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             // no reset - handled in the DecoratorManager
169
String JavaDoc [] 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     /**
180      * Get the LightweightDecoratorDefinition with the supplied id
181      * @return LightweightDecoratorDefinition or <code>null</code> if it is not found
182      * @param decoratorId String
183      * @since 3.0
184      */

185     private LightweightDecoratorDefinition getLightweightDecoratorDefinition(
186             String JavaDoc decoratorId) {
187         int idx = getLightweightDecoratorDefinitionIdx(decoratorId);
188         if (idx != -1) {
189             return lightweightDefinitions[idx];
190         }
191         return null;
192     }
193     
194     /**
195      * Return the index of the definition in the array.
196      *
197      * @param decoratorId the id
198      * @return the index of the definition in the array or <code>-1</code>
199      * @since 3.1
200      */

201     private int getLightweightDecoratorDefinitionIdx(
202             String JavaDoc 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     /**
212      * Return the enabled lightweight decorator definitions.
213      * @return LightweightDecoratorDefinition[]
214      */

215     LightweightDecoratorDefinition[] enabledDefinitions() {
216         ArrayList JavaDoc result = new ArrayList JavaDoc();
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     /**
229      * Return whether there are enabled lightwieght decorators
230      * @return boolean
231      */

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     /**
242      * Reset any cached values.
243      */

244     void reset() {
245         runnable.clearReferences();
246     }
247
248     /**
249      * Shutdown the decorator manager by disabling all
250      * of the decorators so that dispose() will be called
251      * on them.
252      */

253     void shutdown() {
254         //Disable all fo the enabled decorators
255
//so as to force a dispose of thier decorators
256
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     /**
265      * Get the LightweightDecoratorDefinition with the supplied id
266      * @return LightweightDecoratorDefinition or <code>null</code> if it is not found
267      * @param decoratorId String
268      */

269     LightweightDecoratorDefinition getDecoratorDefinition(String JavaDoc 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     /**
279      * Get the lightweight registered for elements of this type.
280      */

281     LightweightDecoratorDefinition[] getDecoratorsFor(Object JavaDoc element) {
282
283         if (element == null) {
284             return EMPTY_LIGHTWEIGHT_DEF;
285         }
286
287         List JavaDoc elements = new ArrayList JavaDoc(1);
288         elements.add(element);
289         LightweightDecoratorDefinition[] decoratorArray = EMPTY_LIGHTWEIGHT_DEF;
290         List JavaDoc contributors = getContributors(elements);
291         if (!contributors.isEmpty()) {
292             Collection JavaDoc 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     /**
305      * Fill the decoration with all of the results of the
306      * decorators.
307      *
308      * @param element The source element
309      * @param context The decoration context
310      * @param decoration The DecorationResult we are working on.
311      * where adaptable is true.
312      */

313     public void getDecorations(Object JavaDoc element, DecorationBuilder decoration) {
314
315         LightweightDecoratorDefinition[] decorators = getDecoratorsFor(element);
316
317         for (int i = 0; i < decorators.length; i++) {
318             //If we are doing the adaptable one make sure we are
319
//only applying the adaptable decorations
320
LightweightDecoratorDefinition dd = decorators[i];
321             decoration.setCurrentDefinition(dd);
322             decorate(element, decoration, dd);
323         }
324     }
325
326     /**
327      * Decorate the element receiver in a SafeRunnable.
328      * @param element The Object to be decorated
329      * @param decoration The object building decorations.
330      * @param decorator The decorator being applied.
331      */

332     private void decorate(Object JavaDoc element, DecorationBuilder decoration,
333             LightweightDecoratorDefinition decorator) {
334
335         runnable.setValues(element, decoration, decorator);
336         Platform.run(runnable);
337     }
338
339     /**
340      * Returns the overlayCache.
341      * @return OverlayCache
342      */

343     OverlayCache getOverlayCache() {
344         return overlayCache;
345     }
346
347     /**
348      * Method for use by test cases
349      * @param object the object to be decorated
350      * @return the decoration result
351      */

352     public DecorationResult getDecorationResult(Object JavaDoc object) {
353         DecorationBuilder builder = new DecorationBuilder();
354         getDecorations(object, builder);
355         return builder.createResult();
356         
357     }
358
359     /* (non-Javadoc)
360      * @see org.eclipse.core.runtime.dynamichelpers.IExtensionChangeHandler#addExtension(org.eclipse.core.runtime.dynamichelpers.IExtensionTracker, org.eclipse.core.runtime.IExtension)
361      */

362     public void addExtension(IExtensionTracker tracker, IExtension extension) {
363         // Do nothing as this is handled by the DecoratorManager
364
//This is not called as canHandleExtensionTracking returns
365
//false.
366
}
367     
368     /* (non-Javadoc)
369      * @see org.eclipse.ui.internal.ObjectContributorManager#canHandleExtensionTracking()
370      */

371     protected boolean canHandleExtensionTracking() {
372         return false;
373     }
374
375 }
376
Popular Tags