KickJava   Java API By Example, From Geeks To Geeks.

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


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 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 /**
26  * The DeclarativeDecoratorDefinition is a decorator definition that is defined
27  * entirely from xml and will not require the activation of its defining
28  * plug-in.
29  */

30 class LightweightDecoratorDefinition extends DecoratorDefinition implements
31         IObjectContributor {
32
33     private static final String JavaDoc ATT_LOCATION = "location"; //$NON-NLS-1$
34

35     static final String JavaDoc ATT_ICON = "icon"; //$NON-NLS-1$
36

37     // Constants for quadrants
38
/**
39      * Position <code>TOP_LEFT</code>. Value <code>0</code>
40      */

41     public static final int TOP_LEFT = 0;
42
43     /**
44      * Position <code>TOP_RIGHT</code>. Value <code>1</code>
45      */

46     public static final int TOP_RIGHT = 1;
47
48     /**
49      * Position <code>BOTTOM_LEFT</code>. Value <code>2</code>
50      */

51     public static final int BOTTOM_LEFT = 2;
52
53     /**
54      * Position <code>BOTTOM_RIGHT</code>. Value <code>3</code>
55      */

56     public static final int BOTTOM_RIGHT = 3;
57
58     /**
59      * Position <code>UNDERLAY</code>. Value <code>4</code>
60      */

61     public static final int UNDERLAY = 4;
62
63     private static final String JavaDoc UNDERLAY_STRING = "UNDERLAY"; //$NON-NLS-1$
64

65     private static final String JavaDoc ATT_QUADRANT = "quadrant"; //$NON-NLS-1$
66

67     // Constants for quadrants
68
private static final String JavaDoc TOP_LEFT_STRING = "TOP_LEFT"; //$NON-NLS-1$
69

70     private static final String JavaDoc TOP_RIGHT_STRING = "TOP_RIGHT"; //$NON-NLS-1$
71

72     private static final String JavaDoc BOTTOM_LEFT_STRING = "BOTTOM_LEFT"; //$NON-NLS-1$
73

74     /**
75      * The DeclarativeDecorator is the internal decorator supplied by the
76      * decorator definition.
77      */

78     private ILightweightLabelDecorator decorator;
79
80     private int quadrant;
81
82     private boolean hasReadQuadrant;
83
84     private String JavaDoc[] objectClasses;
85
86     LightweightDecoratorDefinition(String JavaDoc identifier,
87             IConfigurationElement element) {
88         super(identifier, element);
89     }
90
91     /**
92      * Gets the decorator and creates it if it does not exist yet. Throws a
93      * CoreException if there is a problem creating the decorator. This method
94      * should not be called unless a check for enabled to be true is done first.
95      *
96      * @return Returns a ILabelDecorator
97      */

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                     /*
127                      * (non-Javadoc) Method declared on ISafeRunnable.
128                      */

129                     public void handleException(Throwable JavaDoc e) {
130                         // Do nothing as Core will handle the logging
131
}
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     /**
151      * Return whether or not this represents a declarative decorator.
152      *
153      * @return boolean <code>true</code> if this is declarative
154      */

155     private boolean isDeclarative() {
156         return definingElement.getAttribute(DecoratorDefinition.ATT_CLASS) == null;
157     }
158
159     /**
160      * Return the icon location.
161      *
162      * @return the icon location
163      */

164     private String JavaDoc getIconLocation() {
165         return definingElement.getAttribute(ATT_ICON);
166     }
167
168     /*
169      * (non-Javadoc)
170      *
171      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#internalGetLabelProvider()
172      */

173     protected IBaseLabelProvider internalGetLabelProvider()
174             throws CoreException {
175         return internalGetDecorator();
176     }
177
178     /*
179      * (non-Javadoc)
180      *
181      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#isFull()
182      */

183     public boolean isFull() {
184         return false;
185     }
186
187     /**
188      * Returns the quadrant.One of the following constants in
189      * DecoratorRegistryReader: TOP_LEFT TOP_RIGHT BOTTOM_LEFT BOTTOM_RIGHT
190      * UNDERLAY
191      *
192      * @return int
193      */

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     /**
204      * Get the constant value based on the location supplied. Default to bottom
205      * right.
206      *
207      * @since 3.1
208      */

209     private int getLocationConstant(String JavaDoc locationDefinition,
210             IConfigurationElement element) {
211
212         // Backwards compatibility
213
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     /**
234      * Decorate the element using the decoration to store the result.
235      * @param element
236      * @param decoration
237      */

238     public void decorate(Object JavaDoc element, IDecoration decoration) {
239         try {
240             // Internal decorator might be null so be prepared
241
ILightweightLabelDecorator currentDecorator = internalGetDecorator();
242             if(currentDecorator == null) {
243                 return;
244             }
245             
246             if (isAdaptable()) {
247                 String JavaDoc[] classes = getObjectClasses();
248                 for (int i = 0; i < classes.length; i++) {
249                     String JavaDoc className = classes[i];
250                     Object JavaDoc 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     /**
269      * Returns the lightweight decorator, or <code>null</code> if not enabled.
270      *
271      * @return the lightweight decorator, or <code>null</code> if not enabled
272      */

273     public ILightweightLabelDecorator getDecorator() {
274         return decorator;
275     }
276
277     /*
278      * (non-Javadoc)
279      *
280      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#refreshDecorator()
281      */

282     protected void refreshDecorator() {
283         // Only do something if disabled so as to prevent
284
// gratutitous activation
285
if (!this.enabled && decorator != null) {
286             IBaseLabelProvider cached = decorator;
287             decorator = null;
288             disposeCachedDecorator(cached);
289         }
290     }
291
292     /*
293      * (non-Javadoc)
294      *
295      * @see org.eclipse.ui.internal.IObjectContributor#isApplicableTo(java.lang.Object)
296      */

297     public boolean isApplicableTo(Object JavaDoc object) {
298         return isEnabledFor(object);
299     }
300
301     /*
302      * (non-Javadoc)
303      *
304      * @see org.eclipse.ui.internal.IObjectContributor#canAdapt()
305      */

306     public boolean canAdapt() {
307         return isAdaptable();
308     }
309
310     /**
311      * Get the object classes to which this decorator is registered.
312      *
313      * @return String [] the object classes to which this decorator is
314      * registered
315      */

316     public String JavaDoc[] getObjectClasses() {
317         if (objectClasses == null) {
318             getEnablement();
319         }
320         return objectClasses;
321     }
322
323     /*
324      * (non-Javadoc)
325      *
326      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#initializeEnablement()
327      */

328     protected void initializeEnablement() {
329         super.initializeEnablement();
330         ActionExpression expression = getEnablement();
331         if (expression != null) {
332             objectClasses = expression.extractObjectClasses();
333         }
334
335         // If the class is null set it to Object
336
if (objectClasses == null) {
337             objectClasses = new String JavaDoc[] {Object JavaDoc.class.getName()};
338         }
339     }
340
341 }
342
Popular Tags