KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collections JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.HashSet JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import java.util.SortedSet JavaDoc;
20 import java.util.TreeSet JavaDoc;
21
22 import org.eclipse.core.resources.*;
23 import org.eclipse.core.resources.mapping.ResourceMapping;
24 import org.eclipse.core.runtime.*;
25 import org.eclipse.osgi.util.NLS;
26 import org.eclipse.team.core.Team;
27 import org.eclipse.team.core.TeamException;
28 import org.eclipse.team.internal.ccvs.core.*;
29 import org.eclipse.team.internal.ccvs.core.client.Command;
30 import org.eclipse.team.internal.ccvs.core.client.Session;
31 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption;
32 import org.eclipse.team.internal.ccvs.core.client.Command.LocalOption;
33 import org.eclipse.team.internal.ccvs.core.connection.CVSServerException;
34 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
35 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
36 import org.eclipse.team.internal.ccvs.ui.Policy;
37 import org.eclipse.ui.IWorkbenchPart;
38
39 /**
40  * Performs a "cvs add"
41  */

42 public class AddOperation extends RepositoryProviderOperation {
43         
44     private Map JavaDoc fModesForExtensions;
45     private Map JavaDoc fModesForFiles;
46     
47     public AddOperation(IWorkbenchPart part, ResourceMapping[] mappers) {
48         super(part, mappers);
49         fModesForExtensions= Collections.EMPTY_MAP;
50         fModesForFiles= Collections.EMPTY_MAP;
51     }
52
53     public void addModesForExtensions(Map JavaDoc modes) {
54         fModesForExtensions= modes;
55     }
56     
57     public void addModesForNames(Map JavaDoc modes) {
58         fModesForFiles= modes;
59     }
60     /* (non-Javadoc)
61      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#execute(org.eclipse.team.internal.ccvs.core.CVSTeamProvider, org.eclipse.core.resources.IResource[], org.eclipse.core.runtime.IProgressMonitor)
62      */

63     protected void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException JavaDoc {
64         if (resources.length == 0)
65             return;
66         add(provider, resources, recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE, monitor);
67     }
68     
69     /* (non-Javadoc)
70      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
71      */

72     protected String JavaDoc getTaskName() {
73         return CVSUIMessages.AddAction_adding;
74     }
75     
76     /* (non-Javadoc)
77      * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryProviderOperation#getTaskName(org.eclipse.team.internal.ccvs.core.CVSTeamProvider)
78      */

79     protected String JavaDoc getTaskName(CVSTeamProvider provider) {
80         return NLS.bind(CVSUIMessages.AddOperation_0, new String JavaDoc[] { provider.getProject().getName() });
81     }
82     
83     /*
84      * Add the given resources to the project.
85      * <p>
86      * The sematics follow that of CVS in the sense that any folders
87      * being added are created remotely as a result of this operation
88      * while files are created remotely on the next commit.
89      * </p>
90      * <p>
91      * This method uses the team file type registry to determine the type
92      * of added files. If the extension of the file is not in the registry,
93      * the file is assumed to be binary.
94      * </p>
95      * <p>
96      * NOTE: for now we do three operations: one each for folders, text files and binary files.
97      * We should optimize this when time permits to either use one operations or defer server
98      * contact until the next commit.
99      * </p>
100      *
101      * <p>
102      * There are special semantics for adding the project itself to the repo. In this case, the project
103      * must be included in the resources array.
104      * </p>
105      */

106     private void add(CVSTeamProvider provider, IResource[] resources, int depth, IProgressMonitor progress) throws CVSException {
107         
108         // Visit the children of the resources using the depth in order to
109
// determine which folders, text files and binary files need to be added
110
// A TreeSet is needed for the folders so they are in the right order (i.e. parents created before children)
111
final SortedSet JavaDoc folders = new TreeSet JavaDoc();
112         // Sets are required for the files to ensure that files will not appear twice if there parent was added as well
113
// and the depth isn't zero
114
final Map JavaDoc /* from KSubstOption to Set */ files = new HashMap JavaDoc();
115         final CVSException[] eHolder = new CVSException[1];
116         for (int i=0; i<resources.length; i++) {
117             
118             final IResource currentResource = resources[i];
119             
120             try {
121                 // Auto-add parents if they are not already managed
122
IContainer parent = currentResource.getParent();
123                 ICVSResource cvsParentResource = CVSWorkspaceRoot.getCVSResourceFor(parent);
124                 while (parent.getType() != IResource.ROOT && parent.getType() != IResource.PROJECT && ! isManaged(cvsParentResource)) {
125                     folders.add(cvsParentResource);
126                     parent = parent.getParent();
127                     cvsParentResource = cvsParentResource.getParent();
128                 }
129                     
130                 // Auto-add children
131
final TeamException[] exception = new TeamException[] { null };
132                 currentResource.accept(new IResourceVisitor() {
133                     public boolean visit(IResource resource) {
134                         try {
135                             ICVSResource mResource = CVSWorkspaceRoot.getCVSResourceFor(resource);
136                             // Add the resource is its not already managed and it was either
137
// added explicitly (is equal currentResource) or is not ignored
138
if (! isManaged(mResource) && (currentResource.equals(resource) || ! mResource.isIgnored())) {
139                                 if (resource.getType() == IResource.FILE) {
140                                     KSubstOption ksubst= getKSubstOption((IFile)resource);
141                                     Set JavaDoc set = (Set JavaDoc) files.get(ksubst);
142                                     if (set == null) {
143                                         set = new HashSet JavaDoc();
144                                         files.put(ksubst, set);
145                                     }
146                                     set.add(mResource);
147                                 } else if (!isManagedProject(resource, mResource)){
148                                     folders.add(mResource);
149                                 }
150                             }
151                             // Always return true and let the depth determine if children are visited
152
return true;
153                         } catch (CVSException e) {
154                             exception[0] = e;
155                             return false;
156                         }
157                     }
158
159                 }, depth, false);
160                 if (exception[0] != null) {
161                     throw exception[0];
162                 }
163             } catch (CoreException e) {
164                 throw CVSException.wrapException(e);
165             }
166         }
167         // If an exception occured during the visit, throw it here
168
if (eHolder[0] != null)
169             throw eHolder[0];
170         
171         // Add the folders, followed by files!
172
progress.beginTask(null, files.size() * 10 + (folders.isEmpty() ? 0 : 10));
173         try {
174             if (!folders.isEmpty()) {
175                 Session session = new Session(getRemoteLocation(provider), getLocalRoot(provider), true /* output to console */);
176                 session.open(Policy.subMonitorFor(progress, 2), true /* open for modification */);
177                 try {
178                     IStatus status = Command.ADD.execute(
179                         session,
180                         Command.NO_GLOBAL_OPTIONS,
181                         Command.NO_LOCAL_OPTIONS,
182                         (ICVSResource[])folders.toArray(new ICVSResource[folders.size()]),
183                         null,
184                         Policy.subMonitorFor(progress, 8));
185                     if (status.getCode() == CVSStatus.SERVER_ERROR) {
186                         throw new CVSServerException(status);
187                     }
188                 } finally {
189                     session.close();
190                 }
191             }
192             for (Iterator JavaDoc it = files.entrySet().iterator(); it.hasNext();) {
193                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
194                 final KSubstOption ksubst = (KSubstOption) entry.getKey();
195                 final Set JavaDoc set = (Set JavaDoc) entry.getValue();
196                 Session session = new Session(getRemoteLocation(provider), getLocalRoot(provider), true /* output to console */);
197                 session.open(Policy.subMonitorFor(progress, 2), true /* open for modification */);
198                 try {
199                     IStatus status = Command.ADD.execute(
200                         session,
201                         Command.NO_GLOBAL_OPTIONS,
202                         new LocalOption[] { ksubst },
203                         (ICVSResource[])set.toArray(new ICVSResource[set.size()]),
204                         null,
205                         Policy.subMonitorFor(progress, 8));
206                     if (status.getCode() == CVSStatus.SERVER_ERROR) {
207                         throw new CVSServerException(status);
208                     }
209                 } finally {
210                     session.close();
211                 }
212             }
213         } finally {
214             progress.done();
215         }
216     }
217     
218     /*
219      * Return true if the resource is a project that is already a CVS folder
220      */

221     protected boolean isManagedProject(IResource resource, ICVSResource resource2) throws CVSException {
222         return resource.getType() == IResource.PROJECT && ((ICVSFolder)resource2).isCVSFolder();
223     }
224
225     /*
226      * Consider a folder managed only if it's also a CVS folder
227      */

228     protected boolean isManaged(ICVSResource cvsResource) throws CVSException {
229         return cvsResource.isManaged() && (!cvsResource.isFolder() || ((ICVSFolder)cvsResource).isCVSFolder());
230     }
231
232     /* (non-Javadoc)
233      * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getErrorMessage(org.eclipse.core.runtime.IStatus[], int)
234      */

235     protected String JavaDoc getErrorMessage(IStatus[] failures, int totalOperations) {
236         return CVSUIMessages.AddAction_addFailed;
237     }
238     
239     protected KSubstOption getKSubstOption(IFile file) {
240         final String JavaDoc extension= file.getFileExtension();
241         final Integer JavaDoc mode;
242         if (extension == null) {
243             mode= (Integer JavaDoc)fModesForFiles.get(file.getName());
244         } else {
245             mode= (Integer JavaDoc)fModesForExtensions.get(extension);
246         }
247         if (mode != null) {
248             return mode.intValue() == Team.BINARY ? Command.KSUBST_BINARY : KSubstOption.getDefaultTextMode();
249         } else {
250             return KSubstOption.fromFile(file);
251         }
252     }
253
254 }
255
Popular Tags