KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.jface.viewers.IBaseLabelProvider;
16 import org.eclipse.jface.viewers.ILabelProviderListener;
17 import org.eclipse.ui.internal.ActionExpression;
18 import org.eclipse.ui.internal.WorkbenchPlugin;
19 import org.eclipse.ui.internal.registry.RegistryReader;
20
21 /**
22  * The DecoratorDefinition is the class that holds onto
23  * the label decorator, the name and the name of the
24  * class a decorator definition applies to,
25  */

26
27 public abstract class DecoratorDefinition {
28     
29     private static final String JavaDoc ATT_LABEL = "label"; //$NON-NLS-1$
30

31     private static final String JavaDoc ATT_OBJECT_CLASS = "objectClass"; //$NON-NLS-1$
32

33     static final String JavaDoc CHILD_ENABLEMENT = "enablement"; //$NON-NLS-1$
34

35     private static final String JavaDoc ATT_ADAPTABLE = "adaptable"; //$NON-NLS-1$
36

37     private static final String JavaDoc ATT_ENABLED = "state"; //$NON-NLS-1$
38

39     private ActionExpression enablement;
40
41     protected boolean enabled;
42
43     private boolean defaultEnabled;
44
45     private String JavaDoc id;
46
47     protected IConfigurationElement definingElement;
48
49     //A flag that is set if there is an error creating the decorator
50
protected boolean labelProviderCreationFailed = false;
51
52     private boolean hasReadEnablement;
53
54     static final String JavaDoc ATT_CLASS = "class";//$NON-NLS-1$
55

56     /**
57      * Create a new instance of the receiver with the
58      * supplied values.
59      */

60
61     DecoratorDefinition(String JavaDoc identifier, IConfigurationElement element) {
62
63         this.id = identifier;
64         this.definingElement = element;
65         
66         this.enabled = this.defaultEnabled = Boolean.valueOf(element.getAttribute(ATT_ENABLED)).booleanValue();
67     }
68
69     /**
70      * Gets the name.
71      * @return Returns a String
72      */

73     public String JavaDoc getName() {
74         return definingElement.getAttribute(ATT_LABEL);
75     }
76
77     /**
78      * Returns the description.
79      * @return String
80      */

81     public String JavaDoc getDescription() {
82         return RegistryReader.getDescription(definingElement);
83     }
84
85     /**
86      * Gets the enabled.
87      * @return Returns a boolean
88      */

89     public boolean isEnabled() {
90         return enabled;
91     }
92
93     /**
94      * Sets the enabled flag and adds or removes the decorator
95      * manager as a listener as appropriate.
96      * @param newState The enabled to set
97      */

98     public void setEnabled(boolean newState) {
99
100         //Only refresh if there has been a change
101
if (this.enabled != newState) {
102             this.enabled = newState;
103             try {
104                 refreshDecorator();
105             } catch (CoreException exception) {
106                 handleCoreException(exception);
107             }
108
109         }
110     }
111
112     /**
113      * Refresh the current decorator based on our enable
114      * state.
115      */

116     protected abstract void refreshDecorator() throws CoreException;
117
118     /**
119      * Dispose the decorator instance and remove listeners
120      * as appropirate.
121      * @param disposedDecorator
122      */

123     protected void disposeCachedDecorator(IBaseLabelProvider disposedDecorator) {
124         disposedDecorator.removeListener(WorkbenchPlugin.getDefault()
125                 .getDecoratorManager());
126         disposedDecorator.dispose();
127
128     }
129
130     /**
131      * Return whether or not this decorator should be
132      * applied to adapted types.
133      *
134      * @return whether or not this decorator should be
135      * applied to adapted types
136      */

137     public boolean isAdaptable() {
138         return Boolean.valueOf(definingElement.getAttribute(ATT_ADAPTABLE)).booleanValue();
139     }
140
141     /**
142      * Gets the id.
143      * @return Returns a String
144      */

145     public String JavaDoc getId() {
146         return id;
147     }
148
149     /**
150      * Return the default value for this type - this value
151      * is the value read from the element description.
152      *
153      * @return the default value for this type - this value
154      * is the value read from the element description
155      */

156     public boolean getDefaultValue() {
157         return defaultEnabled;
158     }
159
160     /**
161      * Returns the enablement.
162      * @return ActionExpression
163      */

164     protected ActionExpression getEnablement() {
165         if (!hasReadEnablement) {
166             hasReadEnablement = true;
167             initializeEnablement();
168         }
169         return enablement;
170     }
171
172     /**
173      * Initialize the enablement expression for this decorator
174      */

175     protected void initializeEnablement() {
176         IConfigurationElement[] elements = definingElement.getChildren(CHILD_ENABLEMENT);
177         if (elements.length == 0) {
178             String JavaDoc className = definingElement.getAttribute(ATT_OBJECT_CLASS);
179             if (className != null) {
180                 enablement = new ActionExpression(ATT_OBJECT_CLASS,
181                         className);
182             }
183         } else {
184             enablement = new ActionExpression(elements[0]);
185         }
186     }
187
188     /**
189      * Add a listener for the decorator.If there is an exception
190      * then inform the user and disable the receiver.
191      * This method should not be called unless a check for
192      * isEnabled() has been done first.
193      */

194     void addListener(ILabelProviderListener listener) {
195         try {
196             //Internal decorator might be null so be prepared
197
IBaseLabelProvider currentDecorator = internalGetLabelProvider();
198             if (currentDecorator != null) {
199                 currentDecorator.addListener(listener);
200             }
201         } catch (CoreException exception) {
202             handleCoreException(exception);
203         }
204     }
205
206     /**
207      * Return whether or not the decorator registered for element
208      * has a label property called property name. If there is an
209      * exception disable the receiver and return false.
210      * This method should not be called unless a check for
211      * isEnabled() has been done first.
212      */

213     boolean isLabelProperty(Object JavaDoc element, String JavaDoc property) {
214         try { //Internal decorator might be null so be prepared
215
IBaseLabelProvider currentDecorator = internalGetLabelProvider();
216             if (currentDecorator != null) {
217                 return currentDecorator.isLabelProperty(element, property);
218             }
219         } catch (CoreException exception) {
220             handleCoreException(exception);
221             return false;
222         }
223         return false;
224     }
225
226     /**
227      * Gets the label provider and creates it if it does not exist yet.
228      * Throws a CoreException if there is a problem
229      * creating the labelProvider.
230      * This method should not be called unless a check for
231      * enabled to be true is done first.
232      * @return Returns a ILabelDecorator
233      */

234     protected abstract IBaseLabelProvider internalGetLabelProvider()
235             throws CoreException;
236
237     /**
238      * A CoreException has occured. Inform the user and disable
239      * the receiver.
240      */

241
242     protected void handleCoreException(CoreException exception) {
243
244         //If there is an error then reset the enabling to false
245
WorkbenchPlugin.log(exception);
246         crashDisable();
247     }
248
249     /**
250      * A crash has occured. Disable the receiver without notification.
251      */

252     public void crashDisable() {
253         this.enabled = false;
254     }
255
256     /**
257      * Return whether or not this is a full or lightweight definition.
258      * @return <code>true</code> if this is not a lightweight decorator.
259      */

260     public abstract boolean isFull();
261
262     /**
263      * Return the configuration element.
264      *
265      * @return the configuration element
266      * @since 3.1
267      */

268     public IConfigurationElement getConfigurationElement() {
269         return definingElement;
270     }
271
272     /**
273      * Return whether the decorator is applicable to the given element
274      * @param element the element to be decorated
275      * @return whether the decorator w=should be applied to the element
276      */

277     public boolean isEnabledFor(Object JavaDoc element) {
278         if(isEnabled()){
279             ActionExpression expression = getEnablement();
280             if(expression != null) {
281                 return expression.isEnabledFor(element);
282             }
283             return true;//Always on if no expression
284
}
285         return false;
286        
287     }
288 }
289
Popular Tags