Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 386426a94d62e6dd093f3357f7042439c437b28c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*******************************************************************************
 * Copyright (c) 2000, 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.core.internal.variables;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.variables.IValueVariable;
import org.eclipse.core.variables.IValueVariableInitializer;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.osgi.util.NLS;

/**
 * Implementation of a value variable.
 */
public class ContributedValueVariable extends StringVariable implements IValueVariable {

	/**
	 * Variable value or <code>null</code> if none
	 */
	private String fValue;

	/**
	 * Whether this variable's value has been initialized
	 */
	private boolean fInitialized = false;

	/**
	 * Whether this variable is read only.  If true, users cannot change the value.
	 */
	private boolean fReadOnly;

	/**
	 * Constructs a new value variable with the given name, description, read only
	 * property and associated configuration element.  The value will be initialized
	 * from the configuration element the first time getValue() is called.
	 *
	 * @param name variable name
	 * @param description variable description or <code>null</code>
	 * @param readOnly whether the variable should be a read only variable
	 * @param configurationElement configuration element
	 */
	public ContributedValueVariable(String name, String description, boolean readOnly, IConfigurationElement configurationElement) {
		super(name, description, configurationElement);
		fReadOnly = readOnly;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.variables.IValueVariable#setValue(java.lang.String)
	 */
	@Override
	public void setValue(String value) {
		if (!isReadOnly() || !isInitialized()){
			fValue = value;
			setInitialized(true);
			StringVariableManager.getDefault().notifyChanged(this);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.variables.IValueVariable#getValue()
	 */
	@Override
	public String getValue() {
		if (!isInitialized()) {
			initialize();
		}
		return fValue;
	}

	/**
	 * Initialize this variable's value from the configuration element.
	 */
	private void initialize() {
		if (getConfigurationElement() != null) {
			// check for a explicit value specified in plug-in XML
			String value = getConfigurationElement().getAttribute("initialValue"); //$NON-NLS-1$
			if (value == null) {
				// check for initializer
				String className = getConfigurationElement().getAttribute("initializerClass"); //$NON-NLS-1$
				if (className != null) {
					try {
						Object object = getConfigurationElement().createExecutableExtension("initializerClass"); //$NON-NLS-1$
						if (object instanceof IValueVariableInitializer) {
							IValueVariableInitializer initializer = (IValueVariableInitializer)object;
							initializer.initialize(this);
						} else {
							VariablesPlugin.logMessage(NLS.bind("Unable to initialize variable {0} - initializer must be an instance of IValueVariableInitializer.", new String[]{getName()}), null); //$NON-NLS-1$
						}
					} catch (CoreException e) {
						VariablesPlugin.logMessage(NLS.bind("Unable to initialize variable {0}",new String[]{getName()}), e); //$NON-NLS-1$
					}
				}
			} else {
				setValue(value);
			}
		}
		setInitialized(true);
	}

	/**
	 * Returns whether this variable has been initialized with a value by one of:
	 * <ul>
	 * <li><code>setValue(String)</code></li>
	 * <li>its configuration element's <code>initialValue</code> attribute</li>
	 * <li>its configuration element's initializer</li>
	 * </ul>
	 * @return whether this variable has been initialized with a value
	 */
	protected boolean isInitialized() {
		return fInitialized;
	}

	/**
	 * Sets whether this variable has been initialized with a value.
	 *
	 * @param initialized whether this variable has been initialized
	 */
	protected void setInitialized(boolean initialized) {
		fInitialized = initialized;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.variables.IValueVariable#isReadOnly()
	 */
	@Override
	public boolean isReadOnly() {
		return fReadOnly;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.variables.IValueVariable#isContributed()
	 */
	@Override
	public boolean isContributed() {
		return getConfigurationElement() != null;
	}

}

Back to the top