Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Valenta2006-05-18 20:01:37 +0000
committerMichael Valenta2006-05-18 20:01:37 +0000
commit187c7c9f70daf88d40b6873b9cc0e5a22efe5dc3 (patch)
tree3a64f9961ee3c7b5e11122c7a67215e89c966983 /examples
parenta8f0cd3295f39262a3a2ec0b92746a5e7dd79ff5 (diff)
downloadeclipse.platform.team-187c7c9f70daf88d40b6873b9cc0e5a22efe5dc3.tar.gz
eclipse.platform.team-187c7c9f70daf88d40b6873b9cc0e5a22efe5dc3.tar.xz
eclipse.platform.team-187c7c9f70daf88d40b6873b9cc0e5a22efe5dc3.zip
Working on model example
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.team.examples.filesystem/plugin.xml38
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObject.java8
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java30
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelResource.java10
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModResourceMapping.java99
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelContainerResourceMapping.java55
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelMerger.java278
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelProvider.java83
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelResourceMapping.java59
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/MoeResourceMapping.java50
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java14
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java1
12 files changed, 712 insertions, 13 deletions
diff --git a/examples/org.eclipse.team.examples.filesystem/plugin.xml b/examples/org.eclipse.team.examples.filesystem/plugin.xml
index 936e190a3..df86ef852 100644
--- a/examples/org.eclipse.team.examples.filesystem/plugin.xml
+++ b/examples/org.eclipse.team.examples.filesystem/plugin.xml
@@ -432,14 +432,6 @@
</includes>
</viewerContentBinding>
</extension>
- <extension
- point="org.eclipse.core.runtime.adapters">
- <factory
- adaptableType="org.eclipse.team.examples.model.ModelObject"
- class="org.eclipse.team.examples.model.ui.AdapterFactory">
- <adapter type="org.eclipse.ui.model.IWorkbenchAdapter"/>
- </factory>
- </extension>
<extension
point="org.eclipse.ui.newWizards">
<category name="Team Logical Model Example" id="org.eclipse.team.examples.model.newWizards">
@@ -457,6 +449,36 @@
</wizard>
</extension>
+ <!-- ================================================================================ -->
+ <!-- Model provider for the model example -->
+ <!-- ================================================================================ -->
+ <extension
+ id="modelProvider"
+ point="org.eclipse.core.resources.modelProviders">
+ <modelProvider class="org.eclipse.team.examples.model.mapping.ModelProvider"/>
+ <extends-model id="org.eclipse.core.resources.modelProvider"/>
+ <enablement>
+ <test property="org.eclipse.core.resources.projectNature" value="org.eclipse.team.examples.filesystem.modelNature" />
+ </enablement>
+ </extension>
+
+ <!-- ================================================================================ -->
+ <!-- Adapter factories for the model example -->
+ <!-- ================================================================================ -->
+ <extension
+ point="org.eclipse.core.runtime.adapters">
+ <factory
+ adaptableType="org.eclipse.team.examples.model.ModelObject"
+ class="org.eclipse.team.examples.model.ui.AdapterFactory">
+ <adapter type="org.eclipse.ui.model.IWorkbenchAdapter"/>
+ <adapter type="org.eclipse.core.resources.mapping.ResourceMapping"/>
+ </factory>
+ <factory
+ adaptableType="org.eclipse.core.resources.mapping.ModelProvider"
+ class="org.eclipse.team.examples.model.ui.AdapterFactory">
+ <adapter type="org.eclipse.team.core.mapping.IResourceMappingMerger"/>
+ </factory>
+ </extension>
<!-- =================================================================================== -->
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObject.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObject.java
index 57ffe94b4..adbcd4e8f 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObject.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObject.java
@@ -59,5 +59,11 @@ public abstract class ModelObject extends PlatformObject {
/**
* Delete the model object
*/
- public abstract void delete() throws CoreException;;
+ public abstract void delete() throws CoreException;
+
+ /**
+ * Return the project that contains this model object.
+ * @return the project that contains this model object
+ */
+ public abstract ModelProject getProject();
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java
index d16ae5820..061c93983 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java
@@ -32,6 +32,21 @@ public class ModelObjectDefinitionFile extends ModelFile {
return false;
}
+ public static IResource[] getReferencedResources(IStorage storage) throws CoreException {
+ List result = new ArrayList();
+ String[] filePaths = readLines(storage);
+ for (int i = 0; i < filePaths.length; i++) {
+ String path = filePaths[i];
+ IFile file = getFile(path);
+ if (file != null
+ && file.getFileExtension() != null
+ && file.getFileExtension().equals(MODEL_OBJECT_DEFINITION_FILE_EXTENSION)) {
+ result.add(file);
+ }
+ }
+ return (IResource[]) result.toArray(new IResource[result.size()]);
+ }
+
public ModelObjectDefinitionFile(IFile file) {
super(file);
}
@@ -43,7 +58,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
return getModelObjectElementFiles();
}
- private ModelObjectElementFile[] getModelObjectElementFiles() throws CoreException {
+ public ModelObjectElementFile[] getModelObjectElementFiles() throws CoreException {
List result = new ArrayList();
String[] filePaths = readLines((IFile)getResource());
for (int i = 0; i < filePaths.length; i++) {
@@ -58,7 +73,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
return (ModelObjectElementFile[]) result.toArray(new ModelObjectElementFile[result.size()]);
}
- private String[] readLines(IFile file) throws CoreException {
+ private static String[] readLines(IStorage file) throws CoreException {
BufferedReader reader = new BufferedReader(new InputStreamReader(file.getContents()));
String line = null;
List result = new ArrayList();
@@ -96,7 +111,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
return null;
}
- private IFile getFile(String path) {
+ private static IFile getFile(String path) {
if (path.length() == 0)
return null;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
@@ -133,4 +148,13 @@ public class ModelObjectDefinitionFile extends ModelFile {
}
}
+ public void setElements(IResource[] resources) throws CoreException {
+ List paths = new ArrayList();
+ for (int i = 0; i < resources.length; i++) {
+ IResource resource = resources[i];
+ paths.add(resource.getFullPath().toString());
+ }
+ writeLines((String[]) paths.toArray(new String[paths.size()]));
+ }
+
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelResource.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelResource.java
index 56da7c26a..c5e4a54e8 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelResource.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelResource.java
@@ -73,7 +73,17 @@ public abstract class ModelResource extends ModelObject{
return getResource().hashCode();
}
+ /* (non-Javadoc)
+ * @see org.eclipse.team.examples.model.ModelObject#delete()
+ */
public void delete() throws CoreException {
getResource().delete(false, null);
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.examples.model.ModelObject#getProject()
+ */
+ public ModelProject getProject() {
+ return (ModelProject)ModelObject.create(getResource().getProject());
+ }
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModResourceMapping.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModResourceMapping.java
new file mode 100644
index 000000000..41143d67d
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModResourceMapping.java
@@ -0,0 +1,99 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.mapping.*;
+import org.eclipse.core.runtime.*;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+import org.eclipse.team.examples.model.*;
+
+public class ModResourceMapping extends ModelResourceMapping {
+
+ public ModResourceMapping(ModelObjectDefinitionFile file) {
+ super(file);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public ResourceTraversal[] getTraversals(ResourceMappingContext context,
+ IProgressMonitor monitor) throws CoreException {
+ Set resources = getLocalResources();
+ if (context instanceof RemoteResourceMappingContext) {
+ monitor.beginTask(null, IProgressMonitor.UNKNOWN);
+ RemoteResourceMappingContext remoteContext = (RemoteResourceMappingContext) context;
+ if (remoteContext.hasRemoteChange(getResource(), new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN))) {
+ IResource[] remoteResources = ModelObjectDefinitionFile.getReferencedResources(
+ remoteContext.fetchRemoteContents((IFile)getResource(),
+ new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)));
+ for (int i = 0; i < remoteResources.length; i++) {
+ IResource resource = remoteResources[i];
+ resources.add(resource);
+ }
+ }
+ if (remoteContext.isThreeWay()
+ && remoteContext.hasLocalChange(getResource(), new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN))) {
+ IResource[] remoteResources = ModelObjectDefinitionFile.getReferencedResources(
+ remoteContext.fetchBaseContents((IFile)getResource(),
+ new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)));
+ for (int i = 0; i < remoteResources.length; i++) {
+ IResource resource = remoteResources[i];
+ resources.add(resource);
+ }
+ }
+ monitor.done();
+ }
+ return new ResourceTraversal[] {
+ new ResourceTraversal((IResource[]) resources.toArray(new IResource[resources.size()]),
+ IResource.DEPTH_ZERO, IResource.NONE)
+ };
+ }
+
+ private IResource getResource() {
+ return ((ModelResource)getModelObject()).getResource();
+ }
+
+ private Set getLocalResources() throws CoreException {
+ ModelObjectDefinitionFile mdf = (ModelObjectDefinitionFile)getModelObject();
+ Set resources = new HashSet();
+ resources.add(mdf.getResource());
+ ModelObjectElementFile[] files = mdf.getModelObjectElementFiles();
+ for (int i = 0; i < files.length; i++) {
+ ModelObjectElementFile file = files[i];
+ resources.add(file.getResource());
+ }
+ return resources;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping)
+ */
+ public boolean contains(ResourceMapping mapping) {
+ if (mapping instanceof ModelResourceMapping) {
+ ModelObject object = (ModelObject)mapping.getModelObject();
+ if (object instanceof ModelResource) {
+ IResource resource = ((ModelResource) object).getResource();
+ try {
+ return getLocalResources().contains(resource);
+ } catch (CoreException e) {
+ FileSystemPlugin.log(e.getStatus());
+ }
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelContainerResourceMapping.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelContainerResourceMapping.java
new file mode 100644
index 000000000..85e61ded5
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelContainerResourceMapping.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.mapping.*;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.team.examples.model.*;
+
+public class ModelContainerResourceMapping extends ModelResourceMapping {
+
+ public ModelContainerResourceMapping(ModelContainer container) {
+ super(container);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public ResourceTraversal[] getTraversals(ResourceMappingContext context,
+ IProgressMonitor monitor) throws CoreException {
+ return new ResourceTraversal[] {
+ new ResourceTraversal(new IResource[] {
+ getResource()
+ }, IResource.DEPTH_INFINITE, IResource.NONE)
+ };
+ }
+
+ private IResource getResource() {
+ return ((ModelContainer)getModelObject()).getResource();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping)
+ */
+ public boolean contains(ResourceMapping mapping) {
+ if (mapping instanceof ModelResourceMapping) {
+ ModelObject object = (ModelObject)mapping.getModelObject();
+ if (object instanceof ModelResource) {
+ IResource resource = ((ModelResource) object).getResource();
+ return getResource().getFullPath().isPrefixOf(resource.getFullPath());
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelMerger.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelMerger.java
new file mode 100644
index 000000000..23c8fb317
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelMerger.java
@@ -0,0 +1,278 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import java.util.*;
+
+import org.eclipse.core.resources.*;
+import org.eclipse.core.resources.mapping.ResourceMapping;
+import org.eclipse.core.resources.mapping.ResourceTraversal;
+import org.eclipse.core.runtime.*;
+import org.eclipse.team.core.diff.*;
+import org.eclipse.team.core.history.IFileRevision;
+import org.eclipse.team.core.mapping.*;
+import org.eclipse.team.core.mapping.provider.*;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+import org.eclipse.team.examples.model.*;
+
+/**
+ * A resource mapping merger for our example model
+ */
+public class ModelMerger extends ResourceMappingMerger {
+
+ private final org.eclipse.team.examples.model.mapping.ModelProvider provider;
+
+ public ModelMerger(org.eclipse.team.examples.model.mapping.ModelProvider provider) {
+ this.provider = provider;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.mapping.ResourceMappingMerger#getModelProvider()
+ */
+ protected org.eclipse.core.resources.mapping.ModelProvider getModelProvider() {
+ return provider;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.core.mapping.ResourceMappingMerger#merge(org.eclipse.team.core.mapping.IMergeContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public IStatus merge(IMergeContext mergeContext, IProgressMonitor monitor) throws CoreException {
+ try {
+ IStatus status;
+ // Only override the merge for three-way synchronizations
+ if (mergeContext.getType() == SynchronizationContext.THREE_WAY) {
+ monitor.beginTask("Merging model elements", 100);
+ status = mergeModelElements(mergeContext, new SubProgressMonitor(monitor, 50));
+ // Stop the merge if there was a failure
+ if (!status.isOK())
+ return status;
+ // We need to wait for any background processing to complete for the context
+ // so the diff tree will be up-to-date when we delegate the rest of the merge
+ // to the superclass
+ try {
+ Platform.getJobManager().join(mergeContext, new SubProgressMonitor(monitor, 50));
+ } catch (InterruptedException e) {
+ // Ignore
+ }
+ // Delegate the rest of the merge to the superclass
+ status = super.merge(mergeContext, monitor);
+ } else {
+ status = super.merge(mergeContext, monitor);
+ }
+ return status;
+ } finally {
+ monitor.done();
+ }
+ }
+
+ /*
+ * Merge all the model element changes in the context
+ */
+ private IStatus mergeModelElements(IMergeContext mergeContext, IProgressMonitor monitor) throws CoreException {
+ try {
+ IDiff[] modeDiffs = getModDiffs(mergeContext);
+ List failures = new ArrayList();
+ monitor.beginTask(null, 100 * modeDiffs.length);
+ for (int i = 0; i < modeDiffs.length; i++) {
+ IDiff diff = modeDiffs[i];
+ if (!mergeModelElement(mergeContext, diff, new SubProgressMonitor(monitor, 100))) {
+ failures.add(diff);
+ }
+ }
+ if (failures.size() > 0) {
+ return new MergeStatus(FileSystemPlugin.ID, "Several objects could not be merged", getMappings(failures));
+ }
+ return Status.OK_STATUS;
+ } finally {
+ monitor.done();
+ }
+ }
+
+ private ResourceMapping[] getMappings(List failures) {
+ List mappings = new ArrayList();
+ for (Iterator iter = failures.iterator(); iter.hasNext();) {
+ IDiff diff = (IDiff) iter.next();
+ IResource resource = ResourceDiffTree.getResourceFor(diff);
+ ModelObjectDefinitionFile file = (ModelObjectDefinitionFile)ModelObject.create(resource);
+ mappings.add(file.getAdapter(ResourceMapping.class));
+ }
+ return (ResourceMapping[]) mappings.toArray(new ResourceMapping[mappings.size()]);
+ }
+
+ /*
+ * Return all the diffs for MOD files.
+ */
+ private IDiff[] getModDiffs(IMergeContext mergeContext) {
+ final List result = new ArrayList();
+ mergeContext.getDiffTree().accept(getModelProjectTraversals(mergeContext), new IDiffVisitor() {
+ public boolean visit(IDiff diff) {
+ IResource resource = ResourceDiffTree.getResourceFor(diff);
+ if (ModelObjectDefinitionFile.isModFile(resource)) {
+ result.add(diff);
+ }
+ return true;
+ }
+
+ });
+ return (IDiff[]) result.toArray(new IDiff[result.size()]);
+ }
+
+ /*
+ * Return a traversal that covers all the model projects in the scope of the merge.
+ */
+ private ResourceTraversal[] getModelProjectTraversals(IMergeContext mergeContext) {
+ IProject[] scopeProjects = mergeContext.getScope().getProjects();
+ List modelProjects = new ArrayList();
+ for (int i = 0; i < scopeProjects.length; i++) {
+ IProject project = scopeProjects[i];
+ try {
+ if (ModelProject.isModProject(project)) {
+ modelProjects.add(project);
+ }
+ } catch (CoreException e) {
+ FileSystemPlugin.log(e.getStatus());
+ }
+ }
+ if (modelProjects.isEmpty())
+ return new ResourceTraversal[0];
+ return new ResourceTraversal[] {
+ new ResourceTraversal((IResource[]) modelProjects.toArray(new IResource[modelProjects.size()]),
+ IResource.DEPTH_INFINITE, IResource.NONE)
+ };
+ }
+
+ /*
+ * Merge the model definition file and all the element files it contains.
+ */
+ private boolean mergeModelElement(IMergeContext mergeContext, IDiff diff, IProgressMonitor monitor) throws CoreException {
+ if (diff instanceof IThreeWayDiff) {
+ IThreeWayDiff twd = (IThreeWayDiff) diff;
+ if (twd.getDirection() == IThreeWayDiff.INCOMING
+ || twd.getDirection() == IThreeWayDiff.CONFLICTING) {
+ IResource resource = ResourceDiffTree.getResourceFor(diff);
+
+ // First, check if a change conflicts with a deletion
+ if (twd.getDirection() == IThreeWayDiff.CONFLICTING) {
+ if (!resource.exists())
+ return false;
+ if (((IResourceDiff)twd.getRemoteChange()).getAfterState() == null)
+ return false;
+ }
+
+ // First determine the element files and element file changes
+ IResourceDiff remoteChange = (IResourceDiff)twd.getRemoteChange();
+ IResource[] localElements = getReferencedResources(resource);
+ IResource[] baseElements = getReferencedResources(remoteChange.getBeforeState(), monitor);
+ IResource[] remoteElements = getReferencedResources(remoteChange.getAfterState(), monitor);
+ IResource[] addedElements = getAddedElements(baseElements, remoteElements);
+ // Trick: The removed elements can be obtained by reversing the base and remote and looking for added
+ IResource[] removedElements = getAddedElements(remoteElements, baseElements);
+
+ // Check to see if any removed elements have changed locally
+ if (hasOutgoingChanges(mergeContext, removedElements)) {
+ return false;
+ }
+
+ // Now try to merge all the element files involved
+ Set elementFiles = new HashSet();
+ elementFiles.addAll(Arrays.asList(baseElements));
+ elementFiles.addAll(Arrays.asList(localElements));
+ elementFiles.addAll(Arrays.asList(remoteElements));
+ if (!mergeElementFiles(mergeContext, (IResource[]) elementFiles.toArray(new IResource[elementFiles.size()]), monitor)) {
+ return false;
+ }
+
+ // Finally, merge the model definition
+ if (!resource.exists()) {
+ // This is a new model definition so just merge it
+ IStatus status = mergeContext.merge(diff, false, monitor);
+ if (!status.isOK())
+ return false;
+ } else {
+ // Update the contents of the model definition file
+ ModelObjectDefinitionFile file = (ModelObjectDefinitionFile)ModelObject.create(resource);
+ elementFiles = new HashSet();
+ elementFiles.addAll(Arrays.asList(localElements));
+ elementFiles.addAll(Arrays.asList(addedElements));
+ elementFiles.removeAll(Arrays.asList(removedElements));
+ file.setElements((IResource[]) elementFiles.toArray(new IResource[elementFiles.size()]));
+ // Let the merge context know we handled the file
+ mergeContext.markAsMerged(diff, false, monitor);
+ }
+ }
+ }
+ return true;
+ }
+
+ private boolean mergeElementFiles(IMergeContext mergeContext, IResource[] resources, IProgressMonitor monitor) throws CoreException {
+ IDiff[] diffs = getDiffs(mergeContext, resources);
+ IStatus status = mergeContext.merge(diffs, false, monitor);
+ return status.isOK();
+ }
+
+ private IDiff[] getDiffs(IMergeContext mergeContext, IResource[] resources) {
+ Set diffSet = new HashSet();
+ for (int i = 0; i < resources.length; i++) {
+ IResource resource = resources[i];
+ IDiff[] diffs = mergeContext.getDiffTree().getDiffs(resource, IResource.DEPTH_ZERO);
+ diffSet.addAll(Arrays.asList(diffs));
+ }
+ return (IDiff[]) diffSet.toArray(new IDiff[diffSet.size()]);
+ }
+
+ private boolean hasOutgoingChanges(IMergeContext mergeContext, IResource[] removedElements) {
+ FastDiffFilter fastDiffFilter = new FastDiffFilter() {
+ public boolean select(IDiff diff) {
+ if (diff instanceof IThreeWayDiff) {
+ IThreeWayDiff twd = (IThreeWayDiff) diff;
+ return twd.getDirection() == IThreeWayDiff.OUTGOING || twd.getDirection() == IThreeWayDiff.CONFLICTING;
+ }
+ return false;
+ }
+ };
+ for (int i = 0; i < removedElements.length; i++) {
+ IResource resource = removedElements[i];
+ if (mergeContext.getDiffTree().hasMatchingDiffs(resource.getFullPath(), fastDiffFilter))
+ return true;
+ }
+ return false;
+ }
+
+ private IResource[] getAddedElements(IResource[] baseElements, IResource[] remoteElements) {
+ List result = new ArrayList();
+ Set base = new HashSet();
+ for (int i = 0; i < baseElements.length; i++) {
+ IResource resource = baseElements[i];
+ base.add(resource);
+ }
+ for (int i = 0; i < remoteElements.length; i++) {
+ IResource resource = remoteElements[i];
+ if (!base.contains(resource))
+ result.add(resource);
+ }
+ return (IResource[]) result.toArray(new IResource[result.size()]);
+ }
+
+ private IResource[] getReferencedResources(IResource resource) throws CoreException {
+ if (resource instanceof IFile && resource.exists()) {
+ return ModelObjectDefinitionFile.getReferencedResources((IFile) resource);
+ }
+ return new IResource[0];
+ }
+
+ private IResource[] getReferencedResources(IFileRevision revision, IProgressMonitor monitor) throws CoreException {
+ if (revision != null) {
+ return ModelObjectDefinitionFile.getReferencedResources(revision.getStorage(monitor));
+ }
+ return new IResource[0];
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelProvider.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelProvider.java
new file mode 100644
index 000000000..4a51fcc14
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelProvider.java
@@ -0,0 +1,83 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.*;
+import org.eclipse.core.resources.mapping.ModelStatus;
+import org.eclipse.core.runtime.*;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.team.examples.filesystem.FileSystemPlugin;
+import org.eclipse.team.examples.model.*;
+
+/**
+ * The model provider for our example
+ */
+public class ModelProvider extends
+ org.eclipse.core.resources.mapping.ModelProvider {
+
+ public static final String ID = "org.eclipse.team.examples.filesystem.modelProvider";
+
+ public ModelProvider() {
+ super();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ModelProvider#validateChange(org.eclipse.core.resources.IResourceDelta, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public IStatus validateChange(IResourceDelta delta, IProgressMonitor monitor) {
+ // Visit the changes in the delta to look for changes we care about
+ final List problems = new ArrayList();
+ try {
+ delta.accept(new IResourceDeltaVisitor() {
+ public boolean visit(IResourceDelta delta) throws CoreException {
+ IResource resource = delta.getResource();
+ if (ModelObjectElementFile.isMoeFile(resource)) {
+ // Removal may leave a stale reference in a MOD file
+ if (delta.getKind() == IResourceDelta.REMOVED) {
+ IStatus status = new ModelStatus(IStatus.ERROR, FileSystemPlugin.ID, getDescriptor().getId(),
+ NLS.bind("Deleting file {0} may corrupt any model definition that references it.", resource.getFullPath()));
+ problems.add(status);
+ }
+ }
+ if (ModelObjectDefinitionFile.isModFile(resource)) {
+ // Removal may leave unreferenced MOE files around
+ if (delta.getKind() == IResourceDelta.REMOVED) {
+ IStatus status = new ModelStatus(IStatus.WARNING, FileSystemPlugin.ID, getDescriptor().getId(),
+ NLS.bind("Deleting file {0} may result in unreferenced element files.", resource.getFullPath()));
+ problems.add(status);
+ }
+ if (delta.getKind() == IResourceDelta.ADDED
+ && ((delta.getFlags() & IResourceDelta.COPIED_FROM) > 0)) {
+ // Copying will result in two MOD files that reference the same elements
+ IStatus status = new ModelStatus(IStatus.ERROR, FileSystemPlugin.ID, getDescriptor().getId(),
+ NLS.bind("Copying file {0} may corrupt the model defintion.", delta.getMovedFromPath()));
+ problems.add(status);
+ }
+ }
+ return delta.getResource().getType() == IResource.ROOT
+ || ModelProject.isModProject(delta.getResource().getProject());
+ }
+ });
+ } catch (CoreException e) {
+ FileSystemPlugin.log(e.getStatus());
+ }
+ if (problems.size() == 1)
+ return (IStatus)problems.get(0);
+ else if (problems.size() > 1) {
+ return new MultiStatus(FileSystemPlugin.ID, 0, (IStatus[]) problems.toArray(new IStatus[problems.size()]), "Multiple potential side effects have been found.", null);
+ }
+ return super.validateChange(delta, monitor);
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelResourceMapping.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelResourceMapping.java
new file mode 100644
index 000000000..09a10d39d
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelResourceMapping.java
@@ -0,0 +1,59 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.mapping.ResourceMapping;
+import org.eclipse.team.examples.model.*;
+
+public abstract class ModelResourceMapping extends ResourceMapping {
+
+ private final ModelObject object;
+
+ public static ResourceMapping create(ModelObject object) {
+ if (object instanceof ModelContainer) {
+ return new ModelContainerResourceMapping((ModelContainer) object);
+ }
+ if (object instanceof ModelObjectDefinitionFile) {
+ return new ModResourceMapping((ModelObjectDefinitionFile) object);
+ }
+ if (object instanceof ModelObjectElementFile) {
+ return new MoeResourceMapping((ModelObjectElementFile) object);
+ }
+ return null;
+ }
+
+ protected ModelResourceMapping(ModelObject object) {
+ this.object = object;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelObject()
+ */
+ public Object getModelObject() {
+ return object;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getModelProviderId()
+ */
+ public String getModelProviderId() {
+ return ModelProvider.ID;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getProjects()
+ */
+ public IProject[] getProjects() {
+ return new IProject[] { (IProject)object.getProject().getResource() };
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/MoeResourceMapping.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/MoeResourceMapping.java
new file mode 100644
index 000000000..3619f6578
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/MoeResourceMapping.java
@@ -0,0 +1,50 @@
+/*******************************************************************************
+ * Copyright (c) 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.team.examples.model.mapping;
+
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.mapping.*;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.team.examples.model.*;
+
+public class MoeResourceMapping extends ModelResourceMapping {
+
+ public MoeResourceMapping(ModelObjectElementFile file) {
+ super(file);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#getTraversals(org.eclipse.core.resources.mapping.ResourceMappingContext, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public ResourceTraversal[] getTraversals(ResourceMappingContext context,
+ IProgressMonitor monitor) throws CoreException {
+ return new ResourceTraversal[] {
+ new ResourceTraversal(new IResource[] {
+ getResource()
+ }, IResource.DEPTH_ZERO, IResource.NONE)
+ };
+ }
+
+ private IResource getResource() {
+ return ((ModelResource)getModelObject()).getResource();
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.core.resources.mapping.ResourceMapping#contains(org.eclipse.core.resources.mapping.ResourceMapping)
+ */
+ public boolean contains(ResourceMapping mapping) {
+ if (mapping.equals(this))
+ return true;
+ return false;
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java
index c3cca4192..1ed4b7b65 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java
@@ -10,17 +10,29 @@
*******************************************************************************/
package org.eclipse.team.examples.model.ui;
+import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.core.runtime.IAdapterFactory;
+import org.eclipse.team.core.mapping.IResourceMappingMerger;
import org.eclipse.team.examples.model.ModelObject;
+import org.eclipse.team.examples.model.mapping.*;
import org.eclipse.ui.model.IWorkbenchAdapter;
public class AdapterFactory implements IAdapterFactory {
private IWorkbenchAdapter modelAdapter = new ModelWorkbenchAdapter();
+ private ModelMerger modelMerger;
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType == IWorkbenchAdapter.class && adaptableObject instanceof ModelObject)
return modelAdapter;
+ if (adapterType == ResourceMapping.class && adaptableObject instanceof ModelObject)
+ return ModelResourceMapping.create((ModelObject)adaptableObject);
+ if (adapterType == IResourceMappingMerger.class && adaptableObject instanceof ModelProvider) {
+ if (modelMerger == null) {
+ modelMerger = new ModelMerger((ModelProvider)adaptableObject);
+ }
+ return modelMerger;
+ }
return null;
}
@@ -28,7 +40,7 @@ public class AdapterFactory implements IAdapterFactory {
* @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
*/
public Class[] getAdapterList() {
- return new Class[] { IWorkbenchAdapter.class };
+ return new Class[] { IWorkbenchAdapter.class, ResourceMapping.class, IResourceMappingMerger.class };
}
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java
index 46d8fc356..58ce25751 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java
@@ -179,6 +179,7 @@ public class ModelNavigatorContentProvider extends BaseWorkbenchContentProvider
*/
public void getPipelinedChildren(Object aParent, Set theCurrentChildren) {
// Nothing to do
+ theCurrentChildren.iterator();
}
/* (non-Javadoc)

Back to the top