diff options
author | Darin Wright | 2007-02-01 15:30:04 +0000 |
---|---|---|
committer | Darin Wright | 2007-02-01 15:30:04 +0000 |
commit | bbca1727f1b702bf9a1181abf85a2bc7f1c98e67 (patch) | |
tree | d4823adcadbc9e18ac438f5da26ed5792d8e6e08 /org.eclipse.core.variables | |
parent | c51731e747d30f768d664839fc4c0a8938a0826c (diff) | |
download | eclipse.platform.debug-bbca1727f1b702bf9a1181abf85a2bc7f1c98e67.tar.gz eclipse.platform.debug-bbca1727f1b702bf9a1181abf85a2bc7f1c98e67.tar.xz eclipse.platform.debug-bbca1727f1b702bf9a1181abf85a2bc7f1c98e67.zip |
Bug 165969 API to allow a contributed JRE and variables to be re-initialized
Diffstat (limited to 'org.eclipse.core.variables')
6 files changed, 270 insertions, 141 deletions
diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ContributedValueVariable.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ContributedValueVariable.java new file mode 100644 index 000000000..5a3d23189 --- /dev/null +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ContributedValueVariable.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2000, 2006 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) + */ + 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() + */ + 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() + */ + public boolean isReadOnly() { + return fReadOnly; + } + + /* (non-Javadoc) + * @see org.eclipse.core.variables.IValueVariable#isContributed() + */ + public boolean isContributed() { + return getConfigurationElement() != null; + } + +} diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariable.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariable.java index 27d7d61c5..dc2448617 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariable.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariable.java @@ -34,10 +34,11 @@ public abstract class StringVariable implements IStringVariable { private IConfigurationElement fConfigurationElement; /** - * Constructs a new variable with the given name and description. + * Constructs a new variable with the given name, description and configuration element. * * @param name variable name * @param description variable description, or <code>null</code> + * @param configurationElement configuration element or <code>null</code> */ public StringVariable(String name, String description, IConfigurationElement configurationElement) { fName = name; diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java index 8c3fd8a35..3694596be 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/StringVariableManager.java @@ -93,14 +93,15 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC // Variable extension point constants private static final String ATTR_NAME= "name"; //$NON-NLS-1$ - private static final String ATTR_DESCRIPTION="description"; //$NON-NLS-1$ + private static final String ATTR_DESCRIPTION="description"; //$NON-NLS-1$ + private static final String ATTR_READ_ONLY="readOnly"; //$NON-NLS-1$ // Persisted variable XML constants private static final String VALUE_VARIABLES_TAG= "valueVariables"; //$NON-NLS-1$ private static final String VALUE_VARIABLE_TAG= "valueVariable"; //$NON-NLS-1$ private static final String NAME_TAG= "name"; //$NON-NLS-1$ private static final String VALUE_TAG= "value"; //$NON-NLS-1$ private static final String DESCRIPTION_TAG="description"; //$NON-NLS-1$ - private static final String INITIALIZED_TAG="contributed"; //$NON-NLS-1$ + private static final String READ_ONLY_TAG="readOnly"; //$NON-NLS-1$ // XML values private static final String TRUE_VALUE= "true"; //$NON-NLS-1$ private static final String FALSE_VALUE= "false"; //$NON-NLS-1$ @@ -200,8 +201,8 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC fInternalChange = true; fDynamicVariables = new HashMap(5); fValueVariables = new HashMap(5); - loadPersistedValueVariables(); loadContributedValueVariables(); + loadPersistedValueVariables(); loadDynamicVariables(); VariablesPlugin.getDefault().getPluginPreferences().addPropertyChangeListener(this); fInternalChange = false; @@ -228,7 +229,32 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC } /** - * Loads any persisted value variables from the preference store. + * Loads contributed value variables. This is done before loading persisted values. + */ + private void loadContributedValueVariables() { + IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(VariablesPlugin.PI_CORE_VARIABLES, EXTENSION_POINT_VALUE_VARIABLES); + IConfigurationElement elements[]= point.getConfigurationElements(); + for (int i = 0; i < elements.length; i++) { + IConfigurationElement element = elements[i]; + String name= element.getAttribute(ATTR_NAME); + if (name == null) { + VariablesPlugin.logMessage(NLS.bind("Variable extension missing required 'name' attribute: {0}", new String[] {element.getDeclaringExtension().getLabel()}), null); //$NON-NLS-1$ + continue; + } + String description= element.getAttribute(ATTR_DESCRIPTION); + boolean isReadOnly = TRUE_VALUE.equals(element.getAttribute(ATTR_READ_ONLY)); + + IValueVariable variable = new ContributedValueVariable(name, description, isReadOnly, element); + fValueVariables.put(name, variable); + } + } + + /** + * Loads persisted value variables from the preference store. This is done after + * loading value variables from the extension point. If a persisted variable has the + * same name as a extension contributed variable the variable's value will be set to + * the persisted value unless either a) The persisted value is <code>null</code>, or + * b) the variable is read-only. */ private void loadPersistedValueVariables() { String variablesString= VariablesPlugin.getDefault().getPluginPreferences().getString(PREF_VALUE_VARIABLES); @@ -274,47 +300,22 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC if (name.length() > 0) { String value= element.getAttribute(VALUE_TAG); String description= element.getAttribute(DESCRIPTION_TAG); - boolean initialized= TRUE_VALUE.equals(element.getAttribute(INITIALIZED_TAG)); - ValueVariable variable= new ValueVariable(name, description, null); - if (initialized) { - variable.setValue(value); + boolean readOnly= TRUE_VALUE.equals(element.getAttribute(READ_ONLY_TAG)); + + IValueVariable existing = getValueVariable(name); + if (existing == null){ + ValueVariable variable = new ValueVariable(name, description, readOnly, value); + fValueVariables.put(name, variable); + } else if (!existing.isReadOnly() && value != null){ + existing.setValue(value); } - fValueVariables.put(name, variable); } else { VariablesPlugin.logMessage("Invalid variable entry encountered while loading value variables. Variable name is null.", null); //$NON-NLS-1$ } } } } - - /** - * Loads contributed value variables. This is done after the persisted value - * variables are restored. Any contributed variables with the same name are - * merged with existing persisted values. - */ - private void loadContributedValueVariables() { - IExtensionPoint point = Platform.getExtensionRegistry().getExtensionPoint(VariablesPlugin.PI_CORE_VARIABLES, EXTENSION_POINT_VALUE_VARIABLES); - IConfigurationElement elements[]= point.getConfigurationElements(); - for (int i = 0; i < elements.length; i++) { - IConfigurationElement element = elements[i]; - String name= element.getAttribute(ATTR_NAME); - if (name == null) { - VariablesPlugin.logMessage(NLS.bind("Variable extension missing required 'name' attribute: {0}", new String[] {element.getDeclaringExtension().getLabel()}), null); //$NON-NLS-1$ - continue; - } - String description= element.getAttribute(ATTR_DESCRIPTION); - ValueVariable variable= new ValueVariable(name, description, element); - // if already present, merge with persisted value - ValueVariable existing = (ValueVariable)getValueVariable(name); - if (existing != null) { - if (existing.isInitialized()) { - variable.setValue(existing.getValue()); - } - } - fValueVariables.put(variable.getName(), variable); - } - } - + /* (non-Javadoc) * @see org.eclipse.debug.internal.core.stringsubstitution.IStringVariableManager#getVariables() */ @@ -353,14 +354,16 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC * @see org.eclipse.debug.internal.core.stringsubstitution.IStringVariableManager#newValueVariable(java.lang.String, java.lang.String) */ public IValueVariable newValueVariable(String name, String description) { - IConfigurationElement element = null; - ValueVariable existing = (ValueVariable)getValueVariable(name); - if (existing != null && existing.isContributed()) { - element = existing.getConfigurationElement(); - } - return new ValueVariable(name, description, element); + return newValueVariable(name, description, false, null); } - + + /* (non-Javadoc) + * @see org.eclipse.core.variables.IStringVariableManager#newValueVariable(java.lang.String, java.lang.String, boolean, java.lang.String) + */ + public IValueVariable newValueVariable(String name, String description, boolean readOnly, String value) { + return new ValueVariable(name, description, readOnly, value); + } + /* (non-Javadoc) * @see org.eclipse.debug.internal.core.stringsubstitution.IStringVariableManager#addVariables(org.eclipse.debug.internal.core.stringsubstitution.IValueVariable[]) */ @@ -447,19 +450,24 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC Element rootElement= document.createElement(VALUE_VARIABLES_TAG); document.appendChild(rootElement); for (int i = 0; i < variables.length; i++) { - ValueVariable variable = (ValueVariable)variables[i]; - Element element= document.createElement(VALUE_VARIABLE_TAG); - element.setAttribute(NAME_TAG, variable.getName()); - String value= variable.getValue(); - if (value != null) { - element.setAttribute(VALUE_TAG, value); - } - String description= variable.getDescription(); - if (description != null) { - element.setAttribute(DESCRIPTION_TAG, description); + IValueVariable variable = variables[i]; + if (!variable.isReadOnly()){ + // don't persist read-only variables or un-initialized contributed variables + if (!variable.isContributed() || ((ContributedValueVariable)variable).isInitialized()) { + Element element= document.createElement(VALUE_VARIABLE_TAG); + element.setAttribute(NAME_TAG, variable.getName()); + String value= variable.getValue(); + if (value != null) { + element.setAttribute(VALUE_TAG, value); + } + element.setAttribute(READ_ONLY_TAG, variable.isReadOnly() ? TRUE_VALUE : FALSE_VALUE); + String description= variable.getDescription(); + if (description != null) { + element.setAttribute(DESCRIPTION_TAG, description); + } + rootElement.appendChild(element); + } } - element.setAttribute(INITIALIZED_TAG, variable.isInitialized() ? TRUE_VALUE : FALSE_VALUE); - rootElement.appendChild(element); } return serializeDocument(document); } @@ -527,7 +535,7 @@ public class StringVariableManager implements IStringVariableManager, IPropertyC * * @param variable the variable that has changed */ - protected void notifyChanged(ValueVariable variable) { + protected void notifyChanged(IValueVariable variable) { if (!fInternalChange) { IValueVariable existing = getValueVariable(variable.getName()); if (variable.equals(existing)) { diff --git a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ValueVariable.java b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ValueVariable.java index dc92fe734..9efb48618 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ValueVariable.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/internal/variables/ValueVariable.java @@ -10,12 +10,7 @@ *******************************************************************************/ 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. @@ -28,98 +23,54 @@ public class ValueVariable extends StringVariable implements IValueVariable { private String fValue; /** - * Whether this variable's value has been initialized + * Whether this variable is read only. If true, users cannot change the value. */ - private boolean fInitialized = false; + private boolean fReadOnly; /** - * Constructs a new value variable with the given name, description, and - * associated configuration element. + * Constructs a new value variable with the given name, description, read only + * property and string value. Value can be null. * * @param name variable name - * @param description variable description, or <code>null</code> - * @param configurationElement configuration element or <code>null</code> + * @param description variable description or <code>null</code> + * @param readOnly whether the variable should be a read only variable + * @param value the initial value of the variable or <code>null</code> */ - public ValueVariable(String name, String description, IConfigurationElement configurationElement) { - super(name, description, configurationElement); + public ValueVariable(String name, String description, boolean readOnly, String value) { + super(name, description, null); + fReadOnly = readOnly; + fValue = value; } - + /* (non-Javadoc) - * @see org.eclipse.debug.internal.core.stringsubstitution.IValueVariable#setValue(java.lang.String) + * @see org.eclipse.core.variables.IValueVariable#setValue(java.lang.String) */ public void setValue(String value) { - fValue = value; - setInitialized(true); - StringVariableManager.getDefault().notifyChanged(this); + if (!isReadOnly()){ + fValue = value; + StringVariableManager.getDefault().notifyChanged(this); + } } - + /* (non-Javadoc) - * @see org.eclipse.debug.internal.core.stringsubstitution.IValueVariable#getValue() + * @see org.eclipse.core.variables.IValueVariable#getValue() */ public String getValue() { - if (!isInitialized()) { - initialize(); - } return fValue; } - /** - * Initialize this variable's value. + /* (non-Javadoc) + * @see org.eclipse.core.variables.IValueVariable#isReadOnly() */ - 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); + public boolean isReadOnly() { + return fReadOnly; } - - /** - * 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.debug.internal.core.stringsubstitution.IValueVariable#isContributed() + * @see org.eclipse.core.variables.IValueVariable#isContributed() */ public boolean isContributed() { - return getConfigurationElement() != null; + return false; } } diff --git a/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java b/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java index 731718a4e..db2fde0be 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/variables/IStringVariableManager.java @@ -122,14 +122,27 @@ public interface IStringVariableManager { public void validateStringVariables(String expression) throws CoreException; /** - * Returns a new value variable with the given name and description. + * Returns a new read-write value variable with the given name and description + * with a <code>null</code> value. * * @param name variable name, cannot be <code>null</code> * @param description variable description, possibly <code>null</code> - * @return a new variable - * @exception CoreException if a variable already exists with the given name + * @return a new value variable */ public IValueVariable newValueVariable(String name, String description); + + /** + * Returns a new value variable with the given properties. + * + * @param name variable name, cannot be <code>null</code> + * @param description variable description, possibly <code>null</code> + * @param readOnly whether this variable is to be a read only variable + * @param value the string value to initialize this variable to - should + * not be <code>null</code> for read-only variables + * @return a new value variable + * @since 3.3 + */ + public IValueVariable newValueVariable(String name, String description, boolean readOnly, String value); /** * Adds the given variables to the variable registry. diff --git a/org.eclipse.core.variables/src/org/eclipse/core/variables/IValueVariable.java b/org.eclipse.core.variables/src/org/eclipse/core/variables/IValueVariable.java index 29a246547..a8b7b1470 100644 --- a/org.eclipse.core.variables/src/org/eclipse/core/variables/IValueVariable.java +++ b/org.eclipse.core.variables/src/org/eclipse/core/variables/IValueVariable.java @@ -15,7 +15,12 @@ package org.eclipse.core.variables; * a value variable is referenced does not effect the value of the variable. * A value variable can be contributed by an extension or created programmatically. * A contributor may optionally specify an initial value for a variable, or - * provide a delegate that will initialize the variable with a value. + * provide a delegate that will initialize the variable with a value. + * <p> + * Since 3.3, a variable can be specified as a "read only" preventing users from changing + * the value after it has been initialized. Furthermore, a read only variable that is + * contributed by an extension will always load the value from the extension. + * </p> * <p> * Example of a value variable contribution with an initial value, the specified * variable is created with the initial value "/usr/local/foo". @@ -49,10 +54,10 @@ package org.eclipse.core.variables; public interface IValueVariable extends IStringVariable { /** - * Sets the value of this variable to the given value. A value of - * <code>null</code> indicates the value of this variable is undefined. - * - * @param value variable value, possibly <code>null</code> + * Sets the value of this variable to the given value. + * Since 3.3, this has no effect if this variable is read only. + * + * @param variable value */ public void setValue(String value); @@ -71,9 +76,18 @@ public interface IValueVariable extends IStringVariable { public boolean isContributed(); /** + * Returns whether this variable is read only. + * + * @return whether this variable is read only + * @since 3.3 + */ + public boolean isReadOnly(); + + /** * Sets the description of this variable to the given value. * * @param description variable description, possibly <code>null</code> */ public void setDescription(String description); + } |