KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > variables > ContributedValueVariable


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.core.internal.variables;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IConfigurationElement;
15 import org.eclipse.core.variables.IValueVariable;
16 import org.eclipse.core.variables.IValueVariableInitializer;
17 import org.eclipse.core.variables.VariablesPlugin;
18 import org.eclipse.osgi.util.NLS;
19
20 /**
21  * Implementation of a value variable.
22  */

23 public class ContributedValueVariable extends StringVariable implements IValueVariable {
24     
25     /**
26      * Variable value or <code>null</code> if none
27      */

28     private String JavaDoc fValue;
29     
30     /**
31      * Whether this variable's value has been initialized
32      */

33     private boolean fInitialized = false;
34     
35     /**
36      * Whether this variable is read only. If true, users cannot change the value.
37      */

38     private boolean fReadOnly;
39     
40     /**
41      * Constructs a new value variable with the given name, description, read only
42      * property and associated configuration element. The value will be initialized
43      * from the configuration element the first time getValue() is called.
44      *
45      * @param name variable name
46      * @param description variable description or <code>null</code>
47      * @param readOnly whether the variable should be a read only variable
48      * @param configurationElement configuration element
49      */

50     public ContributedValueVariable(String JavaDoc name, String JavaDoc description, boolean readOnly, IConfigurationElement configurationElement) {
51         super(name, description, configurationElement);
52         fReadOnly = readOnly;
53     }
54     
55     /* (non-Javadoc)
56      * @see org.eclipse.core.variables.IValueVariable#setValue(java.lang.String)
57      */

58     public void setValue(String JavaDoc value) {
59         if (!isReadOnly() || !isInitialized()){
60             fValue = value;
61             setInitialized(true);
62             StringVariableManager.getDefault().notifyChanged(this);
63         }
64     }
65     
66     /* (non-Javadoc)
67      * @see org.eclipse.core.variables.IValueVariable#getValue()
68      */

69     public String JavaDoc getValue() {
70         if (!isInitialized()) {
71             initialize();
72         }
73         return fValue;
74     }
75
76     /**
77      * Initialize this variable's value from the configuration element.
78      */

79     private void initialize() {
80         if (getConfigurationElement() != null) {
81             // check for a explicit value specified in plug-in XML
82
String JavaDoc value = getConfigurationElement().getAttribute("initialValue"); //$NON-NLS-1$
83
if (value == null) {
84                 // check for initializer
85
String JavaDoc className = getConfigurationElement().getAttribute("initializerClass"); //$NON-NLS-1$
86
if (className != null) {
87                     try {
88                         Object JavaDoc object = getConfigurationElement().createExecutableExtension("initializerClass"); //$NON-NLS-1$
89
if (object instanceof IValueVariableInitializer) {
90                             IValueVariableInitializer initializer = (IValueVariableInitializer)object;
91                             initializer.initialize(this);
92                         } else {
93                             VariablesPlugin.logMessage(NLS.bind("Unable to initialize variable {0} - initializer must be an instance of IValueVariableInitializer.", new String JavaDoc[]{getName()}), null); //$NON-NLS-1$
94
}
95                     } catch (CoreException e) {
96                         VariablesPlugin.logMessage(NLS.bind("Unable to initialize variable {0}",new String JavaDoc[]{getName()}), e); //$NON-NLS-1$
97
}
98                 }
99             } else {
100                 setValue(value);
101             }
102         }
103         setInitialized(true);
104     }
105
106     /**
107      * Returns whether this variable has been initialized with a value by one of:
108      * <ul>
109      * <li><code>setValue(String)</code></li>
110      * <li>its configuration element's <code>initialValue</code> attribute</li>
111      * <li>its configuration element's initializer</li>
112      * </ul>
113      * @return whether this variable has been initialized with a value
114      */

115     protected boolean isInitialized() {
116         return fInitialized;
117     }
118     
119     /**
120      * Sets whether this variable has been initialized with a value.
121      *
122      * @param initialized whether this variable has been initialized
123      */

124     protected void setInitialized(boolean initialized) {
125         fInitialized = initialized;
126     }
127
128     /* (non-Javadoc)
129      * @see org.eclipse.core.variables.IValueVariable#isReadOnly()
130      */

131     public boolean isReadOnly() {
132         return fReadOnly;
133     }
134
135     /* (non-Javadoc)
136      * @see org.eclipse.core.variables.IValueVariable#isContributed()
137      */

138     public boolean isContributed() {
139         return getConfigurationElement() != null;
140     }
141
142 }
143
Popular Tags