KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > operations > CacheTreeContentsOperation


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.ccvs.ui.operations;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.mapping.ResourceMapping;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.osgi.util.NLS;
21 import org.eclipse.team.core.TeamException;
22 import org.eclipse.team.core.diff.IDiff;
23 import org.eclipse.team.core.diff.IThreeWayDiff;
24 import org.eclipse.team.core.history.IFileRevision;
25 import org.eclipse.team.core.mapping.IResourceDiffTree;
26 import org.eclipse.team.core.variants.IResourceVariant;
27 import org.eclipse.team.internal.ccvs.core.*;
28 import org.eclipse.team.internal.ccvs.core.client.*;
29 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
30 import org.eclipse.team.internal.ccvs.core.client.listeners.IUpdateMessageListener;
31 import org.eclipse.team.internal.ccvs.core.client.listeners.UpdateListener;
32 import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;
33 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
34 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
35 import org.eclipse.team.internal.ui.Utils;
36 import org.eclipse.ui.IWorkbenchPart;
37
38 /**
39  * Abstract operation for caching the contents for any files
40  * in a particular remote tree that differ from the local contents.*
41  */

42 public abstract class CacheTreeContentsOperation extends SingleCommandOperation {
43
44     private final IResourceDiffTree tree;
45
46     public CacheTreeContentsOperation(IWorkbenchPart part, ResourceMapping[] mappings, IResourceDiffTree tree) {
47         super(part, mappings, Command.NO_LOCAL_OPTIONS);
48         this.tree = tree;
49     }
50     
51     /* (non-Javadoc)
52      * @see org.eclipse.team.internal.ccvs.ui.operations.SingleCommandOperation#execute(org.eclipse.team.internal.ccvs.core.CVSTeamProvider, org.eclipse.core.resources.IResource[], boolean, org.eclipse.core.runtime.IProgressMonitor)
53      */

54     protected void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
55         IResource[] files = getFilesWithUncachedContents(resources, recurse);
56         if (files.length > 0)
57             super.execute(provider, files, recurse, monitor);
58     }
59     
60     private IResource[] getFilesWithUncachedContents(IResource[] resources, boolean recurse) {
61         ArrayList JavaDoc result = new ArrayList JavaDoc();
62         for (int i = 0; i < resources.length; i++) {
63             IResource resource = resources[i];
64             IDiff[] nodes = tree.getDiffs(resource, recurse ? IResource.DEPTH_INFINITE: IResource.DEPTH_ONE);
65             for (int j = 0; j < nodes.length; j++) {
66                 IDiff node = nodes[j];
67                 if (needsContents(node)) {
68                     result.add(tree.getResource(node));
69                 }
70             }
71         }
72         return (IResource[]) result.toArray(new IResource[result.size()]);
73     }
74
75     protected boolean needsContents(IDiff node) {
76         if (node instanceof IThreeWayDiff) {
77             IThreeWayDiff twd = (IThreeWayDiff) node;
78             IResource local = getTree().getResource(node);
79             IFileRevision remote = getRemoteFileState(twd);
80             if (remote != null) {
81                 IResourceVariant variant = (IResourceVariant)Utils.getAdapter(remote, IResourceVariant.class);
82                 if (local.getType() == IResource.FILE
83                         && isEnabledForDirection(twd.getDirection())
84                         && variant instanceof RemoteFile) {
85                     RemoteFile rf = (RemoteFile) variant;
86                     if (!rf.isContentsCached()) {
87                         return true;
88                     }
89                 }
90             }
91         }
92         return false;
93     }
94     
95     /**
96      * Get the remote file state that is of interest.
97      * @param twd a three way diff
98      * @return the remote file state that is of interest
99      */

100     protected abstract IFileRevision getRemoteFileState(IThreeWayDiff twd);
101
102     /**
103      * Return whether the direction is of interest.
104      * @param direction the direction of a diff
105      * @return whether the direction is of interest
106      */

107     protected abstract boolean isEnabledForDirection(int direction);
108
109     /* (non-Javadoc)
110      *
111      * Use a local root that is really the base tree so we can cache
112      * the base contents without affecting the local contents.
113      *
114      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getLocalRoot(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
115      */

116     protected ICVSFolder getLocalRoot(CVSTeamProvider provider)
117             throws CVSException {
118         try {
119             ICVSRemoteResource tree = buildTree(provider);
120             return (ICVSFolder)tree;
121         } catch (TeamException e) {
122             throw CVSException.wrapException(e);
123         }
124     }
125
126     protected abstract ICVSRemoteResource buildTree(CVSTeamProvider provider) throws TeamException;
127     
128     /* (non-Javadoc)
129      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getCVSArguments(org.eclipse.core.resources.IResource[])
130      */

131     protected ICVSResource[] getCVSArguments(Session session, IResource[] resources) {
132         List JavaDoc result = new ArrayList JavaDoc();
133         for (int i = 0; i < resources.length; i++) {
134             IResource resource = resources[i];
135             try {
136                 ICVSResource file = session.getLocalRoot().getChild(resource.getProjectRelativePath().toString());
137                 result.add(file);
138             } catch (CVSException e) {
139                 // Log and continue
140
CVSUIPlugin.log(e);
141             }
142         }
143
144         return (ICVSResource[]) result.toArray(new ICVSResource[result.size()]);
145     }
146     
147     /* (non-Javadoc)
148      * @see org.eclipse.team.internal.ccvs.ui.operations.SingleCommandOperation#executeCommand(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.CVSTeamProvider, org.eclipse.team.internal.ccvs.core.ICVSResource[], boolean, org.eclipse.core.runtime.IProgressMonitor)
149      */

150     protected IStatus executeCommand(Session session, CVSTeamProvider provider, ICVSResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
151         return Command.UPDATE.execute(
152                 session,
153                 Command.NO_GLOBAL_OPTIONS,
154                 getLocalOptions(true),
155                 resources,
156                 new UpdateListener(new IUpdateMessageListener() {
157                     public void fileInformation(int type, ICVSFolder parent, String JavaDoc filename) {
158                         // Do nothing
159
}
160                     public void fileDoesNotExist(ICVSFolder parent, String JavaDoc filename) {
161                         // Do nothing
162
}
163                     public void directoryInformation(ICVSFolder commandRoot, String JavaDoc path,
164                             boolean newDirectory) {
165                         // Do nothing
166
}
167                     public void directoryDoesNotExist(ICVSFolder commandRoot, String JavaDoc path) {
168                         // Do nothing
169
}
170                 }),
171                 monitor);
172     }
173
174     /* (non-Javadoc)
175      * @see org.eclipse.team.internal.ccvs.ui.operations.SingleCommandOperation#getLocalOptions(boolean)
176      */

177     protected LocalOption[] getLocalOptions(boolean recurse) {
178         return Update.IGNORE_LOCAL_CHANGES.addTo(super.getLocalOptions(recurse));
179     }
180     
181     /* (non-Javadoc)
182      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getTaskName(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
183      */

184     protected String JavaDoc getTaskName(CVSTeamProvider provider) {
185         return NLS.bind(CVSUIMessages.CacheTreeContentsOperation_0, new String JavaDoc[] {provider.getProject().getName()});
186     }
187
188     /* (non-Javadoc)
189      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
190      */

191     protected String JavaDoc getTaskName() {
192         return CVSUIMessages.CacheTreeContentsOperation_1;
193     }
194
195     /**
196      * Return the diff tree whose contents are being cached
197      * @return
198      */

199     protected IResourceDiffTree getTree() {
200         return tree;
201     }
202     
203     /* (non-Javadoc)
204      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#consultModelsForMappings()
205      */

206     public boolean consultModelsForMappings() {
207         return false;
208     }
209     
210     /* (non-Javadoc)
211      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#isReportableError(org.eclipse.core.runtime.IStatus)
212      */

213     protected boolean isReportableError(IStatus status) {
214         return super.isReportableError(status) && status.getSeverity() == IStatus.ERROR;
215     }
216
217 }
218
Popular Tags