KickJava   Java API By Example, From Geeks To Geeks.

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


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.Platform;
16 import org.eclipse.jface.util.SafeRunnable;
17 import org.eclipse.jface.viewers.IBaseLabelProvider;
18 import org.eclipse.jface.viewers.ILabelDecorator;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.ui.internal.WorkbenchMessages;
22 import org.eclipse.ui.internal.WorkbenchPlugin;
23
24 /**
25  * The RunnableDecoratorDefinition is the definition for
26  * decorators that have an ILabelDecorator class to instantiate.
27  */

28
29 class FullDecoratorDefinition extends DecoratorDefinition {
30
31     ILabelDecorator decorator;
32
33     /**
34      * Create a new instance of the receiver with the
35      * supplied values.
36      */

37
38     FullDecoratorDefinition(String JavaDoc identifier, IConfigurationElement element) {
39         super(identifier, element);
40     }
41
42     /**
43      * Gets the decorator and creates it if it does
44      * not exist yet. Throws a CoreException if there is a problem
45      * creating the decorator.
46      * This method should not be called unless a check for
47      * enabled to be true is done first.
48      * @return Returns a ILabelDecorator
49      */

50     protected ILabelDecorator internalGetDecorator() throws CoreException {
51         if (labelProviderCreationFailed) {
52             return null;
53         }
54
55         final CoreException[] exceptions = new CoreException[1];
56
57         if (decorator == null) {
58             Platform
59                     .run(new SafeRunnable(
60                             NLS.bind(WorkbenchMessages.DecoratorManager_ErrorActivatingDecorator, getName() )) {
61                         public void run() {
62                             try {
63                                 decorator = (ILabelDecorator) WorkbenchPlugin
64                                         .createExtension(
65                                                 definingElement,
66                                                 DecoratorDefinition.ATT_CLASS);
67                                 decorator.addListener(WorkbenchPlugin
68                                         .getDefault().getDecoratorManager());
69                             } catch (CoreException exception) {
70                                 exceptions[0] = exception;
71                             }
72                         }
73                     });
74         } else {
75             return decorator;
76         }
77
78         if (decorator == null) {
79             this.labelProviderCreationFailed = true;
80             setEnabled(false);
81         }
82
83         if (exceptions[0] != null) {
84             throw exceptions[0];
85         }
86
87         return decorator;
88     }
89
90     /* (non-Javadoc)
91      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#refreshDecorator()
92      */

93     protected void refreshDecorator() {
94         //Only do something if disabled so as to prevent
95
//gratutitous activation
96
if (!this.enabled && decorator != null) {
97             IBaseLabelProvider cached = decorator;
98             decorator = null;
99             disposeCachedDecorator(cached);
100         }
101     }
102
103     /**
104      * Decorate the image provided for the element type.
105      * This method should not be called unless a check for
106      * isEnabled() has been done first.
107      * Return null if there is no image or if an error occurs.
108      */

109     Image decorateImage(Image image, Object JavaDoc element) {
110         try {
111             //Internal decorator might be null so be prepared
112
ILabelDecorator currentDecorator = internalGetDecorator();
113             if (currentDecorator != null) {
114                 return currentDecorator.decorateImage(image, element);
115             }
116
117         } catch (CoreException exception) {
118             handleCoreException(exception);
119         }
120         return null;
121     }
122
123     /**
124      * Decorate the text provided for the element type.
125      * This method should not be called unless a check for
126      * isEnabled() has been done first.
127      * Return null if there is no text or if there is an exception.
128      */

129     String JavaDoc decorateText(String JavaDoc text, Object JavaDoc element) {
130         try {
131             //Internal decorator might be null so be prepared
132
ILabelDecorator currentDecorator = internalGetDecorator();
133             if (currentDecorator != null) {
134                 return currentDecorator.decorateText(text, element);
135             }
136         } catch (CoreException exception) {
137             handleCoreException(exception);
138         }
139         return null;
140     }
141
142     /**
143      * Returns the decorator, or <code>null</code> if not enabled.
144      *
145      * @return the decorator, or <code>null</code> if not enabled
146      */

147     public ILabelDecorator getDecorator() {
148         return decorator;
149     }
150
151     /* (non-Javadoc)
152      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#internalGetLabelProvider()
153      */

154     protected IBaseLabelProvider internalGetLabelProvider()
155             throws CoreException {
156         return internalGetDecorator();
157     }
158
159     /* (non-Javadoc)
160      * @see org.eclipse.ui.internal.decorators.DecoratorDefinition#isFull()
161      */

162     public boolean isFull() {
163         return true;
164     }
165
166 }
167
Popular Tags