001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.decorators;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.core.runtime.IConfigurationElement;
014: import org.eclipse.core.runtime.ISafeRunnable;
015: import org.eclipse.core.runtime.Platform;
016: import org.eclipse.jface.viewers.IBaseLabelProvider;
017: import org.eclipse.jface.viewers.IDecoration;
018: import org.eclipse.jface.viewers.ILightweightLabelDecorator;
019: import org.eclipse.ui.internal.ActionExpression;
020: import org.eclipse.ui.internal.IObjectContributor;
021: import org.eclipse.ui.internal.LegacyResourceSupport;
022: import org.eclipse.ui.internal.WorkbenchPlugin;
023:
024: /**
025: * The DeclarativeDecoratorDefinition is a decorator definition that is defined
026: * entirely from xml and will not require the activation of its defining
027: * plug-in.
028: */
029: class LightweightDecoratorDefinition extends DecoratorDefinition
030: implements IObjectContributor {
031:
032: private static final String ATT_LOCATION = "location"; //$NON-NLS-1$
033:
034: static final String ATT_ICON = "icon"; //$NON-NLS-1$
035:
036: // Constants for quadrants
037: /**
038: * Position <code>TOP_LEFT</code>. Value <code>0</code>
039: */
040: public static final int TOP_LEFT = 0;
041:
042: /**
043: * Position <code>TOP_RIGHT</code>. Value <code>1</code>
044: */
045: public static final int TOP_RIGHT = 1;
046:
047: /**
048: * Position <code>BOTTOM_LEFT</code>. Value <code>2</code>
049: */
050: public static final int BOTTOM_LEFT = 2;
051:
052: /**
053: * Position <code>BOTTOM_RIGHT</code>. Value <code>3</code>
054: */
055: public static final int BOTTOM_RIGHT = 3;
056:
057: /**
058: * Position <code>UNDERLAY</code>. Value <code>4</code>
059: */
060: public static final int UNDERLAY = 4;
061:
062: private static final String UNDERLAY_STRING = "UNDERLAY"; //$NON-NLS-1$
063:
064: private static final String ATT_QUADRANT = "quadrant"; //$NON-NLS-1$
065:
066: // Constants for quadrants
067: private static final String TOP_LEFT_STRING = "TOP_LEFT"; //$NON-NLS-1$
068:
069: private static final String TOP_RIGHT_STRING = "TOP_RIGHT"; //$NON-NLS-1$
070:
071: private static final String BOTTOM_LEFT_STRING = "BOTTOM_LEFT"; //$NON-NLS-1$
072:
073: /**
074: * The DeclarativeDecorator is the internal decorator supplied by the
075: * decorator definition.
076: */
077: private ILightweightLabelDecorator decorator;
078:
079: private int quadrant;
080:
081: private boolean hasReadQuadrant;
082:
083: private String[] objectClasses;
084:
085: LightweightDecoratorDefinition(String identifier,
086: IConfigurationElement element) {
087: super (identifier, element);
088: }
089:
090: /**
091: * Gets the decorator and creates it if it does not exist yet. Throws a
092: * CoreException if there is a problem creating the decorator. This method
093: * should not be called unless a check for enabled to be true is done first.
094: *
095: * @return Returns a ILabelDecorator
096: */
097: protected ILightweightLabelDecorator internalGetDecorator()
098: throws CoreException {
099: if (labelProviderCreationFailed) {
100: return null;
101: }
102:
103: final CoreException[] exceptions = new CoreException[1];
104:
105: if (decorator == null) {
106:
107: if (isDeclarative()) {
108: decorator = new DeclarativeDecorator(definingElement,
109: getIconLocation());
110: } else {
111:
112: Platform.run(new ISafeRunnable() {
113: public void run() {
114: try {
115: decorator = (ILightweightLabelDecorator) WorkbenchPlugin
116: .createExtension(
117: definingElement,
118: DecoratorDefinition.ATT_CLASS);
119: decorator
120: .addListener(WorkbenchPlugin
121: .getDefault()
122: .getDecoratorManager());
123: } catch (CoreException exception) {
124: exceptions[0] = exception;
125: }
126: }
127:
128: /*
129: * (non-Javadoc) Method declared on ISafeRunnable.
130: */
131: public void handleException(Throwable e) {
132: // Do nothing as Core will handle the logging
133: }
134: });
135: }
136: } else {
137: return decorator;
138: }
139:
140: if (decorator == null) {
141: this .labelProviderCreationFailed = true;
142: setEnabled(false);
143: }
144:
145: if (exceptions[0] != null) {
146: throw exceptions[0];
147: }
148:
149: return decorator;
150: }
151:
152: /**
153: * Return whether or not this represents a declarative decorator.
154: *
155: * @return boolean <code>true</code> if this is declarative
156: */
157: private boolean isDeclarative() {
158: return definingElement
159: .getAttribute(DecoratorDefinition.ATT_CLASS) == null;
160: }
161:
162: /**
163: * Return the icon location.
164: *
165: * @return the icon location
166: */
167: private String getIconLocation() {
168: return definingElement.getAttribute(ATT_ICON);
169: }
170:
171: /*
172: * (non-Javadoc)
173: *
174: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#internalGetLabelProvider()
175: */
176: protected IBaseLabelProvider internalGetLabelProvider()
177: throws CoreException {
178: return internalGetDecorator();
179: }
180:
181: /*
182: * (non-Javadoc)
183: *
184: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#isFull()
185: */
186: public boolean isFull() {
187: return false;
188: }
189:
190: /**
191: * Returns the quadrant.One of the following constants in
192: * DecoratorRegistryReader: TOP_LEFT TOP_RIGHT BOTTOM_LEFT BOTTOM_RIGHT
193: * UNDERLAY
194: *
195: * @return int
196: */
197: public int getQuadrant() {
198: if (!hasReadQuadrant) {
199: hasReadQuadrant = true;
200: quadrant = getLocationConstant(definingElement
201: .getAttribute(ATT_LOCATION), definingElement);
202: }
203: return quadrant;
204: }
205:
206: /**
207: * Get the constant value based on the location supplied. Default to bottom
208: * right.
209: *
210: * @since 3.1
211: */
212: private int getLocationConstant(String locationDefinition,
213: IConfigurationElement element) {
214:
215: // Backwards compatibility
216: if (locationDefinition == null) {
217: locationDefinition = element.getAttribute(ATT_QUADRANT);
218: }
219:
220: if (TOP_RIGHT_STRING.equals(locationDefinition)) {
221: return TOP_RIGHT;
222: }
223: if (TOP_LEFT_STRING.equals(locationDefinition)) {
224: return TOP_LEFT;
225: }
226: if (BOTTOM_LEFT_STRING.equals(locationDefinition)) {
227: return BOTTOM_LEFT;
228: }
229: if (UNDERLAY_STRING.equals(locationDefinition)) {
230: return UNDERLAY;
231: }
232: return BOTTOM_RIGHT;
233:
234: }
235:
236: /**
237: * Decorate the element using the decoration to store the result.
238: * @param element
239: * @param decoration
240: */
241: public void decorate(Object element, IDecoration decoration) {
242: try {
243: // Internal decorator might be null so be prepared
244: ILightweightLabelDecorator currentDecorator = internalGetDecorator();
245: if (currentDecorator == null) {
246: return;
247: }
248:
249: if (isAdaptable()) {
250: String[] classes = getObjectClasses();
251: for (int i = 0; i < classes.length; i++) {
252: String className = classes[i];
253: Object adapted = LegacyResourceSupport.getAdapter(
254: element, className);
255: if (adapted != null) {
256: currentDecorator.decorate(adapted, decoration);
257: }
258: }
259: } else {
260: if (currentDecorator != null && element != null) {
261: currentDecorator.decorate(element, decoration);
262: }
263: }
264: } catch (CoreException exception) {
265: handleCoreException(exception);
266: }
267:
268: }
269:
270: /**
271: * Returns the lightweight decorator, or <code>null</code> if not enabled.
272: *
273: * @return the lightweight decorator, or <code>null</code> if not enabled
274: */
275: public ILightweightLabelDecorator getDecorator() {
276: return decorator;
277: }
278:
279: /*
280: * (non-Javadoc)
281: *
282: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#refreshDecorator()
283: */
284: protected void refreshDecorator() {
285: // Only do something if disabled so as to prevent
286: // gratutitous activation
287: if (!this .enabled && decorator != null) {
288: IBaseLabelProvider cached = decorator;
289: decorator = null;
290: disposeCachedDecorator(cached);
291: }
292: }
293:
294: /*
295: * (non-Javadoc)
296: *
297: * @see org.eclipse.ui.internal.IObjectContributor#isApplicableTo(java.lang.Object)
298: */
299: public boolean isApplicableTo(Object object) {
300: return isEnabledFor(object);
301: }
302:
303: /*
304: * (non-Javadoc)
305: *
306: * @see org.eclipse.ui.internal.IObjectContributor#canAdapt()
307: */
308: public boolean canAdapt() {
309: return isAdaptable();
310: }
311:
312: /**
313: * Get the object classes to which this decorator is registered.
314: *
315: * @return String [] the object classes to which this decorator is
316: * registered
317: */
318: public String[] getObjectClasses() {
319: if (objectClasses == null) {
320: getEnablement();
321: }
322: return objectClasses;
323: }
324:
325: /*
326: * (non-Javadoc)
327: *
328: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#initializeEnablement()
329: */
330: protected void initializeEnablement() {
331: super .initializeEnablement();
332: ActionExpression expression = getEnablement();
333: if (expression != null) {
334: objectClasses = expression.extractObjectClasses();
335: }
336:
337: // If the class is null set it to Object
338: if (objectClasses == null) {
339: objectClasses = new String[] { Object.class.getName() };
340: }
341: }
342:
343: }
|