1 11 package org.eclipse.team.internal.ccvs.ui.operations; 12 13 import java.util.Collections ; 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.Set ; 19 import java.util.SortedSet ; 20 import java.util.TreeSet ; 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 42 public class AddOperation extends RepositoryProviderOperation { 43 44 private Map fModesForExtensions; 45 private Map 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 modes) { 54 fModesForExtensions= modes; 55 } 56 57 public void addModesForNames(Map modes) { 58 fModesForFiles= modes; 59 } 60 63 protected void execute(CVSTeamProvider provider, IResource[] resources, boolean recurse, IProgressMonitor monitor) throws CVSException, InterruptedException { 64 if (resources.length == 0) 65 return; 66 add(provider, resources, recurse ? IResource.DEPTH_INFINITE : IResource.DEPTH_ONE, monitor); 67 } 68 69 72 protected String getTaskName() { 73 return CVSUIMessages.AddAction_adding; 74 } 75 76 79 protected String getTaskName(CVSTeamProvider provider) { 80 return NLS.bind(CVSUIMessages.AddOperation_0, new String [] { provider.getProject().getName() }); 81 } 82 83 106 private void add(CVSTeamProvider provider, IResource[] resources, int depth, IProgressMonitor progress) throws CVSException { 107 108 final SortedSet folders = new TreeSet (); 112 final Map files = new HashMap (); 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 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 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 if (! isManaged(mResource) && (currentResource.equals(resource) || ! mResource.isIgnored())) { 139 if (resource.getType() == IResource.FILE) { 140 KSubstOption ksubst= getKSubstOption((IFile)resource); 141 Set set = (Set ) files.get(ksubst); 142 if (set == null) { 143 set = new HashSet (); 144 files.put(ksubst, set); 145 } 146 set.add(mResource); 147 } else if (!isManagedProject(resource, mResource)){ 148 folders.add(mResource); 149 } 150 } 151 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 (eHolder[0] != null) 169 throw eHolder[0]; 170 171 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 ); 176 session.open(Policy.subMonitorFor(progress, 2), true ); 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 it = files.entrySet().iterator(); it.hasNext();) { 193 Map.Entry entry = (Map.Entry ) it.next(); 194 final KSubstOption ksubst = (KSubstOption) entry.getKey(); 195 final Set set = (Set ) entry.getValue(); 196 Session session = new Session(getRemoteLocation(provider), getLocalRoot(provider), true ); 197 session.open(Policy.subMonitorFor(progress, 2), true ); 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 221 protected boolean isManagedProject(IResource resource, ICVSResource resource2) throws CVSException { 222 return resource.getType() == IResource.PROJECT && ((ICVSFolder)resource2).isCVSFolder(); 223 } 224 225 228 protected boolean isManaged(ICVSResource cvsResource) throws CVSException { 229 return cvsResource.isManaged() && (!cvsResource.isFolder() || ((ICVSFolder)cvsResource).isCVSFolder()); 230 } 231 232 235 protected String getErrorMessage(IStatus[] failures, int totalOperations) { 236 return CVSUIMessages.AddAction_addFailed; 237 } 238 239 protected KSubstOption getKSubstOption(IFile file) { 240 final String extension= file.getFileExtension(); 241 final Integer mode; 242 if (extension == null) { 243 mode= (Integer )fModesForFiles.get(file.getName()); 244 } else { 245 mode= (Integer )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 |