KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > client > Update


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.core.client;
12
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.List JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.osgi.util.NLS;
22 import org.eclipse.team.core.RepositoryProvider;
23 import org.eclipse.team.internal.ccvs.core.*;
24 import org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener;
25 import org.eclipse.team.internal.ccvs.core.client.listeners.UpdateListener;
26
27 public class Update extends Command {
28     /*** Local options: specific to update ***/
29     public static final LocalOption CLEAR_STICKY = new LocalOption("-A"); //$NON-NLS-1$
30
public static final LocalOption IGNORE_LOCAL_CHANGES = new LocalOption("-C"); //$NON-NLS-1$
31
public static final LocalOption RETRIEVE_ABSENT_DIRECTORIES = new LocalOption("-d"); //$NON-NLS-1$
32
public static final LocalOption JOIN = new LocalOption("-j"); //$NON-NLS-1$
33

34     /*** Default command output listener ***/
35     private static final ICommandOutputListener DEFAULT_OUTPUT_LISTENER = new UpdateListener(null);
36     
37     /*** File information status returned from update ***/
38     public static final int STATE_NONE = 0; // no state information available
39
public static final int STATE_ADDED_LOCAL = 1; // new file locally that was added but not comitted to server yet
40
public static final int STATE_UNKOWN = 2; // new file locally but not added to server
41
public static final int STATE_REMOTE_CHANGES = 3; // remote changes to an unmodified local file
42
public static final int STATE_DELETED = 4; // removed locally but still exists on the server
43
public static final int STATE_MODIFIED = 5; // modified locally
44
public static final int STATE_CONFLICT = 6; // modified locally and on the server but cannot be auto-merged
45
public static final int STATE_MERGEABLE_CONFLICT = 7; // modified locally and on the server but can be auto-merged
46

47     /**
48      * Makes a -r or -D or -A option for a tag.
49      * Valid for: checkout export history rdiff update
50      */

51     public static LocalOption makeTagOption(CVSTag tag) {
52         int type = tag.getType();
53         switch (type) {
54             case CVSTag.HEAD:
55                 return CLEAR_STICKY;
56             default:
57                 return Command.makeTagOption(tag);
58         }
59     }
60     
61     protected Update() { }
62     protected String JavaDoc getRequestId() {
63         return "update"; //$NON-NLS-1$
64
}
65     
66     protected ICommandOutputListener getDefaultCommandOutputListener() {
67         return DEFAULT_OUTPUT_LISTENER;
68     }
69     
70     protected ICVSResource[] sendLocalResourceState(Session session, GlobalOption[] globalOptions,
71         LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor)
72         throws CVSException {
73         
74         // Send all folders that are already managed to the server
75
// even folders that are empty
76
sendFileStructure(session, resources, localOptions, true, monitor);
77         return resources;
78     }
79     
80     /**
81      * On successful finish, prune empty directories if the -P or -D option was specified.
82      */

83     protected IStatus commandFinished(Session session, GlobalOption[] globalOptions,
84         LocalOption[] localOptions, ICVSResource[] resources, IProgressMonitor monitor,
85         IStatus status) throws CVSException {
86         // If we didn't succeed, don't do any post processing
87
if (status.getCode() == CVSStatus.SERVER_ERROR) {
88             return status;
89         }
90
91         // If we are pruning (-P), then prune empty directories
92
// Note, the CVS spec says that Date (-D) and version (-r) updates
93
// should automatically prune but this is a problem for remote CVS handles
94
// which fetch a level at a time
95
if (PRUNE_EMPTY_DIRECTORIES.isElementOf(localOptions)) {
96             // Delete empty directories
97
new PruneFolderVisitor().visit(session, resources);
98             
99         }
100         return status;
101     }
102     
103     protected LocalOption[] filterLocalOptions(Session session, GlobalOption[] globalOptions, LocalOption[] localOptions) {
104         List JavaDoc newOptions = new ArrayList JavaDoc(Arrays.asList(localOptions));
105         
106         if (shouldRetrieveAbsentDirectories(session) && ! RETRIEVE_ABSENT_DIRECTORIES.isElementOf(localOptions)) {
107             newOptions.add(Update.RETRIEVE_ABSENT_DIRECTORIES);
108         }
109
110         // Prune empty directories if pruning is enabled and the command in not being run in non-update mode
111
if (CVSProviderPlugin.getPlugin().getPruneEmptyDirectories() && ! PRUNE_EMPTY_DIRECTORIES.isElementOf(localOptions)) {
112             if (! DO_NOT_CHANGE.isElementOf(globalOptions)) {
113                 newOptions.add(Command.PRUNE_EMPTY_DIRECTORIES);
114             }
115         }
116         localOptions = (LocalOption[]) newOptions.toArray(new LocalOption[newOptions.size()]);
117         return super.filterLocalOptions(session, globalOptions, localOptions);
118     }
119     
120     /**
121      * Return whether the update command should retrieve absent directories.
122      * @param session the session
123      * @return whether the update command should retrieve absent directories
124      */

125     protected boolean shouldRetrieveAbsentDirectories(Session session) {
126         // Look for absent directories if enabled and the option is not already included
127
IResource resource = null;
128         RepositoryProvider provider = null;
129         // If there is a provider, use the providers setting
130
try {
131             resource = session.getLocalRoot().getIResource();
132             if (resource != null) {
133                 provider = RepositoryProvider.getProvider(resource.getProject(), CVSProviderPlugin.getTypeId());
134                 if (provider != null) {
135                     if (((CVSTeamProvider)provider).getFetchAbsentDirectories()) {
136                         return true;
137                     }
138                 }
139             }
140         } catch (CVSException e) {
141             CVSProviderPlugin.log(e);
142         }
143         // If there is no provider, use the global setting
144
if (provider == null) {
145             if (CVSProviderPlugin.getPlugin().getFetchAbsentDirectories()) {
146                 return true;
147             }
148         }
149         return false;
150     }
151     
152     /**
153      * We allow unmanaged resources as long as there parents are managed.
154      *
155      * @see Command#checkResourcesManaged(Session, ICVSResource[])
156      */

157     protected void checkResourcesManaged(Session session, ICVSResource[] resources) throws CVSException {
158         for (int i = 0; i < resources.length; ++i) {
159             ICVSFolder folder;
160             if (resources[i].isFolder()) {
161                 if (((ICVSFolder)resources[i]).isCVSFolder()) {
162                     folder = (ICVSFolder)resources[i];
163                 } else {
164                     folder = resources[i].getParent();
165                 }
166             }
167             else {
168                 folder = resources[i].getParent();
169             }
170             if (folder==null || (!folder.isCVSFolder() && folder.exists())) {
171                 if (folder == null)
172                     folder = (ICVSFolder)resources[i];
173                 IStatus status = new CVSStatus(IStatus.ERROR,CVSStatus.ERROR,NLS.bind(CVSMessages.Command_argumentNotManaged, new String JavaDoc[] { folder.getName() }),session.getLocalRoot());
174                 throw new CVSException(status);
175             }
176         }
177     }
178
179     /**
180      * @see org.eclipse.team.internal.ccvs.core.client.Command#doExecute(org.eclipse.team.internal.ccvs.core.client.Session, org.eclipse.team.internal.ccvs.core.client.Command.GlobalOption, org.eclipse.team.internal.ccvs.core.client.Command.LocalOption, java.lang.String, org.eclipse.team.internal.ccvs.core.client.listeners.ICommandOutputListener, org.eclipse.core.runtime.IProgressMonitor)
181      */

182     protected IStatus doExecute(
183             Session session,
184             GlobalOption[] globalOptions,
185             LocalOption[] localOptions,
186             String JavaDoc[] arguments,
187             ICommandOutputListener listener,
188             IProgressMonitor monitor)
189             throws CVSException {
190             
191         session.setIgnoringLocalChanges(IGNORE_LOCAL_CHANGES.isElementOf(localOptions));
192         try {
193             return super.doExecute(
194                 session,
195                 globalOptions,
196                 localOptions,
197                 arguments,
198                 listener,
199                 monitor);
200         } finally {
201             session.setIgnoringLocalChanges(false);
202         }
203
204     }
205
206 }
207
Popular Tags