Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Valenta2006-05-19 15:48:39 +0000
committerMichael Valenta2006-05-19 15:48:39 +0000
commit98bd9e49f141efdd4d71854704784fbab3bfcf00 (patch)
tree1f64a3787af71c8a4791f707208a17a3c6674a25 /examples
parent363487360103638313ac94b049c760d8f11b70e3 (diff)
downloadeclipse.platform.team-98bd9e49f141efdd4d71854704784fbab3bfcf00.tar.gz
eclipse.platform.team-98bd9e49f141efdd4d71854704784fbab3bfcf00.tar.xz
eclipse.platform.team-98bd9e49f141efdd4d71854704784fbab3bfcf00.zip
Working on model exampleI200605191155
Diffstat (limited to 'examples')
-rw-r--r--examples/org.eclipse.team.examples.filesystem/plugin.xml2
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectDefinitionFile.java21
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectElementFile.java2
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModResourceMapping.java2
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/mapping/ModelMerger.java10
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/AdapterFactory.java11
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/ModelNavigatorContentProvider.java17
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/CompareAdapter.java98
-rw-r--r--examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/ModelSyncContentProvider.java142
9 files changed, 279 insertions, 26 deletions
diff --git a/examples/org.eclipse.team.examples.filesystem/plugin.xml b/examples/org.eclipse.team.examples.filesystem/plugin.xml
index 0a7772038..d1322b3d5 100644
--- a/examples/org.eclipse.team.examples.filesystem/plugin.xml
+++ b/examples/org.eclipse.team.examples.filesystem/plugin.xml
@@ -454,6 +454,7 @@
<!-- ================================================================================ -->
<extension
id="modelProvider"
+ name="Example Model"
point="org.eclipse.core.resources.modelProviders">
<modelProvider class="org.eclipse.team.examples.model.mapping.ExampleModelProvider"/>
<extends-model id="org.eclipse.core.resources.modelProvider"/>
@@ -477,6 +478,7 @@
adaptableType="org.eclipse.core.resources.mapping.ModelProvider"
class="org.eclipse.team.examples.model.ui.AdapterFactory">
<adapter type="org.eclipse.team.core.mapping.IResourceMappingMerger"/>
+ <adapter type="org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter"/>
</factory>
</extension>
<extension
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 061c93983..fa32c4a07 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,15 +32,15 @@ public class ModelObjectDefinitionFile extends ModelFile {
return false;
}
- public static IResource[] getReferencedResources(IStorage storage) throws CoreException {
+ public static IResource[] getReferencedResources(String projectName, 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);
+ IFile file = getFile(projectName, path);
if (file != null
&& file.getFileExtension() != null
- && file.getFileExtension().equals(MODEL_OBJECT_DEFINITION_FILE_EXTENSION)) {
+ && file.getFileExtension().equals(ModelObjectElementFile.MODEL_OBJECT_ELEMENTFILE_EXTENSION)) {
result.add(file);
}
}
@@ -63,7 +63,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
String[] filePaths = readLines((IFile)getResource());
for (int i = 0; i < filePaths.length; i++) {
String path = filePaths[i];
- IFile file = getFile(path);
+ IFile file = getFile(getResource().getProject().getName(), path);
if (file != null) {
ModelObjectElementFile moeFile = getMoeFile(file);
if (moeFile != null)
@@ -111,20 +111,21 @@ public class ModelObjectDefinitionFile extends ModelFile {
return null;
}
- private static IFile getFile(String path) {
+ private static IFile getFile(String projectName, String path) {
if (path.length() == 0)
return null;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
- IStatus status = workspace.validatePath(path, IResource.FILE);
+ IStatus status = workspace.validatePath("/" + projectName + "/" + path, IResource.FILE);
if (status.isOK()) {
- return workspace.getRoot().getFile(new Path(path));
+ IProject project = workspace.getRoot().getProject(projectName);
+ return project.getFile(new Path(path));
}
FileSystemPlugin.log(status);
return null;
}
public void addMoe(IFile file) throws CoreException {
- ((IFile)getResource()).appendContents(new ByteArrayInputStream(("\n" + file.getFullPath()).getBytes()), false, true, null);
+ ((IFile)getResource()).appendContents(new ByteArrayInputStream(("\n" + file.getProjectRelativePath()).getBytes()), false, true, null);
}
public void remove(ModelObjectElementFile file) throws CoreException {
@@ -133,7 +134,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
for (int i = 0; i < files.length; i++) {
ModelObjectElementFile child = files[i];
if (!child.equals(file)) {
- paths.add(child.getResource().getFullPath().toString());
+ paths.add(child.getResource().getProjectRelativePath().toString());
}
}
writeLines((String[]) paths.toArray(new String[paths.size()]));
@@ -152,7 +153,7 @@ public class ModelObjectDefinitionFile extends ModelFile {
List paths = new ArrayList();
for (int i = 0; i < resources.length; i++) {
IResource resource = resources[i];
- paths.add(resource.getFullPath().toString());
+ paths.add(resource.getProjectRelativePath().toString());
}
writeLines((String[]) paths.toArray(new String[paths.size()]));
}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectElementFile.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectElementFile.java
index 4c6b02dbd..84ec39268 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectElementFile.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ModelObjectElementFile.java
@@ -16,7 +16,7 @@ import org.eclipse.core.runtime.CoreException;
public class ModelObjectElementFile extends ModelFile {
- private static final String MODEL_OBJECT_ELEMENTFILE_EXTENSION = "moe";
+ public static final String MODEL_OBJECT_ELEMENTFILE_EXTENSION = "moe";
private final ModelObjectDefinitionFile parent;
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
index 41143d67d..e7f38c956 100644
--- 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
@@ -37,6 +37,7 @@ public class ModResourceMapping extends ModelResourceMapping {
RemoteResourceMappingContext remoteContext = (RemoteResourceMappingContext) context;
if (remoteContext.hasRemoteChange(getResource(), new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN))) {
IResource[] remoteResources = ModelObjectDefinitionFile.getReferencedResources(
+ getResource().getProject().getName(),
remoteContext.fetchRemoteContents((IFile)getResource(),
new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)));
for (int i = 0; i < remoteResources.length; i++) {
@@ -47,6 +48,7 @@ public class ModResourceMapping extends ModelResourceMapping {
if (remoteContext.isThreeWay()
&& remoteContext.hasLocalChange(getResource(), new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN))) {
IResource[] remoteResources = ModelObjectDefinitionFile.getReferencedResources(
+ getResource().getProject().getName(),
remoteContext.fetchBaseContents((IFile)getResource(),
new SubProgressMonitor(monitor, IProgressMonitor.UNKNOWN)));
for (int i = 0; i < remoteResources.length; i++) {
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
index 75953ff19..920a81662 100644
--- 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
@@ -170,8 +170,8 @@ public class ModelMerger extends ResourceMappingMerger {
// 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[] baseElements = getReferencedResources(resource.getProject().getName(), remoteChange.getBeforeState(), monitor);
+ IResource[] remoteElements = getReferencedResources(resource.getProject().getName(), 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);
@@ -263,14 +263,14 @@ public class ModelMerger extends ResourceMappingMerger {
private IResource[] getReferencedResources(IResource resource) throws CoreException {
if (resource instanceof IFile && resource.exists()) {
- return ModelObjectDefinitionFile.getReferencedResources((IFile) resource);
+ return ModelObjectDefinitionFile.getReferencedResources(resource.getProject().getName(), (IFile) resource);
}
return new IResource[0];
}
- private IResource[] getReferencedResources(IFileRevision revision, IProgressMonitor monitor) throws CoreException {
+ private IResource[] getReferencedResources(String projectName, IFileRevision revision, IProgressMonitor monitor) throws CoreException {
if (revision != null) {
- return ModelObjectDefinitionFile.getReferencedResources(revision.getStorage(monitor));
+ return ModelObjectDefinitionFile.getReferencedResources(projectName, revision.getStorage(monitor));
}
return new IResource[0];
}
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 9a61b7cbf..814e6cdac 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
@@ -15,12 +15,15 @@ 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.team.examples.model.ui.mapping.CompareAdapter;
+import org.eclipse.team.ui.mapping.ISynchronizationCompareAdapter;
import org.eclipse.ui.model.IWorkbenchAdapter;
public class AdapterFactory implements IAdapterFactory {
private IWorkbenchAdapter modelAdapter = new ModelWorkbenchAdapter();
private ModelMerger modelMerger;
+ private CompareAdapter compareAdapter;
public Object getAdapter(Object adaptableObject, Class adapterType) {
if (adapterType == IWorkbenchAdapter.class && adaptableObject instanceof ModelObject)
@@ -33,6 +36,12 @@ public class AdapterFactory implements IAdapterFactory {
}
return modelMerger;
}
+ if (adapterType == ISynchronizationCompareAdapter.class && adaptableObject instanceof ExampleModelProvider) {
+ if (compareAdapter == null) {
+ compareAdapter = new CompareAdapter((ExampleModelProvider)adaptableObject);
+ }
+ return compareAdapter;
+ }
return null;
}
@@ -40,7 +49,7 @@ public class AdapterFactory implements IAdapterFactory {
* @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
*/
public Class[] getAdapterList() {
- return new Class[] { IWorkbenchAdapter.class, ResourceMapping.class, IResourceMappingMerger.class };
+ return new Class[] { IWorkbenchAdapter.class, ResourceMapping.class, IResourceMappingMerger.class, ISynchronizationCompareAdapter.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 46b5b0e89..1f871a28b 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
@@ -36,13 +36,28 @@ public class ModelNavigatorContentProvider extends BaseWorkbenchContentProvider
private ICommonContentExtensionSite extensionSite;
private boolean isWorkspaceRoot;
private Viewer viewer;
+ private final boolean updateViewer;
+
+ public ModelNavigatorContentProvider() {
+ super();
+ updateViewer = true;
+ }
+
+ /**
+ * Create a contentProvider
+ * @param updateViewer whether this content provider is reponsible for updating the viewer
+ */
+ public ModelNavigatorContentProvider(boolean updateViewer) {
+ this.updateViewer = updateViewer;
+ }
/* (non-Javadoc)
* @see org.eclipse.ui.navigator.ICommonContentProvider#init(org.eclipse.ui.navigator.ICommonContentExtensionSite)
*/
public void init(ICommonContentExtensionSite aConfig) {
extensionSite = aConfig;
- ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
+ if (updateViewer)
+ ResourcesPlugin.getWorkspace().addResourceChangeListener(this, IResourceChangeEvent.POST_CHANGE);
}
/* (non-Javadoc)
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/CompareAdapter.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/CompareAdapter.java
new file mode 100644
index 000000000..db893cc66
--- /dev/null
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/CompareAdapter.java
@@ -0,0 +1,98 @@
+/*******************************************************************************
+ * 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.ui.mapping;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.compare.structuremergeviewer.ICompareInput;
+import org.eclipse.core.resources.mapping.ModelProvider;
+import org.eclipse.core.resources.mapping.ResourceMapping;
+import org.eclipse.team.core.mapping.ISynchronizationContext;
+import org.eclipse.team.examples.model.ModelObject;
+import org.eclipse.team.examples.model.ModelObjectElementFile;
+import org.eclipse.team.examples.model.mapping.ExampleModelProvider;
+import org.eclipse.team.ui.mapping.SynchronizationCompareAdapter;
+import org.eclipse.ui.IMemento;
+
+/**
+ * Compare adapter for use with our example model.
+ */
+public class CompareAdapter extends SynchronizationCompareAdapter {
+
+ private static final String CTX_MODEL_MAPPINGS = "org.eclipse.team.examples.filesystem.modelMappings";
+
+ private final ExampleModelProvider provider;
+
+ public CompareAdapter(ExampleModelProvider provider) {
+ this.provider = provider;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.ui.mapping.SynchronizationCompareAdapter#getName(org.eclipse.core.resources.mapping.ResourceMapping)
+ */
+ public String getName(ResourceMapping mapping) {
+ Object o = mapping.getModelObject();
+ if (o instanceof ModelObject) {
+ return ((ModelObject) o).getName();
+ }
+ return super.getName(mapping);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.ui.mapping.SynchronizationCompareAdapter#getPathString(org.eclipse.core.resources.mapping.ResourceMapping)
+ */
+ public String getPathString(ResourceMapping mapping) {
+ Object o = mapping.getModelObject();
+ if (o instanceof ModelObject) {
+ return ((ModelObject) o).getPath();
+ }
+ return super.getPathString(mapping);
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.ui.mapping.SynchronizationCompareAdapter#asCompareInput(org.eclipse.team.core.mapping.ISynchronizationContext, java.lang.Object)
+ */
+ public ICompareInput asCompareInput(ISynchronizationContext context, Object o) {
+ if (o instanceof ModelObjectElementFile) {
+ ModelObjectElementFile moeFile = (ModelObjectElementFile) o;
+ // Use a file compare input for the model element file
+ return super.asCompareInput(context, moeFile.getResource());
+ }
+ return super.asCompareInput(context, o);
+ }
+
+ public ResourceMapping[] restore(IMemento memento) {
+ List result = new ArrayList();
+ IMemento[] children = memento.getChildren(CTX_MODEL_MAPPINGS);
+ for (int i = 0; i < children.length; i++) {
+ IMemento child = children[i];
+ ResourceMapping mapping = restoreMapping(child);
+ if (mapping != null)
+ result.add(mapping);
+ }
+ return (ResourceMapping[]) result.toArray(new ResourceMapping[result.size()]);
+ }
+
+ private ResourceMapping restoreMapping(IMemento child) {
+ // TODO Auto-generated method stub
+ return null;
+ }
+
+ public void save(ResourceMapping[] mappings, IMemento memento) {
+
+ }
+
+ public ModelProvider getProvider() {
+ return provider;
+ }
+
+}
diff --git a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/ModelSyncContentProvider.java b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/ModelSyncContentProvider.java
index 236f7c1d0..c77f46f81 100644
--- a/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/ModelSyncContentProvider.java
+++ b/examples/org.eclipse.team.examples.filesystem/src/org/eclipse/team/examples/model/ui/mapping/ModelSyncContentProvider.java
@@ -14,10 +14,11 @@ import java.util.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.mapping.*;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.NullProgressMonitor;
-import org.eclipse.jface.viewers.ITreeContentProvider;
-import org.eclipse.team.core.diff.IDiff;
+import org.eclipse.core.runtime.*;
+import org.eclipse.jface.viewers.*;
+import org.eclipse.swt.widgets.Tree;
+import org.eclipse.swt.widgets.TreeItem;
+import org.eclipse.team.core.diff.*;
import org.eclipse.team.core.mapping.ISynchronizationContext;
import org.eclipse.team.core.mapping.ISynchronizationScope;
import org.eclipse.team.core.mapping.provider.ResourceDiffTree;
@@ -25,6 +26,7 @@ import org.eclipse.team.examples.filesystem.FileSystemPlugin;
import org.eclipse.team.examples.model.*;
import org.eclipse.team.examples.model.mapping.ExampleModelProvider;
import org.eclipse.team.examples.model.ui.ModelNavigatorContentProvider;
+import org.eclipse.team.internal.ui.Utils;
import org.eclipse.team.internal.ui.mapping.SynchronizationResourceMappingContext;
import org.eclipse.team.ui.mapping.SynchronizationContentProvider;
import org.eclipse.ui.navigator.*;
@@ -48,7 +50,7 @@ public class ModelSyncContentProvider extends SynchronizationContentProvider imp
*/
public void init(ICommonContentExtensionSite site) {
super.init(site);
- delegate = new ModelNavigatorContentProvider();
+ delegate = new ModelNavigatorContentProvider(getContext() != null);
delegate.init(site);
}
@@ -90,7 +92,7 @@ public class ModelSyncContentProvider extends SynchronizationContentProvider imp
ResourceMappingContext rmc = new SynchronizationResourceMappingContext(context);
try {
// Technically speaking, this may end up being too long running for this
- // but it will do for illustration purposes
+ // (i.e. we may end up hitting the server) but it will do for illustration purposes
return mapping.getTraversals(rmc, new NullProgressMonitor());
} catch (CoreException e) {
FileSystemPlugin.log(e.getStatus());
@@ -114,7 +116,9 @@ public class ModelSyncContentProvider extends SynchronizationContentProvider imp
IDiff diff = diffs[i];
IResource resource = ResourceDiffTree.getResourceFor(diff);
if (!resource.exists() && ModelObjectDefinitionFile.isModFile(resource)) {
- allChildren.add(ModelObject.create(resource));
+ ModelObject o = ModelObject.create(resource);
+ if (o != null)
+ allChildren.add(o);
}
}
}
@@ -125,7 +129,9 @@ public class ModelSyncContentProvider extends SynchronizationContentProvider imp
IDiff diff = diffs[i];
IResource resource = ResourceDiffTree.getResourceFor(diff);
if (!resource.exists() && ModelObjectElementFile.isMoeFile(resource)) {
- allChildren.add(ModelObject.create(resource));
+ ModelObject o = new ModelObjectElementFile((ModelObjectDefinitionFile)parent, (IFile)resource);
+ if (o != null)
+ allChildren.add(o);
}
}
}
@@ -223,5 +229,125 @@ public class ModelSyncContentProvider extends SynchronizationContentProvider imp
// No need to intercept the update
return false;
}
+
+ /* (non-Javadoc)
+ * @see org.eclipse.team.ui.mapping.SynchronizationContentProvider#diffsChanged(org.eclipse.team.core.diff.IDiffChangeEvent, org.eclipse.core.runtime.IProgressMonitor)
+ */
+ public void diffsChanged(final IDiffChangeEvent event, IProgressMonitor monitor) {
+ // Override in order to perform custom viewer updates when the diff tree changes
+ Utils.syncExec(new Runnable() {
+ public void run() {
+ handleChange(event);
+ }
+ }, (StructuredViewer)getViewer());
+ }
+
+ void handleChange(IDiffChangeEvent event) {
+ Set existingProjects = getVisibleModelProjects();
+ IProject[] changedProjects = getChangedModelProjects(event);
+ List refreshes = new ArrayList(changedProjects.length);
+ List additions = new ArrayList(changedProjects.length);
+ List removals = new ArrayList(changedProjects.length);
+ for (int i = 0; i < changedProjects.length; i++) {
+ IProject project = changedProjects[i];
+ if (hasVisibleChanges(event.getTree(), project)) {
+ if (existingProjects.contains(project)) {
+ refreshes.add(ModelObject.create(project));
+ } else {
+ additions.add(ModelObject.create(project));
+ }
+ } else if (existingProjects.contains(project)) {
+ removals.add(ModelObject.create(project));
+
+ }
+ }
+ if (!removals.isEmpty() || !additions.isEmpty() || !refreshes.isEmpty()) {
+ TreeViewer viewer = (TreeViewer)getViewer();
+ Tree tree = viewer.getTree();
+ try {
+ tree.setRedraw(false);
+ if (!additions.isEmpty())
+ viewer.add(viewer.getInput(), additions.toArray());
+ if (!removals.isEmpty())
+ viewer.remove(viewer.getInput(), removals.toArray());
+ if (!refreshes.isEmpty()) {
+ for (Iterator iter = refreshes.iterator(); iter.hasNext();) {
+ Object element = iter.next();
+ viewer.refresh(element);
+ }
+ }
+ } finally {
+ tree.setRedraw(true);
+ }
+ }
+ }
+
+ private boolean hasVisibleChanges(IDiffTree tree, IProject project) {
+ return tree.hasMatchingDiffs(project.getFullPath(), new FastDiffFilter() {
+ public boolean select(IDiff diff) {
+ return isVisible(diff);
+ }
+ });
+ }
+
+ /*
+ * Return the list of all projects that are model projects
+ */
+ private IProject[] getChangedModelProjects(IDiffChangeEvent event) {
+ Set result = new HashSet();
+ IDiff[] changes = event.getChanges();
+ for (int i = 0; i < changes.length; i++) {
+ IDiff diff = changes[i];
+ IResource resource = ResourceDiffTree.getResourceFor(diff);
+ if (resource != null && isModProject(resource.getProject())) {
+ result.add(resource.getProject());
+ }
+ }
+ IDiff[] additions = event.getAdditions();
+ for (int i = 0; i < additions.length; i++) {
+ IDiff diff = additions[i];
+ IResource resource = ResourceDiffTree.getResourceFor(diff);
+ if (resource != null && isModProject(resource.getProject())) {
+ result.add(resource.getProject());
+ }
+ }
+ IPath[] removals = event.getRemovals();
+ for (int i = 0; i < removals.length; i++) {
+ IPath path = removals[i];
+ if (path.segmentCount() > 0) {
+ IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
+ if (isModProject(project))
+ result.add(project);
+ }
+ }
+ return (IProject[]) result.toArray(new IProject[result.size()]);
+ }
+
+ private boolean isModProject(IProject project) {
+ try {
+ return ModelProject.isModProject(project);
+ } catch (CoreException e) {
+ FileSystemPlugin.log(e.getStatus());
+ }
+ return false;
+ }
+
+ /*
+ * Return the set of visible model projects
+ */
+ private Set getVisibleModelProjects() {
+ TreeViewer viewer = (TreeViewer)getViewer();
+ Tree tree = viewer.getTree();
+ TreeItem[] children = tree.getItems();
+ Set result = new HashSet();
+ for (int i = 0; i < children.length; i++) {
+ TreeItem control = children[i];
+ Object data = control.getData();
+ if (data instanceof ModelProject) {
+ result.add(((ModelProject) data).getProject());
+ }
+ }
+ return result;
+ }
}

Back to the top