KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ui > mapping > SynchronizationResourceMappingContext


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ui.mapping;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.*;
17 import org.eclipse.core.resources.mapping.RemoteResourceMappingContext;
18 import org.eclipse.core.resources.mapping.ResourceTraversal;
19 import org.eclipse.core.runtime.*;
20 import org.eclipse.team.core.diff.IDiff;
21 import org.eclipse.team.core.diff.IThreeWayDiff;
22 import org.eclipse.team.core.mapping.IResourceDiff;
23 import org.eclipse.team.core.mapping.ISynchronizationContext;
24 import org.eclipse.team.ui.mapping.SynchronizationContentProvider;
25
26 /**
27  * A remote resource mapping context that wraps a synchronization context.
28  * This is used by the {@link SynchronizationContentProvider} to get the traversals
29  * for resource mappings. Since it is used to provide content, it avoids long running
30  * operations if possible.
31  *
32  * @since 3.2
33  */

34 public final class SynchronizationResourceMappingContext extends
35         RemoteResourceMappingContext {
36
37     private final ISynchronizationContext context;
38
39     /**
40      * Create a resource mapping context for the given synchronization context
41      * @param context the synchronization context
42      */

43     public SynchronizationResourceMappingContext(ISynchronizationContext context) {
44         this.context = context;
45     }
46
47     /* (non-Javadoc)
48      * @see org.eclipse.core.resources.mapping.RemoteResourceMappingContext#isThreeWay()
49      */

50     public boolean isThreeWay() {
51         return context.getType() == ISynchronizationContext.THREE_WAY;
52     }
53
54     /* (non-Javadoc)
55      * @see org.eclipse.core.resources.mapping.RemoteResourceMappingContext#hasRemoteChange(org.eclipse.core.resources.IResource, org.eclipse.core.runtime.IProgressMonitor)
56      */

57     public boolean hasRemoteChange(IResource resource, IProgressMonitor monitor) throws CoreException {
58         IDiff diff = context.getDiffTree().getDiff(resource);
59         if (diff instanceof IThreeWayDiff) {
60             IThreeWayDiff twd = (IThreeWayDiff) diff;
61             IDiff remote = twd.getRemoteChange();
62             return remote != null && remote.getKind() != IDiff.NO_CHANGE;
63         }
64         return diff != null && diff.getKind() != IDiff.NO_CHANGE;
65     }
66
67     /* (non-Javadoc)
68      * @see org.eclipse.core.resources.mapping.RemoteResourceMappingContext#hasLocalChange(org.eclipse.core.resources.IResource, org.eclipse.core.runtime.IProgressMonitor)
69      */

70     public boolean hasLocalChange(IResource resource, IProgressMonitor monitor) throws CoreException {
71         IDiff diff = context.getDiffTree().getDiff(resource);
72         if (diff instanceof IThreeWayDiff) {
73             IThreeWayDiff twd = (IThreeWayDiff) diff;
74             IDiff local = twd.getLocalChange();
75             return local != null && local.getKind() != IDiff.NO_CHANGE;
76         }
77         return false;
78     }
79
80     /* (non-Javadoc)
81      * @see org.eclipse.core.resources.mapping.RemoteResourceMappingContext#fetchRemoteContents(org.eclipse.core.resources.IFile, org.eclipse.core.runtime.IProgressMonitor)
82      */

83     public IStorage fetchRemoteContents(IFile file, IProgressMonitor monitor) throws CoreException {
84         IDiff diff = context.getDiffTree().getDiff(file);
85         if (diff instanceof IThreeWayDiff) {
86             IThreeWayDiff twd = (IThreeWayDiff) diff;
87             IDiff remote = twd.getRemoteChange();
88             if (remote instanceof IResourceDiff) {
89                 IResourceDiff rd = (IResourceDiff) remote;
90                 return rd.getAfterState().getStorage(monitor);
91             }
92         } else if (diff instanceof IResourceDiff) {
93             IResourceDiff rd = (IResourceDiff) diff;
94             return rd.getAfterState().getStorage(monitor);
95         }
96         return file;
97     }
98
99     /* (non-Javadoc)
100      * @see org.eclipse.core.resources.mapping.RemoteResourceMappingContext#fetchBaseContents(org.eclipse.core.resources.IFile, org.eclipse.core.runtime.IProgressMonitor)
101      */

102     public IStorage fetchBaseContents(IFile file, IProgressMonitor monitor) throws CoreException {
103         IDiff diff = context.getDiffTree().getDiff(file);
104         if (diff instanceof IThreeWayDiff) {
105             IThreeWayDiff twd = (IThreeWayDiff) diff;
106             IDiff remote = twd.getRemoteChange();
107             if (remote instanceof IResourceDiff) {
108                 IResourceDiff rd = (IResourceDiff) remote;
109                 return rd.getBeforeState().getStorage(monitor);
110             }
111             IDiff local = twd.getLocalChange();
112             if (local instanceof IResourceDiff) {
113                 IResourceDiff rd = (IResourceDiff) local;
114                 return rd.getBeforeState().getStorage(monitor);
115             }
116         }
117         return null;
118     }
119
120     public IResource[] fetchMembers(IContainer container, IProgressMonitor monitor) throws CoreException {
121         Set JavaDoc result = new HashSet JavaDoc();
122         IResource[] children = container.members();
123         for (int i = 0; i < children.length; i++) {
124             IResource resource = children[i];
125             result.add(resource);
126         }
127         IPath[] childPaths = context.getDiffTree().getChildren(container.getFullPath());
128         for (int i = 0; i < childPaths.length; i++) {
129             IPath path = childPaths[i];
130             IDiff delta = context.getDiffTree().getDiff(path);
131             IResource child;
132             if (delta == null) {
133                 // the path has descendent deltas so it must be a folder
134
if (path.segmentCount() == 1) {
135                     child = ((IWorkspaceRoot)container).getProject(path.lastSegment());
136                 } else {
137                     child = container.getFolder(new Path(path.lastSegment()));
138                 }
139             } else {
140                 child = context.getDiffTree().getResource(delta);
141             }
142             result.add(child);
143         }
144         return (IResource[]) result.toArray(new IResource[result.size()]);
145     }
146
147     public void refresh(ResourceTraversal[] traversals, int flags, IProgressMonitor monitor) throws CoreException {
148         //context.refresh(traversals, flags, monitor);
149
}
150
151     public ISynchronizationContext getSynchronizationContext() {
152         return context;
153     }
154
155     public IProject[] getProjects() {
156         Set JavaDoc projects = new HashSet JavaDoc();
157         IResource[] roots = context.getScope().getRoots();
158         for (int i = 0; i < roots.length; i++) {
159             IResource resource = roots[i];
160             projects.add(resource.getProject());
161         }
162         return (IProject[]) projects.toArray(new IProject[projects.size()]);
163     }
164
165 }
166
Popular Tags