diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/LanguageMappingConfiguration.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/LanguageMappingConfiguration.java deleted file mode 100644 index 55fdf694e12..00000000000 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/LanguageMappingConfiguration.java +++ /dev/null @@ -1,78 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2007 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.cdt.core.language; - -import java.util.Collections; -import java.util.Map; -import java.util.TreeMap; - -/** - * Provides programmatic access to language mappings for a project. - * - *

- * EXPERIMENTAL. This class or interface has been added as - * part of a work in progress. There is no guarantee that this API will work or - * that it will remain the same. Please do not use this API without consulting - * with the CDT team. - *

- * - * @since 4.0 - */ -public class LanguageMappingConfiguration { - - /** - * Project-wide mappings. - */ - private Map fProjectMappings; - - /** - * Creates a new LanguageMappingConfiguration with no - * mappings defined. - */ - public LanguageMappingConfiguration() { - fProjectMappings = new TreeMap(); - } - - /** - * Returns a read-only copy of the project-wide language mappings. - * @return a read-only copy of the project-wide language mappings. - */ - public Map getProjectMappings() { - return Collections.unmodifiableMap(fProjectMappings); - } - - /** - * Replaces the existing language mappings with the given - * mappings. The given mappings should be between content type ids - * (String) and language ids (String) - * @param projectMappings - */ - public void setProjectMappings(Map/**/ projectMappings) { - fProjectMappings = new TreeMap(projectMappings); - } - - /** - * Maps a content type id to a language id. - * @param contentType - * @param language - */ - public void addProjectMapping(String contentType, String language) { - fProjectMappings.put(contentType, language); - } - - /** - * Removes the given content type mapping (if it exists). - * @param contentType - */ - public void removeProjectMapping(String contentType) { - fProjectMappings.remove(contentType); - } -} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/ProjectLanguageConfiguration.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/ProjectLanguageConfiguration.java new file mode 100644 index 00000000000..f1c819efd4c --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/ProjectLanguageConfiguration.java @@ -0,0 +1,166 @@ +/******************************************************************************* + * Copyright (c) 2007 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.cdt.core.language; + +import java.util.Collections; +import java.util.Map; +import java.util.TreeMap; + +import org.eclipse.core.resources.IFile; + +/** + * Provides programmatic access to language mappings for a project. + * + *

+ * EXPERIMENTAL. This class or interface has been added as + * part of a work in progress. There is no guarantee that this API will work or + * that it will remain the same. Please do not use this API without consulting + * with the CDT team. + *

+ * + * @since 4.0 + */ +public class ProjectLanguageConfiguration { + + /** + * Project-wide content type mappings. + */ + private Map fContentTypeMappings; + + /** + * Per-file mappings. + */ + private Map fFileMappings; + + /** + * Creates a new ProjectLanguageConfiguration with no + * language mappings defined. + */ + public ProjectLanguageConfiguration() { + fContentTypeMappings = new TreeMap(); + fFileMappings = new TreeMap(); + } + + /** + * Returns a read-only copy of the project-wide content-type-specific language mappings. + * @return a read-only copy of the project-wide content-type-specific language mappings. + */ + public Map getContentTypeMappings() { + return Collections.unmodifiableMap(fContentTypeMappings); + } + + /** + * Returns the language id that is mapped to the given content type. + * @param contentTypeId + * @return + */ + public String getLanguageForContentType(String contentTypeId) { + return (String) fContentTypeMappings.get(contentTypeId); + } + + /** + * Returns the language id that is mapped to the given file. + * @param file + * @return + */ + public String getLanguageForFile(IFile file) { + return (String) fFileMappings.get(file.getProjectRelativePath().toPortableString()); + } + + /** + * Returns the language id that is mapped to the given file. + * @param path + * @return + */ + public String getLanguageForFile(String path) { + return (String) fFileMappings.get(path); + } + + + /** + * Replaces the existing content-type-specific language mappings with the given + * mappings. The given mappings should be between content type ids + * (String) and language ids (String) + * @param mappings + */ + public void setContentTypeMappings(Map/**/ mappings) { + fContentTypeMappings = new TreeMap(mappings); + } + + /** + * Maps a content type id to a language id. + * @param contentType + * @param language + */ + public void addContentTypeMapping(String contentType, String language) { + fContentTypeMappings.put(contentType, language); + } + + /** + * Removes the given content type mapping (if it exists). + * @param contentType + */ + public void removeContentTypeMapping(String contentType) { + fContentTypeMappings.remove(contentType); + } + + /** + * Sets the language for a file. + * @param file + * @param language + */ + public void addFileMapping(IFile file, String language) { + fFileMappings.put(file.getProjectRelativePath().toPortableString(), language); + } + + /** + * Sets the language for a file. + * @param filePath + * @param language + */ + public void addFileMapping(String filePath, String language) { + fFileMappings.put(filePath, language); + } + + /** + * Removes the given file mapping (if it exists). + * @param file + */ + public void removeFileMapping(IFile file) { + fFileMappings.remove(file.getProjectRelativePath().toPortableString()); + } + + /** + * Removes the given file mapping (if it exists). + * @param filePath + */ + public void removeFileMapping(String filePath) { + fFileMappings.remove(filePath); + } + + /** + * Returns a read-only copy of the file-specific language mappings. + * @return a read-only copy of the file-specific language mappings. + */ + public Map getFileMappings() { + return Collections.unmodifiableMap(fFileMappings); + } + + /** + * Replaces the existing file-specific language mappings with the given + * mappings. The given mappings should be between full paths + * (String) and language ids (String) + * @param projectMappings + */ + public void setFileMappings(Map/**/ fileMappings) { + fContentTypeMappings = new TreeMap(fileMappings); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/WorkspaceLanguageConfiguration.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/WorkspaceLanguageConfiguration.java new file mode 100644 index 00000000000..7c21925c14b --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/language/WorkspaceLanguageConfiguration.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2007 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.cdt.core.language; + +import java.util.Collections; +import java.util.Map; +import java.util.TreeMap; + +/** + * Provides programmatic access to language mappings for the workspace. + */ +public class WorkspaceLanguageConfiguration { + + /** + * Workspace-wide content type mappings. + */ + private Map fMappings; + + /** + * Creates a new WorkspaceLanguageConfiguration with no + * language mappings defined. + */ + public WorkspaceLanguageConfiguration() { + fMappings = new TreeMap(); + } + + /** + * Maps a content type id to a language id. + * @param contentType + * @param language + */ + public void addWorkspaceMapping(String contentType, String language) { + fMappings.put(contentType, language); + } + + /** + * Removes the given content type mapping (if it exists). + * @param contentType + */ + public void removeWorkspaceMapping(String contentType) { + fMappings.remove(contentType); + } + + /** + * Replaces the existing language mappings with the given + * mappings. The given mappings should be between content type ids + * (String) and language ids (String) + * @param projectMappings + */ + public void setWorkspaceMappings(Map/**/ mappings) { + fMappings = new TreeMap(mappings); + } + + /** + * Returns a read-only copy of the workspace-wide language mappings. + * @return a read-only copy of the workspace-wide language mappings. + */ + public Map getWorkspaceMappings() { + return Collections.unmodifiableMap(fMappings); + } + + /** + * Returns the language id that is mapped to the given content type. + * @param contentTypeId + * @return + */ + public String getLanguageForContentType(String contentTypeId) { + return (String) fMappings.get(contentTypeId); + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeEvent.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeEvent.java new file mode 100644 index 00000000000..b49cfc30dec --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeEvent.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2007 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.cdt.core.model; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.content.IContentType; + +/** + * Contains the details of changes that occurred as a result of modifying + * language mappings. + * + * @author crecoskie + * @since 4.0 + */ +public interface ILanguageMappingChangeEvent { + + public static final int TYPE_WORKSPACE = 0; + public static final int TYPE_PROJECT = 1; + public static final int TYPE_FILE = 2; + + /** + * Returns an IFile corresponding to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * @return an IFile corresponding to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * + * @since 4.0 + */ + public IFile getFile(); + + /** + * Returns a String corresponding to the full path to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * In order to obtain the full context for the file it may be required that you also call getProject(), + * as it is possible that this file may not live inside the workspace. + * + * @return a String corresponding to the full path to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * @see getProject() + * + * @since 4.0 + */ + public String getFilename(); + + /** + * Returns an IPath corresponding to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * @return an IPath corresponding to the file for which settings have changed if this + * event's type is TYPE_FILE, or null otherwise. + * + * In order to obtain the full context for the file it may be required that you also call getProject(), + * as it is possible that this file may not live inside the workspace. + * + * @see getProject() + * + * @since 4.0 + */ + public IPath getPath(); + + /** + * Returns an IProject corresponding to the project for which settings have changed if this + * event's type is TYPE_PROJECT or TYPE_FILE, or null otherwise. + * @return an IProject corresponding to the project for which settings have changed if this + * event's type is TYPE_PROJECT or TYPE_FILE, or null otherwise. + * + * @since 4.0 + */ + public IProject getProject(); + + /** + * Returns the type of even being reported. + * @return the type of even being reported + * @see TYPE_WORKSPACE + * @see TYPE_PROJECT + * @see TYPE_FILE + * + * @since 4.0 + */ + public int getType(); + + /** + * Returns an array of IContentTypes for which mappings have been changed, or an empty collection + * if there are no affected content types. Since there currently should be no change event unless + * a content type has changed, this should always contain at least one content type, but clients + * should theoretically be prepared to handle an empty collection. + * @return the content types for which mappings have been changed. + */ + public IContentType[] getAffectedContentTypes(); +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeListener.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeListener.java new file mode 100644 index 00000000000..cd3061a3c9e --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguageMappingChangeListener.java @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2007 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.cdt.core.model; + +/** + * Listens to changes in language mappings. + * + * @author crecoskie + * @ since 4.0 + */ +public interface ILanguageMappingChangeListener { + + /** + * Indicates that language mappings have been changed. + * @param event + */ + public void handleLanguageMappingChangeEvent(ILanguageMappingChangeEvent event); + +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageManager.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageManager.java index 633ee9245a8..e75e28704d5 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageManager.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageManager.java @@ -8,6 +8,8 @@ * Contributors: * QNX - Initial API and implementation * Markus Schorn (Wind River Systems) + * IBM Corporation + * - Language managment feature (see Bugzilla 151850) *******************************************************************************/ package org.eclipse.cdt.core.model; @@ -22,7 +24,8 @@ import java.util.Set; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.dom.ILinkage; -import org.eclipse.cdt.core.language.LanguageMappingConfiguration; +import org.eclipse.cdt.core.language.ProjectLanguageConfiguration; +import org.eclipse.cdt.core.language.WorkspaceLanguageConfiguration; import org.eclipse.cdt.internal.core.CContentTypes; import org.eclipse.cdt.internal.core.language.LanguageMappingStore; import org.eclipse.cdt.internal.core.model.LanguageDescriptor; @@ -36,6 +39,7 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.ISafeRunnable; +import org.eclipse.core.runtime.ListenerList; import org.eclipse.core.runtime.Platform; import org.eclipse.core.runtime.SafeRunner; import org.eclipse.core.runtime.content.IContentType; @@ -62,6 +66,7 @@ public class LanguageManager { private boolean fIsFullyCached; private HashMap fIdToLanguageDescriptorCache;//= new HashMap(); private HashMap fContentTypeToDescriptorListCache; + private ListenerList fLanguageChangeListeners = new ListenerList(ListenerList.IDENTITY); public static LanguageManager getInstance() { if (instance == null) @@ -320,6 +325,10 @@ public class LanguageManager { return result[0]; } + /** + * Returns all of the languages registered with the Platform. + * @return all of the languages registered with the Platform. + */ public ILanguage[] getRegisteredLanguages() { cacheAllLanguages(); ILanguage[] languages = new ILanguage[fLanguageCache.size()]; @@ -357,25 +366,68 @@ public class LanguageManager { fIsFullyCached = true; } - public LanguageMappingConfiguration getLanguageMappingConfiguration(IProject project) throws CoreException { - LanguageMappingConfiguration mappings = (LanguageMappingConfiguration) fLanguageConfigurationCache.get(project); - if (mappings != null) { - return mappings; - } - - LanguageMappingStore store = new LanguageMappingStore(project); - mappings = store.decodeMappings(); - fLanguageConfigurationCache.put(project, mappings); - return mappings; - } - - public void storeLanguageMappingConfiguration(IProject project) throws CoreException { - LanguageMappingConfiguration mappings = (LanguageMappingConfiguration) fLanguageConfigurationCache.get(project); - LanguageMappingStore store = new LanguageMappingStore(project); - store.storeMappings(mappings); + /** + * Returns the language configuration for the workspace. + * @return + * @throws CoreException + * @since 4.0 + */ + public WorkspaceLanguageConfiguration getWorkspaceLanguageConfiguration() throws CoreException { + // TODO: Implement this. + return new WorkspaceLanguageConfiguration(); } /** + * Saves the workspace language configuration to persistent storage and notifies + * all ILanguageMappingChangeListeners of changes. + * @param affectedContentTypes + * @throws CoreException + * @since 4.0 + */ + public void storeWorkspaceLanguageConfiguration(IContentType[] affectedContentTypes) throws CoreException { + // TODO: Implement this. + } + + /** + * Returns the language configuration for the given project. + * @param project + * @return + * @throws CoreException + * @since 4.0 + */ + public ProjectLanguageConfiguration getLanguageConfiguration(IProject project) throws CoreException { + synchronized (this) { + ProjectLanguageConfiguration mappings = (ProjectLanguageConfiguration) fLanguageConfigurationCache.get(project); + if (mappings != null) { + return mappings; + } + + LanguageMappingStore store = new LanguageMappingStore(project); + mappings = store.decodeMappings(); + fLanguageConfigurationCache.put(project, mappings); + return mappings; + } + } + + /** + * Saves the language configuration for the given project to persistent + * storage and notifies all ILanguageMappingChangeListeners + * of changes. + * @param project + * @param affectedContentTypes + * @throws CoreException + * @since 4.0 + */ + public void storeLanguageMappingConfiguration(IProject project, IContentType[] affectedContentTypes) throws CoreException { + ProjectLanguageConfiguration mappings = (ProjectLanguageConfiguration) fLanguageConfigurationCache.get(project); + LanguageMappingStore store = new LanguageMappingStore(project); + store.storeMappings(mappings); + + // TODO: Notify listeners that the language mappings have changed. + } + + /** + * Returns an ILanguage representing the language to be used for the given file. * @since 4.0 * @return an ILanguage representing the language to be used for the given file * @param fullPathToFile the full path to the file for which the language is requested @@ -396,37 +448,25 @@ public class LanguageManager { String contentTypeID = contentType.getId(); - // TODO: other mappings would go here - - // Project-level mappings - LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project); - if (mappings != null) { - String id = (String) mappings.getProjectMappings().get(contentTypeID); - if (id != null) { - return getLanguage(id); - } - } - - // Content type mappings - return getLanguageForContentTypeID(contentTypeID); + return computeLanguage(project, fullPathToFile, contentTypeID); } /** - * @since 4.0 + * Returns an ILanguage representing the language to be used for the given file. * @return an ILanguage representing the language to be used for the given file * @param pathToFile the path to the file for which the language is requested. * The path can be either workspace or project relative. * @param project the project that this file should be parsed in context of. This field is optional and may * be set to null. If the project is null then this method tries to determine the project context via workspace APIs. * @throws CoreException - * * TODO: implement other mapping levels besides project level and content type level + * @since 4.0 */ public ILanguage getLanguageForFile(IPath pathToFile, IProject project) throws CoreException { return getLanguageForFile(pathToFile, project, null); } /** - * @since 4.0 + * Returns an ILanguage representing the language to be used for the given file. * @return an ILanguage representing the language to be used for the given file * @param pathToFile the path to the file for which the language is requested. * The path can be either workspace or project relative. @@ -434,7 +474,7 @@ public class LanguageManager { * be set to null. If the project is null then this method tries to determine the project context via workspace APIs. * @param contentTypeID id of the content type, may be null. * @throws CoreException - * * TODO: implement other mapping levels besides project level and content type level + * @since 4.0 */ public ILanguage getLanguageForFile(IPath pathToFile, IProject project, String contentTypeID) throws CoreException { if (project == null) { @@ -452,27 +492,15 @@ public class LanguageManager { contentTypeID= ct.getId(); } - // TODO: other mappings would go here - - // Project-level mappings - LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project); - if (mappings != null) { - String id = (String) mappings.getProjectMappings().get(contentTypeID); - if (id != null) { - return getLanguage(id); - } - } - - // Content type mappings - return getLanguageForContentTypeID(contentTypeID); + return computeLanguage(project, pathToFile.toPortableString(), contentTypeID); } /** - * @since 4.0 + * Returns an ILanguage representing the language to be used for the given file. * @return an ILanguage representing the language to be used for the given file * @param file the file for which the language is requested * @throws CoreException - * TODO: implement other mapping levels besides project level and content type level + * @since 4.0 */ public ILanguage getLanguageForFile(IFile file) throws CoreException { return getLanguageForFile(file, null); @@ -480,12 +508,12 @@ public class LanguageManager { /** - * @since 4.0 + * Returns an ILanguage representing the language to be used for the given file. * @return an ILanguage representing the language to be used for the given file * @param file the file for which the language is requested * @param contentTypeID id of the content type, may be null. * @throws CoreException - * TODO: implement other mapping levels besides project level and content type level + * @since 4.0 */ public ILanguage getLanguageForFile(IFile file, String contentTypeId) throws CoreException { IProject project = file.getProject(); @@ -499,19 +527,89 @@ public class LanguageManager { contentTypeId= contentType.getId(); } - // TODO: other mappings would go here - - // Project-level mappings - LanguageMappingConfiguration mappings = getLanguageMappingConfiguration(project); + return computeLanguage(project, file.getProjectRelativePath().toPortableString(), contentTypeId); + } + + private ILanguage computeLanguage(IProject project, String filePath, String contentTypeId) throws CoreException { + ProjectLanguageConfiguration mappings = getLanguageConfiguration(project); if (mappings != null) { - String id = (String) mappings.getProjectMappings().get(contentTypeId); + // File-level mappings + String id = mappings.getLanguageForFile(filePath); + if (id != null) { + return getLanguage(id); + } + + // Project-level mappings + id = mappings.getLanguageForContentType(contentTypeId); if (id != null) { return getLanguage(id); } } + // Workspace mappings + WorkspaceLanguageConfiguration workspaceMappings = getWorkspaceLanguageConfiguration(); + String id = workspaceMappings.getLanguageForContentType(contentTypeId); + if (id != null) { + return getLanguage(id); + } + // Content type mappings return getLanguageForContentTypeID(contentTypeId); } + + /** + * Adds a listener that will be notified of changes in language mappings. + * + * @param listener the ILanguageMappingChangeListener to add + */ + public void registerLanguageChangeListener(ILanguageMappingChangeListener listener) { + fLanguageChangeListeners.add(listener); + } + + /** + * Removes a language mapping change listener. + * + * @param listener the ILanguageMappingChangeListener to remove. + */ + public void unregisterLanguageChangeListener(ILanguageMappingChangeListener listener) { + fLanguageChangeListeners.remove(listener); + } + + /** + * Notifies all language mappings change listeners of a change in the mappings. + * + * @param event the ILanguageMappingsChange event to be broadcast. + */ + public void notifyLanguageChangeListeners(ILanguageMappingChangeEvent event) { + Object[] listeners = fLanguageChangeListeners.getListeners(); + + for (int i= 0; i < listeners.length; i++) { + ILanguageMappingChangeListener listener = (ILanguageMappingChangeListener) listeners[i]; + listener.handleLanguageMappingChangeEvent(event); + } + } + /** + * Saves the language configuration for the given file to persistent + * storage and notifies all ILanguageMappingChangeListeners + * of changes. + * @param file + * @throws CoreException + * @since 4.0 + */ + public void storeLanguageMappingConfiguration(IFile file) throws CoreException { + IProject project = file.getProject(); + synchronized (this) { + ProjectLanguageConfiguration mappings = (ProjectLanguageConfiguration) fLanguageConfigurationCache.get(project); + LanguageMappingStore store = new LanguageMappingStore(project); + store.storeMappings(mappings); + } + + // Notify listeners that the language mappings have changed. + LanguageMappingChangeEvent event = new LanguageMappingChangeEvent(); + event.setType(LanguageMappingChangeEvent.TYPE_FILE); + event.setProject(project); + event.setFile(file); + notifyLanguageChangeListeners(event); + } } diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageMappingChangeEvent.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageMappingChangeEvent.java new file mode 100644 index 00000000000..ebd057a1fce --- /dev/null +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/LanguageMappingChangeEvent.java @@ -0,0 +1,188 @@ +/******************************************************************************* + * Copyright (c) 2007 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.cdt.core.model; + +import java.net.URI; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.content.IContentType; + +/** + * A minimal implementation of ILanguageMappingsChangeEvent. + * + * @author crecoskie + */ +public class LanguageMappingChangeEvent implements ILanguageMappingChangeEvent { + + private int fType; + private IProject fProject; + private IFile fFile; + private IPath fPath; + private String fFileName; + private IContentType[] fContentTypes; + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getAffectedContentTypes() + */ + public IContentType[] getAffectedContentTypes() { + return fContentTypes; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getFile() + */ + public IFile getFile() { + return fFile; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getFilename() + */ + public String getFilename() { + + // if the filename has been set, use it. otherwise get the path from + // either the IFile or the IPath if we have one + + if(fFileName != null) + return fFileName; + else { + if(fFile != null) + { + IPath location = fFile.getLocation(); + if(location != null) + return location.toString(); + else { + // use the URI if there is one + URI uri = fFile.getLocationURI(); + + if(uri != null) + return uri.toString(); + } + + } + else { // no file, use path + if(fPath != null) + return fPath.toString(); + + } + } + + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getPath() + */ + public IPath getPath() { + + if(fPath != null) + return fPath; + + else { // try to get the path from the file if we have one + if(fFile != null) + { + IPath location = fFile.getLocation(); + return location; + } + } + + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getProject() + */ + public IProject getProject() { + + if(fProject != null) + return fProject; + + else { // try to get the project from the file if we have one + + if(fFile != null) + return fFile.getProject(); + + } + + return null; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.model.ILanguageMappingsChangeEvent#getType() + */ + public int getType() { + return fType; + } + + /** + * Sets the associated IContentTypes for this event. The provided array will be returned + * subsequently by getAffectedContentTypes. + * + * @param affectedContentTypes + */ + public void setAffectedContentTypes(IContentType[] affectedContentTypes) { + fContentTypes = affectedContentTypes; + } + + /** + * Sets the associated IFile for this event. This file will be returned subsequently + * by getFile(). + * + * @param file the IFile to set + */ + public void setFile(IFile file) { + fFile = file; + } + + /** + * Sets the associated String filename for this event. This filename will be returned subsequently + * by getFileName(). + * + * @param fileName the fFileName to set + */ + public void setFileName(String fileName) { + fFileName = fileName; + } + + /** + * Sets the associated IPath for this event. This path will be returned subsequently + * by getPath(). + * + * @param path the IPath to set + */ + public void setPath(IPath path) { + fPath = path; + } + + /** + * Sets the associated project for this event. This project will be returned subsequently + * by getProject(). + * + * @param project the IProject to set + */ + public void setProject(IProject project) { + fProject = project; + } + + /** + * Sets the type of this event. This type will be returned by getType(). + * + * @param type the type to set + * @see ILanguageMappingChangeEvent.TYPE_WORKSPACE + * @see ILanguageMappingChangeEvent.TYPE_PROJECT + * @see ILanguageMappingChangeEvent.TYPE_FILE + */ + public void setType(int type) { + fType = type; + } +} diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java index 6ac7152bfdd..88ed2147ba2 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/internal/core/language/LanguageMappingStore.java @@ -17,7 +17,7 @@ import java.util.Map.Entry; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ICDescriptor; -import org.eclipse.cdt.core.language.LanguageMappingConfiguration; +import org.eclipse.cdt.core.language.ProjectLanguageConfiguration; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.w3c.dom.Document; @@ -42,14 +42,14 @@ public class LanguageMappingStore { fProject = project; } - public LanguageMappingConfiguration decodeMappings() throws CoreException { - LanguageMappingConfiguration config = new LanguageMappingConfiguration(); + public ProjectLanguageConfiguration decodeMappings() throws CoreException { + ProjectLanguageConfiguration config = new ProjectLanguageConfiguration(); ICDescriptor descriptor = getProjectDescription(); Element rootElement = descriptor.getProjectData(LANGUAGE_MAPPING_ID); if (rootElement == null) { return config; } - config.setProjectMappings(decodeProjectMappings(rootElement)); + config.setContentTypeMappings(decodeProjectMappings(rootElement)); return config; } @@ -73,11 +73,11 @@ public class LanguageMappingStore { return decodedMappings; } - public void storeMappings(LanguageMappingConfiguration config) throws CoreException { + public void storeMappings(ProjectLanguageConfiguration config) throws CoreException { ICDescriptor descriptor = getProjectDescription(); Element rootElement = descriptor.getProjectData(LANGUAGE_MAPPING_ID); clearChildren(rootElement); - addProjectMappings(config.getProjectMappings(), rootElement); + addProjectMappings(config.getContentTypeMappings(), rootElement); descriptor.saveProjectData(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/language/ProjectLanguageMappingPropertyPage.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/language/ProjectLanguageMappingPropertyPage.java index 4ea1ed6aea5..89f4e436bf1 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/language/ProjectLanguageMappingPropertyPage.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/language/ProjectLanguageMappingPropertyPage.java @@ -17,6 +17,7 @@ import java.util.Map.Entry; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.content.IContentType; import org.eclipse.core.runtime.content.IContentTypeManager; import org.eclipse.jface.layout.TableColumnLayout; import org.eclipse.jface.preference.PreferencePage; @@ -36,7 +37,7 @@ import org.eclipse.swt.widgets.TableItem; import org.eclipse.ui.dialogs.PropertyPage; import org.eclipse.cdt.core.CCorePlugin; -import org.eclipse.cdt.core.language.LanguageMappingConfiguration; +import org.eclipse.cdt.core.language.ProjectLanguageConfiguration; import org.eclipse.cdt.core.model.LanguageManager; @@ -45,7 +46,7 @@ import org.eclipse.cdt.internal.ui.preferences.PreferencesMessages; public class ProjectLanguageMappingPropertyPage extends PropertyPage { private static final int MINIMUM_COLUMN_WIDTH = 150; - private LanguageMappingConfiguration fMappings; + private ProjectLanguageConfiguration fMappings; private Table fTable; private HashMap fContentTypeNamesToIDsMap; @@ -118,14 +119,14 @@ public class ProjectLanguageMappingPropertyPage extends PropertyPage { public void handleEvent(Event event) { ContentTypeMappingDialog dialog = new ContentTypeMappingDialog( getShell()); - dialog.setContentTypeFilter(fMappings.getProjectMappings() + dialog.setContentTypeFilter(fMappings.getContentTypeMappings() .keySet()); dialog.setBlockOnOpen(true); if (dialog.open() == Window.OK) { String contentType = dialog.getContentTypeID(); String language = dialog.getLanguageID(); - fMappings.addProjectMapping(contentType, language); + fMappings.addContentTypeMapping(contentType, language); refreshMappings(); } } @@ -141,7 +142,7 @@ public class ProjectLanguageMappingPropertyPage extends PropertyPage { for (int i = 0; i < selection.length; i++) { fMappings - .removeProjectMapping((String) fContentTypeNamesToIDsMap + .removeContentTypeMapping((String) fContentTypeNamesToIDsMap .get(selection[i].getText(0))); } @@ -155,7 +156,7 @@ public class ProjectLanguageMappingPropertyPage extends PropertyPage { private void refreshMappings() { fTable.removeAll(); - Iterator mappings = fMappings.getProjectMappings().entrySet() + Iterator mappings = fMappings.getContentTypeMappings().entrySet() .iterator(); IContentTypeManager contentTypeManager = Platform @@ -179,20 +180,21 @@ public class ProjectLanguageMappingPropertyPage extends PropertyPage { private void fetchMappings() { try { fMappings = LanguageManager.getInstance() - .getLanguageMappingConfiguration(getProject()); + .getLanguageConfiguration(getProject()); } catch (CoreException e) { CCorePlugin.log(e); } } protected void performDefaults() { - fMappings = new LanguageMappingConfiguration(); + fMappings = new ProjectLanguageConfiguration(); } public boolean performOk() { try { + IContentType[] affectedContentTypes = null; LanguageManager.getInstance().storeLanguageMappingConfiguration( - getProject()); + getProject(), affectedContentTypes); return true; } catch (CoreException e) { CCorePlugin.log(e);