1 11 package org.eclipse.team.internal.ccvs.ui.subscriber; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.List ; 16 17 import org.eclipse.compare.structuremergeviewer.IDiffElement; 18 import org.eclipse.core.resources.IProject; 19 import org.eclipse.core.resources.IResource; 20 import org.eclipse.core.resources.mapping.ResourceMappingContext; 21 import org.eclipse.core.runtime.IProgressMonitor; 22 import org.eclipse.core.runtime.OperationCanceledException; 23 import org.eclipse.jface.dialogs.IDialogConstants; 24 import org.eclipse.jface.dialogs.MessageDialog; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.swt.widgets.Shell; 27 import org.eclipse.team.core.TeamException; 28 import org.eclipse.team.core.mapping.provider.SynchronizationScopeManager; 29 import org.eclipse.team.core.synchronize.SyncInfo; 30 import org.eclipse.team.core.synchronize.SyncInfoSet; 31 import org.eclipse.team.internal.ccvs.core.*; 32 import org.eclipse.team.internal.ccvs.core.client.Command; 33 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 34 import org.eclipse.team.internal.ccvs.ui.*; 35 import org.eclipse.team.internal.ccvs.ui.Policy; 36 import org.eclipse.team.internal.ccvs.ui.operations.*; 37 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager; 38 import org.eclipse.team.internal.core.subscribers.ChangeSet; 39 import org.eclipse.team.internal.ui.Utils; 40 import org.eclipse.team.ui.synchronize.ISynchronizePageConfiguration; 41 42 public class WorkspaceCommitOperation extends CVSSubscriberOperation { 43 44 private String comment; 45 private SyncInfoSet syncSet; 46 private boolean override; 47 48 public WorkspaceCommitOperation(ISynchronizePageConfiguration configuration, IDiffElement[] elements, boolean override) { 49 super(configuration, elements); 50 this.override = override; 51 } 52 53 56 protected String getErrorTitle() { 57 return CVSUIMessages.CommitAction_commitFailed; 58 } 59 60 63 protected String getJobName() { 64 SyncInfoSet syncSet = getSyncInfoSet(); 65 return NLS.bind(CVSUIMessages.CommitAction_jobName, new String [] { new Integer (syncSet.size()).toString() }); 66 } 67 68 71 public boolean shouldRun() { 72 SyncInfoSet set = getSyncInfoSet(); 73 return !set.isEmpty(); 74 } 75 76 79 protected SyncInfoSet getSyncInfoSet() { 80 if (syncSet == null) { 81 syncSet = super.getSyncInfoSet(); 82 if (!promptForConflictHandling(syncSet)) { 83 syncSet.clear(); 84 return syncSet; 85 } 86 try { 87 if (!promptForUnaddedHandling(syncSet)) { 88 syncSet.clear(); 89 return syncSet; 90 } 91 } catch (CVSException e) { 92 Utils.handle(e); 93 syncSet.clear(); 94 } 95 } 96 return syncSet; 97 } 98 99 protected boolean promptForConflictHandling(SyncInfoSet syncSet) { 100 if (syncSet.hasConflicts() || syncSet.hasIncomingChanges()) { 101 if (override) { 102 switch (promptForConflicts(syncSet)) { 104 case 0: 105 break; 107 case 1: 108 return false; 110 case 2: 111 default: 112 return false; 114 } 115 } else { 116 syncSet.removeConflictingNodes(); 118 syncSet.removeIncomingNodes(); 119 } 120 } 121 return true; 122 } 123 124 128 public void runWithProjectRule(IProject project, SyncInfoSet syncSet, IProgressMonitor monitor) throws TeamException { 129 130 final SyncInfo[] changed = syncSet.getSyncInfos(); 131 if (changed.length == 0) return; 132 133 final List commits = new ArrayList (); final List additions = new ArrayList (); final List makeOutgoing = new ArrayList (); final List makeInSync = new ArrayList (); 142 for (int i = 0; i < changed.length; i++) { 143 SyncInfo changedNode = changed[i]; 144 int kind = changedNode.getKind(); 145 IResource resource = changedNode.getLocal(); 146 147 SyncInfo parent = getParent(changedNode); 150 if (parent != null) { 151 if (isOutOfSync(parent)) { 152 makeInSync.add(parent); 153 } 154 } 155 156 if (resource.getType() == IResource.FILE) { 157 commits.add(resource); 159 switch (kind & SyncInfo.DIRECTION_MASK) { 161 case SyncInfo.INCOMING: 162 makeOutgoing.add(changedNode); 164 break; 165 case SyncInfo.OUTGOING: 166 switch (kind & SyncInfo.CHANGE_MASK) { 167 case SyncInfo.ADDITION: 168 if (!isAdded(resource)) 170 additions.add(resource); 171 break; 172 case SyncInfo.DELETION: 173 break; 176 case SyncInfo.CHANGE: 177 break; 179 } 180 break; 181 case SyncInfo.CONFLICTING: 182 makeOutgoing.add(changedNode); 184 break; 185 } 186 } else { 187 if (((kind & SyncInfo.DIRECTION_MASK) == SyncInfo.OUTGOING) 188 && ((kind & SyncInfo.CHANGE_MASK) == SyncInfo.ADDITION)) { 189 additions.add(changedNode.getLocal()); 191 } else if (isOutOfSync(changedNode)) { 192 makeInSync.add(changedNode); 194 } 195 } 196 } 197 monitor.beginTask(null, 200); 198 199 if (makeInSync.size() > 0) { 200 makeInSync((SyncInfo[]) makeInSync.toArray(new SyncInfo[makeInSync.size()]), Policy.subMonitorFor(monitor, 25)); 201 } 202 203 if (makeOutgoing.size() > 0) { 204 makeOutgoing((SyncInfo[]) makeOutgoing.toArray(new SyncInfo[makeInSync.size()]), Policy.subMonitorFor(monitor, 25)); 205 } 206 207 if (additions.size() != 0) { 208 add(project, (IResource[])additions.toArray(new IResource[0]), Policy.subMonitorFor(monitor, 50)); 209 } 210 commit(project, (IResource[])commits.toArray(new IResource[commits.size()]), Policy.subMonitorFor(monitor, 100)); 211 } 212 213 private void commit(final IProject project, IResource[] commits, IProgressMonitor monitor) throws TeamException { 214 try { 215 CommitOperation commitOperation = new CommitOperation(getPart(), RepositoryProviderOperation.asResourceMappers(commits), 216 new Command.LocalOption[0], comment) { 217 protected ResourceMappingContext getResourceMappingContext() { 218 return new SingleProjectSubscriberContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(), false, project); 219 } 220 protected SynchronizationScopeManager createScopeManager(boolean consultModels) { 221 return new SingleProjectScopeManager(getJobName(), getSelectedMappings(), getResourceMappingContext(), consultModels, project); 222 } 223 }; 224 commitOperation 225 .run(monitor); 226 } catch (InvocationTargetException e) { 227 throw TeamException.asTeamException(e); 228 } catch (InterruptedException e) { 229 throw new OperationCanceledException(); 230 } 231 } 232 233 private void add(final IProject project, IResource[] additions, IProgressMonitor monitor) throws TeamException { 234 try { 235 new AddOperation(getPart(), RepositoryProviderOperation.asResourceMappers(additions)) { 236 protected ResourceMappingContext getResourceMappingContext() { 237 return new SingleProjectSubscriberContext(CVSProviderPlugin.getPlugin().getCVSWorkspaceSubscriber(), false, project); 238 } 239 protected SynchronizationScopeManager createScopeManager(boolean consultModels) { 240 return new SingleProjectScopeManager(getJobName(), getSelectedMappings(), getResourceMappingContext(), consultModels, project); 241 } 242 }.run(monitor); 243 } catch (InvocationTargetException e1) { 244 throw TeamException.asTeamException(e1); 245 } catch (InterruptedException e1) { 246 throw new OperationCanceledException(); 247 } 248 } 249 250 255 protected int promptForConflicts(SyncInfoSet syncSet) { 256 String [] buttons = new String [] {IDialogConstants.YES_LABEL, IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL}; 257 String question = CVSUIMessages.CommitSyncAction_questionRelease; 258 String title = CVSUIMessages.CommitSyncAction_titleRelease; 259 String [] tips = new String [] { 260 CVSUIMessages.CommitSyncAction_releaseAll, 261 CVSUIMessages.CommitSyncAction_releasePart, 262 CVSUIMessages.CommitSyncAction_cancelRelease 263 }; 264 Shell shell = getShell(); 265 final ToolTipMessageDialog dialog = new ToolTipMessageDialog(shell, title, null, question, MessageDialog.QUESTION, buttons, tips, 0); 266 shell.getDisplay().syncExec(new Runnable () { 267 public void run() { 268 dialog.open(); 269 } 270 }); 271 return dialog.getReturnCode(); 272 } 273 274 279 protected String promptForComment(RepositoryManager manager, IResource[] resourcesToCommit) { 280 String proposedComment = getProposedComment(resourcesToCommit); 281 return manager.promptForComment(getShell(), resourcesToCommit, proposedComment); 282 } 283 284 private String getProposedComment(IResource[] resourcesToCommit) { 285 StringBuffer comment = new StringBuffer (); 286 ChangeSet[] sets = CVSUIPlugin.getPlugin().getChangeSetManager().getSortedSets(); 287 int numMatchedSets = 0; 288 for (int i = 0; i < sets.length; i++) { 289 ChangeSet set = sets[i]; 290 if (containsOne(set, resourcesToCommit)) { 291 if(numMatchedSets > 0) comment.append(System.getProperty("line.separator")); comment.append(set.getComment()); 293 numMatchedSets++; 294 } 295 } 296 return comment.toString(); 297 } 298 299 private boolean containsOne(ChangeSet set, IResource[] resourcesToCommit) { 300 for (int j = 0; j < resourcesToCommit.length; j++) { 301 IResource resource = resourcesToCommit[j]; 302 if (set.contains(resource)) { 303 return true; 304 } 305 } 306 return false; 307 } 308 309 protected IResource[] promptForResourcesToBeAdded(RepositoryManager manager, IResource[] unadded) { 310 return manager.promptForResourcesToBeAdded(getShell(), unadded); 311 } 312 313 private boolean promptForUnaddedHandling(SyncInfoSet syncSet) throws CVSException { 314 if (syncSet.isEmpty()) return false; 315 316 IResource[] unadded = getUnaddedResources(syncSet); 318 319 RepositoryManager manager = CVSUIPlugin.getPlugin().getRepositoryManager(); 321 IResource[] toBeAdded = promptForResourcesToBeAdded(manager, unadded); 322 if (toBeAdded == null) return false; comment = promptForComment(manager, syncSet.getResources()); 324 if (comment == null) return false; 326 if (unadded != null && unadded.length > 0) { 328 List resourcesToRemove = new ArrayList (unadded.length); 329 for (int i = 0; i < unadded.length; i++) { 330 IResource unaddedResource = unadded[i]; 331 boolean included = false; 332 for (int j = 0; j < toBeAdded.length; j++) { 333 IResource resourceToAdd = toBeAdded[j]; 334 if (unaddedResource.equals(resourceToAdd)) { 335 included = true; 336 break; 337 } 338 } 339 if (!included) 340 resourcesToRemove.add(unaddedResource); 341 } 342 syncSet.removeAll((IResource[]) resourcesToRemove.toArray(new IResource[resourcesToRemove.size()])); 343 } 344 return true; 345 } 346 347 private IResource[] getUnaddedResources(SyncInfoSet syncSet) throws CVSException { 348 IResource[] resources = syncSet.getResources(); 351 List result = new ArrayList (); 352 for (int i = 0; i < resources.length; i++) { 353 IResource resource = resources[i]; 354 if (!isAdded(resource)) { 355 result.add(resource); 356 } 357 } 358 return (IResource[]) result.toArray(new IResource[result.size()]); 359 } 360 361 private boolean isAdded(IResource resource) throws CVSException { 362 ICVSResource cvsResource = CVSWorkspaceRoot.getCVSResourceFor(resource); 363 if (cvsResource.isFolder()) { 364 return ((ICVSFolder)cvsResource).isCVSFolder(); 365 } else { 366 return cvsResource.isManaged(); 367 } 368 } 369 370 } 371 | Popular Tags |