From 975547bcca5fd29c67942e501ec2634d5799331d Mon Sep 17 00:00:00 2001 From: Mikhail Sennikovsky Date: Tue, 27 Feb 2007 12:13:30 +0000 Subject: [PATCH] 1. Fix for the [Bug 175675] ClassCastException when parsing a file 2. Path Entry Variable Manager functionality is made as a wrapper over the Workspace-level Cdt Variables (aka Build Macros) functionality 3. bug-fixes --- .../ConfigurationDataProvider.java | 2 +- .../util/PathEntryResolveInfoElement.java | 1 - .../model/util/PathEntryTranslator.java | 12 + .../core/settings/model/CLanguageSetting.java | 58 +--- .../settings/model/CLanguageSettingCache.java | 32 +- .../model/CProjectDescriptionManager.java | 58 ++++ .../model/DescriptionScannerInfoProvider.java | 23 +- .../src/org/eclipse/cdt/core/CCorePlugin.java | 5 +- .../core/CdtVarPathEntryVariableManager.java | 253 ++++++++++++++++ .../core/PathEntryVariableManager.java | 13 +- .../ICdtVariableChangeListener.java | 15 + .../UserDefinedVariableSupplier.java | 282 +++++++++++++++++- .../cdtvariables/VariableChangeEvent.java | 37 +++ 13 files changed, 722 insertions(+), 69 deletions(-) create mode 100644 core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/CdtVarPathEntryVariableManager.java create mode 100644 core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/ICdtVariableChangeListener.java create mode 100644 core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/VariableChangeEvent.java diff --git a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/dataprovider/ConfigurationDataProvider.java b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/dataprovider/ConfigurationDataProvider.java index 8b364b8f38f..30220c21fab 100644 --- a/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/dataprovider/ConfigurationDataProvider.java +++ b/build/org.eclipse.cdt.managedbuilder.core/src/org/eclipse/cdt/managedbuilder/internal/dataprovider/ConfigurationDataProvider.java @@ -297,7 +297,7 @@ public class ConfigurationDataProvider extends CConfigurationDataProvider implem Tool tool = (Tool)tc.createTool(extTool, id, name, false); InputType type = (InputType)tool.getInputTypes()[0]; type = (InputType)tool.getEdtableInputType(type); - type.setSourceContentTypes(type.getSourceContentTypes()); + type.setSourceContentTypes(des.getContentTypes()); type.setLanguageNameAttribute(des.getName()); type.setName(des.getName()); type.setLanguageIdAttribute(des.getId()); diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryResolveInfoElement.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryResolveInfoElement.java index 4f5c50ee6d1..2f6dc612980 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryResolveInfoElement.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryResolveInfoElement.java @@ -10,7 +10,6 @@ *******************************************************************************/ package org.eclipse.cdt.core.settings.model.util; -import java.util.ArrayList; import java.util.List; import org.eclipse.cdt.core.model.IPathEntry; diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryTranslator.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryTranslator.java index 449cec54f80..7dc11664bf4 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryTranslator.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/settings/model/util/PathEntryTranslator.java @@ -31,6 +31,7 @@ import org.eclipse.cdt.core.model.ILibraryEntry; import org.eclipse.cdt.core.model.IMacroEntry; import org.eclipse.cdt.core.model.IMacroFileEntry; import org.eclipse.cdt.core.model.IPathEntry; +import org.eclipse.cdt.core.resources.IPathEntryVariableManager; import org.eclipse.cdt.core.settings.model.CIncludeFileEntry; import org.eclipse.cdt.core.settings.model.CIncludePathEntry; import org.eclipse.cdt.core.settings.model.CLibraryFileEntry; @@ -556,12 +557,21 @@ public class PathEntryTranslator { do{ // IPath p; // IPath inc = getIncludePath(); + IPath unresolvedBase = basePath; + IPath unresolvedValue = valuePath; + IPathEntryVariableManager mngr = CCorePlugin.getDefault().getPathEntryVariableManager(); + + basePath = mngr.resolvePath(basePath); + valuePath = mngr.resolvePath(valuePath); + if (!basePath.isEmpty()) { IPath loc = basePath; if (!loc.isAbsolute()) { ResourceInfo rcInfo = findResourceInfo(fRoot, loc.append(valuePath), !isFile); if (rcInfo.fExists) { fResourceInfo = rcInfo; + fName = unresolvedBase.append(unresolvedValue).toString(); + fValue = fName; // fFullPath = fResourceInfo.fRc.getFullPath(); // fLocation = fResourceInfo.fRc.getLocation(); // fName = fValuePath.toString(); @@ -570,6 +580,8 @@ public class PathEntryTranslator { } } fLocation = loc.append(valuePath); + fName = unresolvedBase.append(unresolvedValue).toString(); + fValue = fName; // fName = fValuePath.toString(); // fValue = fValuePath.toString(); break; diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/CLanguageSetting.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/CLanguageSetting.java index 18b9504bd27..9ba4e6dbba3 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/CLanguageSetting.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/settings/model/CLanguageSetting.java @@ -11,7 +11,6 @@ package org.eclipse.cdt.internal.core.settings.model; import java.util.ArrayList; -import java.util.Arrays; import java.util.Iterator; import java.util.List; @@ -23,12 +22,6 @@ import org.eclipse.cdt.core.settings.model.extension.impl.CDefaultLanguageData; import org.eclipse.cdt.core.settings.model.util.CDataUtil; import org.eclipse.cdt.core.settings.model.util.EntryStore; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.ProjectScope; -import org.eclipse.core.runtime.Platform; -import org.eclipse.core.runtime.content.IContentType; -import org.eclipse.core.runtime.content.IContentTypeManager; -import org.eclipse.core.runtime.content.IContentTypeSettings; -import org.eclipse.core.runtime.preferences.IScopeContext; public class CLanguageSetting extends CDataProxy implements ICLanguageSetting { @@ -97,36 +90,6 @@ public class CLanguageSetting extends CDataProxy implements return (getSupportedEntryKinds() & kind) == kind; } - public String[] getContentTypeFileSpecs (IContentType type) { - String[] globalSpecs = type.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); - IContentTypeSettings settings = null; - IProject project = getProject(); - if (project != null) { - IScopeContext projectScope = new ProjectScope(project); - try { - settings = type.getSettings(projectScope); - } catch (Exception e) {} - if (settings != null) { - String[] specs = settings.getFileSpecs(IContentType.FILE_EXTENSION_SPEC); - if (specs.length > 0) { - int total = globalSpecs.length + specs.length; - String[] projSpecs = new String[total]; - int i=0; - for (int j=0; j 0) { + int total = globalSpecs.length + specs.length; + String[] projSpecs = new String[total]; + int i=0; + for (int j=0; j vars.length; i++){ + if(getVariablePath(vars[i]) != null) + list.add(vars[i].getName()); + } + return (String[])list.toArray(new String[list.size()]); + } + + public boolean isDefined(String name) { + return getValue(name) != null; + } + + public IPath resolvePath(IPath path) { + String str = path.toPortableString(); + + try { + str = CdtVariableResolver.resolveToString(str, fSubstitutor); + } catch (CdtVariableException e) { + CCorePlugin.log(e); + } + + return new Path(str); + } + + public void setValue(String name, IPath value) throws CoreException { + if(value != null) + fUserVarSupplier.createMacro(name, ICdtVariable.VALUE_PATH_ANY, value.toString(), ICoreVariableContextInfo.CONTEXT_WORKSPACE, null); + else + fUserVarSupplier.deleteMacro(name, ICoreVariableContextInfo.CONTEXT_WORKSPACE, null); + + fUserVarSupplier.storeWorkspaceVariables(false); + } + + /** + * Fires a property change event corresponding to a change to the + * current value of the variable with the given name. + * + * @param name the name of the variable, to be used as the variable + * in the event object + * @param value the current value of the path variable or null if + * the variable was deleted + * @param type one of IPathVariableChangeEvent.VARIABLE_CREATED, + * PathEntryVariableChangeEvent.VARIABLE_CHANGED, or + * PathEntryVariableChangeEvent.VARIABLE_DELETED + * @see PathEntryVariableChangeEvent + * @see PathEntryVariableChangeEvent#VARIABLE_CREATED + * @see PathEntryVariableChangeEvent#VARIABLE_CHANGED + * @see PathEntryVariableChangeEvent#VARIABLE_DELETED + */ + private void fireVariableChangeEvent(String name, IPath value, int type) { + if (this.fListeners.size() == 0) + return; + // use a separate collection to avoid interference of simultaneous additions/removals + Object[] listenerArray = this.fListeners.toArray(); + final PathEntryVariableChangeEvent pve = new PathEntryVariableChangeEvent(this, name, value, type); + for (int i = 0; i < listenerArray.length; ++i) { + final IPathEntryVariableChangeListener l = (IPathEntryVariableChangeListener) listenerArray[i]; + ISafeRunnable job = new ISafeRunnable() { + public void handleException(Throwable exception) { + // already being logged in Platform#run() + } + + public void run() throws Exception { + l.pathVariableChanged(pve); + } + }; + SafeRunner.run(job); + } + } + + /** + * @see org.eclipse.core.resources. + * IPathEntryVariableManager#addChangeListener(IPathEntryVariableChangeListener) + */ + public void addChangeListener(IPathEntryVariableChangeListener listener) { + fListeners.add(listener); + } + + /** + * @see org.eclipse.core.resources. + * IPathEntryVariableManager#removeChangeListener(IPathEntryVariableChangeListener) + */ + public void removeChangeListener(IPathEntryVariableChangeListener listener) { + fListeners.remove(listener); + } + + public void variablesChanged(VariableChangeEvent event) { + ICdtVariable[] added = event.getAddedVariables(); + ICdtVariable[] removed = event.getRemovedVariables(); + ICdtVariable[] changed = event.getChangedVariables(); + + if(added.length != 0){ + fireEvent(added, PathEntryVariableChangeEvent.VARIABLE_CREATED); + } + + if(removed.length != 0){ + fireEvent(removed, PathEntryVariableChangeEvent.VARIABLE_DELETED); + } + + if(changed.length != 0){ + fireEvent(changed, PathEntryVariableChangeEvent.VARIABLE_CHANGED); + } + } + + private void fireEvent(ICdtVariable vars[], int type){ + for(int i = 0; i < vars.length; i++){ + IPath path = getVariablePath(vars[i]); + if(path != null) + fireVariableChangeEvent(vars[i].getName(), path, type); + } + + } + + public void startup(){ + } + + public void shutdown(){ + } + +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PathEntryVariableManager.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PathEntryVariableManager.java index 0dd51fc88d3..4470134a195 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PathEntryVariableManager.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/PathEntryVariableManager.java @@ -44,8 +44,17 @@ public class PathEntryVariableManager implements IPathEntryVariableManager { /** * Constructor for the class. - */ - public PathEntryVariableManager() { + * + * The current manager implementation is not used any more + * Instead the CdtVarPathEntryVariableManager is used that actually wraps the CdtVariables contributed at workspace level + * + * NOTE: all PathEntryVariableManager functionality remains workable with the new + * CdtVarPathEntryVariableManager. We could either remove this class or copy the contents of the + * CdtVarPathEntryVariableManager to this class to preserve internal class name for better backward compatibility. + * + * + */ + private PathEntryVariableManager() { this.listeners = Collections.synchronizedSet(new HashSet()); this.preferences = CCorePlugin.getDefault().getPluginPreferences(); } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/ICdtVariableChangeListener.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/ICdtVariableChangeListener.java new file mode 100644 index 00000000000..79740cd4bef --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/ICdtVariableChangeListener.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2007 Intel 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: + * Intel Corporation - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.cdtvariables; + +public interface ICdtVariableChangeListener { + void variablesChanged(VariableChangeEvent event); +} diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/UserDefinedVariableSupplier.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/UserDefinedVariableSupplier.java index 94bd5902e6c..decee3d318c 100644 --- a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/UserDefinedVariableSupplier.java +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/UserDefinedVariableSupplier.java @@ -15,6 +15,11 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; @@ -28,13 +33,15 @@ import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.cdtvariables.CdtVariableException; import org.eclipse.cdt.core.cdtvariables.ICdtVariable; -import org.eclipse.cdt.core.settings.model.ICConfigurationDescription; import org.eclipse.cdt.core.settings.model.ICStorageElement; +import org.eclipse.cdt.core.settings.model.util.CDataUtil; import org.eclipse.cdt.core.settings.model.util.XmlStorageElement; import org.eclipse.cdt.internal.core.settings.model.CConfigurationSpecSettings; import org.eclipse.cdt.internal.core.settings.model.ExceptionFactory; import org.eclipse.cdt.internal.core.settings.model.IInternalCCfgInfo; +import org.eclipse.cdt.utils.cdtvariables.CdtVariableResolver; import org.eclipse.core.resources.IWorkspace; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.preferences.InstanceScope; @@ -54,10 +61,13 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { // public static final String MACROS_ELEMENT_NAME = "macros"; //$NON-NLS-1$ public static final String NODENAME = "macros"; //$NON-NLS-1$ public static final String PREFNAME_WORKSPACE = "workspace"; //$NON-NLS-1$ + static final String OLD_VARIABLE_PREFIX = "pathEntryVariable."; //$NON-NLS-1$ + private static UserDefinedVariableSupplier fInstance; private StorableCdtVariables fWorkspaceMacros; + private Set fListeners; private StorableCdtVariables getStorableMacros(int contextType, Object contextData){ StorableCdtVariables macros = null; @@ -83,7 +93,7 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { } private UserDefinedVariableSupplier(){ - + fListeners = Collections.synchronizedSet(new HashSet()); } public static UserDefinedVariableSupplier getInstance(){ @@ -127,12 +137,21 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return null; + ICdtVariable oldVar = macros.getMacro(macroName); + ICdtVariable macro = macros.createMacro(macroName,type,value); if(macros.isChanged()){ setRebuildStateForContext(contextType, contextData); macros.setChanged(false); } + if(macro != null){ + VariableChangeEvent event = createVariableChangeEvent(macro, oldVar); + if(event != null){ + notifyListeners(event); + } + } + return macro; } @@ -148,12 +167,21 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return null; + ICdtVariable oldVar = macros.getMacro(macroName); + ICdtVariable macro = macros.createMacro(macroName,type,value); if(macros.isChanged()){ setRebuildStateForContext(contextType, contextData); macros.setChanged(false); } + if(macro != null){ + VariableChangeEvent event = createVariableChangeEvent(macro, oldVar); + if(event != null){ + notifyListeners(event); + } + } + return macro; } @@ -168,12 +196,20 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return null; + ICdtVariable oldVar = macros.getMacro(macroName); + ICdtVariable macro = macros.createMacro(copy); if(macros.isChanged()){ setRebuildStateForContext(contextType, contextData); macros.setChanged(false); } + if(macro != null){ + VariableChangeEvent event = createVariableChangeEvent(macro, oldVar); + if(event != null){ + notifyListeners(event); + } + } return macro; } @@ -182,8 +218,15 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return null; ICdtVariable macro = macros.deleteMacro(name); - if(macro != null) + if(macro != null){ setRebuildStateForContext(contextType, contextData); + + VariableChangeEvent event = createVariableChangeEvent(null, macro); + if(event != null){ + notifyListeners(event); + } + + } return macro; } @@ -193,8 +236,16 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return; + ICdtVariable[] oldVars = macros.getMacros(); + if(macros.deleteAll()) setRebuildStateForContext(contextType, contextData); + + VariableChangeEvent event = createVariableChangeEvent(null, oldVars); + if(event != null){ + notifyListeners(event); + } + } public void setMacros(ICdtVariable m[], int contextType, Object contextData){ @@ -202,12 +253,184 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { if(macros == null) return; + ICdtVariable[] oldVars = macros.getMacros(); + macros.setMacros(m); if(macros.isChanged()){ setRebuildStateForContext(contextType, contextData); macros.setChanged(false); + + VariableChangeEvent event = createVariableChangeEvent(m, oldVars); + if(event != null){ + notifyListeners(event); + } + } } + + private static class VarKey { + private ICdtVariable fVar; + private boolean fNameOnly; + + VarKey(ICdtVariable var, boolean nameOnly){ + fVar = var; + fNameOnly = nameOnly; + } + + public ICdtVariable getVariable(){ + return fVar; + } + + public boolean equals(Object obj) { + if(obj == this) + return true; + + if(!(obj instanceof VarKey)) + return false; + + VarKey other = (VarKey)obj; + + ICdtVariable otherVar = other.fVar; + + if(fVar == otherVar) + return true; + + if(!CDataUtil.objectsEqual(fVar.getName(), otherVar.getName())) + return false; + + if(fNameOnly) + return true; + + if(fVar.getValueType() != otherVar.getValueType()) + return false; + + if(CdtVariableResolver.isStringListVariable(fVar.getValueType())){ + try { + if(!Arrays.equals(fVar.getStringListValue(), otherVar.getStringListValue())) + return false; + } catch (CdtVariableException e) { + CCorePlugin.log(e); + } + } else { + try { + if(!CDataUtil.objectsEqual(fVar.getStringValue(), otherVar.getStringValue())) + return false; + } catch (CdtVariableException e) { + CCorePlugin.log(e); + } + } + + return true; + } + + public int hashCode() { + int code = 51; + + String name = fVar.getName(); + if(name != null) + code += name.hashCode(); + + if(fNameOnly) + return code; + + code += fVar.getValueType(); + if(CdtVariableResolver.isStringListVariable(fVar.getValueType())){ + try { + String[] value = fVar.getStringListValue(); + if(value != null){ + for(int i = 0; i < value.length; i++){ + code += value[i].hashCode(); + } + } + } catch (CdtVariableException e) { + CCorePlugin.log(e); + } + } else { + try { + String value =fVar.getStringValue(); + if(value != null){ + code += value.hashCode(); + } + } catch (CdtVariableException e) { + CCorePlugin.log(e); + } + } + + return code; + } + + } + + static VariableChangeEvent createVariableChangeEvent(ICdtVariable newVar, ICdtVariable oldVar){ + ICdtVariable newVars[] = newVar != null ? new ICdtVariable[]{newVar} : null; + ICdtVariable oldVars[] = oldVar != null ? new ICdtVariable[]{oldVar} : null; + + return createVariableChangeEvent(newVars, oldVars); + } + + static ICdtVariable[] varsFromKeySet(Set set){ + ICdtVariable vars[] = new ICdtVariable[set.size()]; + int i = 0; + for(Iterator iter = set.iterator(); iter.hasNext(); i++){ + VarKey key = (VarKey)iter.next(); + vars[i] = key.getVariable(); + } + + return vars; + } + static VariableChangeEvent createVariableChangeEvent(ICdtVariable[] newVars, ICdtVariable[] oldVars){ + ICdtVariable[] addedVars = null, removedVars = null, changedVars = null; + + if(oldVars == null || oldVars.length == 0){ + if(newVars != null && newVars.length != 0) + addedVars = (ICdtVariable[])newVars.clone() ; + } else if(newVars == null || newVars.length == 0){ + removedVars = (ICdtVariable[])oldVars.clone(); + } else { + HashSet newSet = new HashSet(newVars.length); + HashSet oldSet = new HashSet(oldVars.length); + + for(int i = 0; i < newVars.length; i++){ + newSet.add(new VarKey(newVars[i], true)); + } + + for(int i = 0; i < oldVars.length; i++){ + oldSet.add(new VarKey(oldVars[i], true)); + } + + HashSet newSetCopy = (HashSet)newSet.clone(); + + newSet.removeAll(oldSet); + oldSet.removeAll(newSetCopy); + + if(newSet.size() != 0){ + addedVars = varsFromKeySet(newSet); + } + + if(oldSet.size() != 0){ + removedVars = varsFromKeySet(oldSet); + } + + newSetCopy.removeAll(newSet); + + HashSet modifiedSet = new HashSet(newSetCopy.size()); + for(Iterator iter = newSetCopy.iterator(); iter.hasNext();){ + VarKey key = (VarKey)iter.next(); + modifiedSet.add(new VarKey(key.getVariable(), false)); + } + + for(int i = 0; i < oldVars.length; i++){ + modifiedSet.remove(new VarKey(oldVars[i], false)); + } + + if(modifiedSet.size() != 0) + changedVars = varsFromKeySet(modifiedSet); + } + + if(addedVars != null || removedVars != null || changedVars != null) + return new VariableChangeEvent(addedVars, removedVars, changedVars); + return null; + } /* * @@ -228,8 +451,20 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { } public void setWorkspaceVariables(StorableCdtVariables vars) throws CoreException{ + StorableCdtVariables old = getStorableMacros(ICoreVariableContextInfo.CONTEXT_WORKSPACE, null); + ICdtVariable[] oldVars = null; + if(old != null) + oldVars = old.getMacros(); + + ICdtVariable[] newVars = vars.getMacros(); + fWorkspaceMacros = new StorableCdtVariables(vars, false); + VariableChangeEvent event = createVariableChangeEvent(newVars, oldVars); + if(event != null){ + notifyListeners(event); + } + storeWorkspaceVariables(true); } @@ -246,12 +481,38 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { * loads the stored workspace macros */ protected StorableCdtVariables loadWorkspaceMacros(){ + StorableCdtVariables macros = loadNewStileWorkspaceMacros(); + + //now load PathEntry Variables from preferences + + + return macros; + } + + protected void loadPathEntryVariables(StorableCdtVariables vars){ + org.eclipse.core.runtime.Preferences prefs = CCorePlugin.getDefault().getPluginPreferences(); + String[] names = prefs.propertyNames(); + for(int i = 0; i < names.length; i++){ + String name = names[i]; + if (name.startsWith(OLD_VARIABLE_PREFIX)) { + String value = prefs.getString(name); + prefs.setToDefault(name); + if(value.length() != 0){ + name = name.substring(OLD_VARIABLE_PREFIX.length()); + vars.createMacro(name, ICdtVariable.VALUE_PATH_ANY, value); + } + } + } + } + + protected StorableCdtVariables loadNewStileWorkspaceMacros(){ InputStream stream = loadInputStream(getWorkspaceNode(),PREFNAME_WORKSPACE); if(stream == null) return new StorableCdtVariables(false); return loadMacrosFromStream(stream); } + /* * stores the given macros */ @@ -396,4 +657,19 @@ public class UserDefinedVariableSupplier extends CoreMacroSupplierBase { */ } + public void addListener(ICdtVariableChangeListener listener){ + fListeners.add(listener); + } + + public void removeListener(ICdtVariableChangeListener listener){ + fListeners.remove(listener); + } + + private void notifyListeners(VariableChangeEvent event){ + ICdtVariableChangeListener[] listeners = (ICdtVariableChangeListener[])fListeners.toArray(new ICdtVariableChangeListener[fListeners.size()]); + for(int i = 0; i < listeners.length; i++){ + listeners[i].variablesChanged(event); + } + } + } diff --git a/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/VariableChangeEvent.java b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/VariableChangeEvent.java new file mode 100644 index 00000000000..e42425b2ada --- /dev/null +++ b/core/org.eclipse.cdt.core/src/org/eclipse/cdt/internal/core/cdtvariables/VariableChangeEvent.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2007 Intel 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: + * Intel Corporation - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.core.cdtvariables; + +import org.eclipse.cdt.core.cdtvariables.ICdtVariable; + +public class VariableChangeEvent { + private static final ICdtVariable[] EMPTY_VAR_ARRAY = new ICdtVariable[0]; + + private ICdtVariable[] fAddedVars, fRemovedVars, fChangedVars; + + VariableChangeEvent(ICdtVariable[] addedVars, ICdtVariable[] removedVars, ICdtVariable[] changedVars){ + fAddedVars = addedVars != null ? (ICdtVariable[])addedVars.clone() : null; + fRemovedVars = removedVars != null ? (ICdtVariable[])removedVars.clone() : null; + fChangedVars = changedVars != null ? (ICdtVariable[])changedVars.clone() : null; + } + public ICdtVariable[] getAddedVariables(){ + return fAddedVars != null ? (ICdtVariable[])fAddedVars.clone() : EMPTY_VAR_ARRAY; + } + + public ICdtVariable[] getRemovedVariables(){ + return fRemovedVars != null ? (ICdtVariable[])fRemovedVars.clone() : EMPTY_VAR_ARRAY; + } + + public ICdtVariable[] getChangedVariables(){ + return fChangedVars != null ? (ICdtVariable[])fChangedVars.clone() : EMPTY_VAR_ARRAY; + } + +}