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.Platform;
015: import org.eclipse.jface.util.SafeRunnable;
016: import org.eclipse.jface.viewers.IBaseLabelProvider;
017: import org.eclipse.jface.viewers.ILabelDecorator;
018: import org.eclipse.osgi.util.NLS;
019: import org.eclipse.swt.graphics.Image;
020: import org.eclipse.ui.internal.WorkbenchMessages;
021: import org.eclipse.ui.internal.WorkbenchPlugin;
022:
023: /**
024: * The RunnableDecoratorDefinition is the definition for
025: * decorators that have an ILabelDecorator class to instantiate.
026: */
027:
028: class FullDecoratorDefinition extends DecoratorDefinition {
029:
030: ILabelDecorator decorator;
031:
032: /**
033: * Create a new instance of the receiver with the
034: * supplied values.
035: */
036:
037: FullDecoratorDefinition(String identifier,
038: IConfigurationElement element) {
039: super (identifier, element);
040: }
041:
042: /**
043: * Gets the decorator and creates it if it does
044: * not exist yet. Throws a CoreException if there is a problem
045: * creating the decorator.
046: * This method should not be called unless a check for
047: * enabled to be true is done first.
048: * @return Returns a ILabelDecorator
049: */
050: protected ILabelDecorator internalGetDecorator()
051: throws CoreException {
052: if (labelProviderCreationFailed) {
053: return null;
054: }
055:
056: final CoreException[] exceptions = new CoreException[1];
057:
058: if (decorator == null) {
059: Platform
060: .run(new SafeRunnable(
061: NLS
062: .bind(
063: WorkbenchMessages.DecoratorManager_ErrorActivatingDecorator,
064: getName())) {
065: public void run() {
066: try {
067: decorator = (ILabelDecorator) WorkbenchPlugin
068: .createExtension(
069: definingElement,
070: DecoratorDefinition.ATT_CLASS);
071: decorator.addListener(WorkbenchPlugin
072: .getDefault()
073: .getDecoratorManager());
074: } catch (CoreException exception) {
075: exceptions[0] = exception;
076: }
077: }
078: });
079: } else {
080: return decorator;
081: }
082:
083: if (decorator == null) {
084: this .labelProviderCreationFailed = true;
085: setEnabled(false);
086: }
087:
088: if (exceptions[0] != null) {
089: throw exceptions[0];
090: }
091:
092: return decorator;
093: }
094:
095: /* (non-Javadoc)
096: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#refreshDecorator()
097: */
098: protected void refreshDecorator() {
099: //Only do something if disabled so as to prevent
100: //gratutitous activation
101: if (!this .enabled && decorator != null) {
102: IBaseLabelProvider cached = decorator;
103: decorator = null;
104: disposeCachedDecorator(cached);
105: }
106: }
107:
108: /**
109: * Decorate the image provided for the element type.
110: * This method should not be called unless a check for
111: * isEnabled() has been done first.
112: * Return null if there is no image or if an error occurs.
113: */
114: Image decorateImage(Image image, Object element) {
115: try {
116: //Internal decorator might be null so be prepared
117: ILabelDecorator currentDecorator = internalGetDecorator();
118: if (currentDecorator != null) {
119: return currentDecorator.decorateImage(image, element);
120: }
121:
122: } catch (CoreException exception) {
123: handleCoreException(exception);
124: }
125: return null;
126: }
127:
128: /**
129: * Decorate the text provided for the element type.
130: * This method should not be called unless a check for
131: * isEnabled() has been done first.
132: * Return null if there is no text or if there is an exception.
133: */
134: String decorateText(String text, Object element) {
135: try {
136: //Internal decorator might be null so be prepared
137: ILabelDecorator currentDecorator = internalGetDecorator();
138: if (currentDecorator != null) {
139: return currentDecorator.decorateText(text, element);
140: }
141: } catch (CoreException exception) {
142: handleCoreException(exception);
143: }
144: return null;
145: }
146:
147: /**
148: * Returns the decorator, or <code>null</code> if not enabled.
149: *
150: * @return the decorator, or <code>null</code> if not enabled
151: */
152: public ILabelDecorator getDecorator() {
153: return decorator;
154: }
155:
156: /* (non-Javadoc)
157: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#internalGetLabelProvider()
158: */
159: protected IBaseLabelProvider internalGetLabelProvider()
160: throws CoreException {
161: return internalGetDecorator();
162: }
163:
164: /* (non-Javadoc)
165: * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#isFull()
166: */
167: public boolean isFull() {
168: return true;
169: }
170:
171: }
|