1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.ArrayList ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 23 import org.eclipse.core.resources.IFile; 24 import org.eclipse.core.resources.IResource; 25 import org.eclipse.core.resources.IResourceVisitor; 26 import org.eclipse.core.runtime.CoreException; 27 import org.eclipse.core.runtime.IProgressMonitor; 28 import org.eclipse.core.runtime.IStatus; 29 import org.eclipse.core.runtime.MultiStatus; 30 import org.eclipse.jface.dialogs.Dialog; 31 import org.eclipse.jface.dialogs.MessageDialog; 32 import org.eclipse.jface.operation.IRunnableWithProgress; 33 import org.eclipse.jface.resource.ImageDescriptor; 34 import org.eclipse.jface.wizard.IWizardPage; 35 import org.eclipse.jface.wizard.Wizard; 36 import org.eclipse.swt.custom.BusyIndicator; 37 import org.eclipse.team.core.RepositoryProvider; 38 import org.eclipse.team.core.TeamException; 39 import org.eclipse.team.internal.ccvs.core.CVSException; 40 import org.eclipse.team.internal.ccvs.core.CVSProviderPlugin; 41 import org.eclipse.team.internal.ccvs.core.CVSStatus; 42 import org.eclipse.team.internal.ccvs.core.CVSTeamProvider; 43 import org.eclipse.team.internal.ccvs.core.ICVSFile; 44 import org.eclipse.team.internal.ccvs.core.client.Command.KSubstOption; 45 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 46 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo; 47 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin; 48 import org.eclipse.team.internal.ccvs.ui.ICVSUIConstants; 49 import org.eclipse.team.internal.ccvs.ui.Policy; 50 51 62 public class KSubstWizard extends Wizard { 63 private KSubstOption defaultKSubst; 64 65 private final IResource[] resources; 66 private final int depth; 67 private List changeList = null; 68 private KSubstOption changeOption = null; 69 70 private KSubstWizardSelectionPage mainPage; 71 private KSubstWizardSummaryPage summaryPage; 72 private KSubstWizardSharedFilesPage sharedFilesPage; 73 private KSubstWizardDirtyFilesPage dirtyFilesPage; 74 75 private Dialog parentDialog; 76 77 private KSubstWizardCommitCommentPage commitCommentPage; 78 79 public class KSubstChangeElement { 80 public static final int ADDED_FILE = 1; 81 public static final int CHANGED_FILE = 2; 82 public static final int UNCHANGED_FILE = 4; 83 84 private IFile file; 85 private int classification; 86 private boolean excluded; 87 private KSubstOption fromKSubst; 88 private KSubstOption toKSubst; 89 90 private KSubstChangeElement(IFile file, int classification, boolean excluded, KSubstOption fromKSubst, KSubstOption toKSubst) { 91 this.file = file; 92 this.classification = classification; 93 this.excluded = excluded; 94 this.fromKSubst = fromKSubst; 95 this.toKSubst = toKSubst; 96 } 97 public boolean matchesFilter(int filter) { 98 return (classification & filter) != 0; 99 } 100 public boolean isExcluded() { 101 return excluded; 102 } 103 public void setExcluded(boolean excluded) { 104 this.excluded = excluded; 105 } 106 public boolean isNewKSubstMode() { 107 return ! fromKSubst.equals(toKSubst); 108 } 109 public void setKSubst(KSubstOption toKSubst) { 110 this.toKSubst = toKSubst; 111 } 112 public KSubstOption getKSubst() { 113 return toKSubst; 114 } 115 public IFile getFile() { 116 return file; 117 } 118 } 119 120 127 public KSubstWizard(IResource[] resources, int depth, KSubstOption defaultOption) { 128 super(); 129 this.defaultKSubst = defaultOption; 130 this.resources = resources; 131 this.depth = depth; 132 setWindowTitle(Policy.bind("KSubstWizard.title")); } 134 135 139 public KSubstOption getKSubstOption() { 140 return defaultKSubst; 141 } 142 143 public void addPages() { 144 ImageDescriptor substImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_KEYWORD); 145 146 String pageTitle = Policy.bind("KSubstWizardSelectionPage.pageTitle"); String pageDescription = Policy.bind("KSubstWizardSelectionPage.pageDescription"); mainPage = new KSubstWizardSelectionPage(pageTitle, pageTitle, substImage, defaultKSubst); 150 mainPage.setDescription(pageDescription); 151 mainPage.setTitle(pageTitle); 152 addPage(mainPage); 153 154 pageTitle = Policy.bind("KSubstWizardSummaryPage.pageTitle"); pageDescription = Policy.bind("KSubstWizardSummaryPage.pageDescription"); summaryPage = new KSubstWizardSummaryPage(pageTitle, pageTitle, substImage, false); 158 summaryPage.setDescription(pageDescription); 159 addPage(summaryPage); 160 161 pageTitle = Policy.bind("KSubstWizardSharedFilesPage.pageTitle"); pageDescription = Policy.bind("KSubstWizardSharedFilesPage.pageDescription"); sharedFilesPage = new KSubstWizardSharedFilesPage(pageTitle, pageTitle, substImage, false); 165 sharedFilesPage.setDescription(pageDescription); 166 addPage(sharedFilesPage); 167 168 pageTitle = Policy.bind("KSubstWizardDirtyFilesPage.pageTitle"); pageDescription = Policy.bind("KSubstWizardDirtyFilesPage.pageDescription"); dirtyFilesPage = new KSubstWizardDirtyFilesPage(pageTitle, pageTitle, substImage, false); 172 dirtyFilesPage.setDescription(pageDescription); 173 addPage(dirtyFilesPage); 174 175 pageTitle = Policy.bind("KSubstWizardCommitCommentPage.pageTitle"); pageDescription = Policy.bind("KSubstWizardCommitCommentPage.pageDescription"); commitCommentPage = new KSubstWizardCommitCommentPage(parentDialog, pageTitle, pageTitle, substImage, pageDescription); 179 addPage(commitCommentPage); 180 } 181 182 public IWizardPage getNextPage(IWizardPage page) { 183 if (page == mainPage) { 184 if (prepareSharedFilesPage()) return sharedFilesPage; 185 } else if (page == sharedFilesPage) { 186 if (sharedFilesPage.includeSharedFiles() && prepareDirtyFilesPage()) return dirtyFilesPage; 187 } else if (page == summaryPage) { 188 return null; 189 } 190 prepareSummaryPage(); 191 if (page != commitCommentPage) return commitCommentPage; 192 return summaryPage; 193 } 194 195 public IWizardPage getPreviousPage(IWizardPage page) { 196 if (page == summaryPage) { 197 return commitCommentPage; 198 } else if (page == commitCommentPage) { 199 if (sharedFilesPage.includeSharedFiles() && prepareDirtyFilesPage()) return dirtyFilesPage; 200 if (prepareSharedFilesPage()) return sharedFilesPage; 201 return mainPage; 202 } else if (page == dirtyFilesPage) { 203 if (prepareSharedFilesPage()) return sharedFilesPage; 204 return mainPage; 205 } else if (page == sharedFilesPage) { 206 return mainPage; 207 } 208 return null; 209 } 210 211 214 public boolean needsProgressMonitor() { 215 return true; 216 } 217 218 221 public boolean needsPreviousAndNextButtons() { 222 return true; 223 } 224 225 228 public boolean performFinish() { 229 try { 230 if (sharedFilesPage.includeSharedFiles() 231 && !MessageDialog.openConfirm(getShell(), null, Policy.bind("KSubstWizardSharedFilesPage.contents"))) { return false; 233 } 234 defaultKSubst = mainPage.getKSubstOption(); 235 final List messages = new ArrayList (); 236 getContainer().run(false , true , new IRunnableWithProgress() { 237 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 238 try { 239 monitor.beginTask("", 10000); monitor.setTaskName(Policy.bind("KSubstWizard.working")); computeChangeList(mainPage.getKSubstOption()); 242 Map table = getProviderMapping(); 243 244 int workPerProvider = 10000 / (table.size() + 1); 245 monitor.worked(workPerProvider); 246 for (Iterator it = table.entrySet().iterator(); it.hasNext();) { 247 Map.Entry entry = (Map.Entry ) it.next(); 248 CVSTeamProvider provider = (CVSTeamProvider) entry.getKey(); 249 Map providerFiles = (Map ) entry.getValue(); 250 251 String comment = commitCommentPage.getComment(); 252 IStatus status = provider.setKeywordSubstitution(providerFiles, comment, 253 Policy.subMonitorFor(monitor, workPerProvider)); 254 if (status.getCode() != CVSStatus.OK) { 255 messages.add(status); 256 } 257 } 258 } catch (TeamException e) { 259 throw new InvocationTargetException (e); 260 } finally { 261 monitor.done(); 262 } 263 } 264 }); 265 if ( ! messages.isEmpty()) { 267 boolean error = false; 268 MultiStatus combinedStatus = new MultiStatus(CVSUIPlugin.ID, 0, 269 Policy.bind("KSubstWizard.problemsMessage"), null); for (int i = 0; i < messages.size(); i++) { 271 IStatus status = (IStatus)messages.get(i); 272 if (status.getSeverity() == IStatus.ERROR || status.getCode() == CVSStatus.SERVER_ERROR) { 273 error = true; 274 } 275 combinedStatus.merge(status); 276 } 277 String message = null; 278 IStatus statusToDisplay; 279 if (combinedStatus.getChildren().length == 1) { 280 message = combinedStatus.getMessage(); 281 statusToDisplay = combinedStatus.getChildren()[0]; 282 } else { 283 statusToDisplay = combinedStatus; 284 } 285 String title; 286 if (error) { 287 title = Policy.bind("KSubstWizard.errorTitle"); } else { 289 title = Policy.bind("KSubstWizard.warningTitle"); } 291 CVSUIPlugin.openError(getShell(), title, message, new CVSException(statusToDisplay)); 292 } 293 return true; 294 } catch (InterruptedException e1) { 295 return true; 296 } catch (InvocationTargetException e2) { 297 CVSUIPlugin.openError(getShell(), Policy.bind("KSubstWizard.problemsMessage"), null, e2); return false; 299 } 300 } 301 302 private boolean prepareDirtyFilesPage() { 303 BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable () { 304 public void run() { 305 computeChangeList(mainPage.getKSubstOption()); 306 dirtyFilesPage.setChangeList(changeList); 307 } 308 }); 309 return ! dirtyFilesPage.isListEmpty(); 310 } 311 312 private boolean prepareSharedFilesPage() { 313 BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable () { 314 public void run() { 315 computeChangeList(mainPage.getKSubstOption()); 316 sharedFilesPage.setChangeList(changeList); 317 } 318 }); 319 return ! sharedFilesPage.isListEmpty(); 320 } 321 322 private void prepareSummaryPage() { 323 BusyIndicator.showWhile(getContainer().getShell().getDisplay(), new Runnable () { 324 public void run() { 325 computeChangeList(mainPage.getKSubstOption()); 326 summaryPage.setChangeList(changeList, getFilters()); 327 } 328 }); 329 } 330 331 335 private void computeChangeList(final KSubstOption ksubst) { 336 if (changeList != null) { 337 if (changeOption == ksubst) return; 338 changeList.clear(); 339 } else { 340 changeList = new ArrayList (); 341 } 342 changeOption = ksubst; 343 final Set seen = new HashSet (); 345 for (int i = 0; i < resources.length; i++) { 346 final IResource currentResource = resources[i]; 347 try { 348 currentResource.accept(new IResourceVisitor() { 349 public boolean visit(IResource resource) throws CoreException { 350 try { 351 if (resource.getType() == IResource.FILE && resource.exists() && ! seen.contains(resource)) { 352 seen.add(resource); 353 IFile file = (IFile) resource; 354 ICVSFile cvsFile = CVSWorkspaceRoot.getCVSFileFor(file); 355 if (cvsFile.isManaged()) { 356 ResourceSyncInfo info = cvsFile.getSyncInfo(); 357 final int classification; 359 if (info.isAdded()) { 360 classification = KSubstChangeElement.ADDED_FILE; 361 } else if (info.isDeleted()) { 362 return true; 363 } else if (cvsFile.isModified(null)) { 364 classification = KSubstChangeElement.CHANGED_FILE; 365 } else { 366 classification = KSubstChangeElement.UNCHANGED_FILE; 367 } 368 KSubstOption fromKSubst = info.getKeywordMode(); 370 KSubstOption toKSubst = ksubst; 371 if (ksubst == null) { 372 toKSubst = KSubstOption.fromFile(file); 373 } 374 changeList.add(new KSubstChangeElement(file, classification, false, fromKSubst, toKSubst)); 375 } 376 } 377 } catch (TeamException e) { 378 throw new CoreException(e.getStatus()); 379 } 380 return true; 382 } 383 }, depth, false); 384 } catch (CoreException e) { 385 CVSUIPlugin.openError(getShell(), Policy.bind("KSubstWizard.problemsMessage"), null, e); } 387 } 388 } 389 390 private int getFilters() { 391 return KSubstChangeElement.ADDED_FILE | 392 (sharedFilesPage.includeSharedFiles() ? KSubstChangeElement.UNCHANGED_FILE | 393 (dirtyFilesPage.includeDirtyFiles() ? KSubstChangeElement.CHANGED_FILE : 0) : 0); 394 } 395 396 private Map getProviderMapping() { 397 Map table = new HashMap (); 398 int filter = getFilters(); 399 for (Iterator it = changeList.iterator(); it.hasNext();) { 400 KSubstChangeElement change = (KSubstChangeElement) it.next(); 401 if (! change.isExcluded() && change.isNewKSubstMode() && change.matchesFilter(filter)) { 402 IFile file = change.getFile(); 404 RepositoryProvider provider = RepositoryProvider.getProvider(file.getProject(), CVSProviderPlugin.getTypeId()); 405 Map providerMap = (Map ) table.get(provider); 406 if (providerMap == null) { 407 providerMap = new HashMap (); 408 table.put(provider, providerMap); 409 } 410 providerMap.put(file, change.toKSubst); 411 } 412 } 413 return table; 414 } 415 416 420 public void setParentDialog(Dialog dialog) { 421 this.parentDialog = dialog; 422 } 423 } 424 | Popular Tags |