1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.*; 19 import org.eclipse.jface.dialogs.IDialogSettings; 20 import org.eclipse.jface.dialogs.MessageDialog; 21 import org.eclipse.jface.operation.IRunnableWithProgress; 22 import org.eclipse.jface.resource.ImageDescriptor; 23 import org.eclipse.jface.wizard.IWizardPage; 24 import org.eclipse.jface.wizard.Wizard; 25 import org.eclipse.osgi.util.NLS; 26 import org.eclipse.swt.widgets.Display; 27 import org.eclipse.swt.widgets.Shell; 28 import org.eclipse.team.core.RepositoryProvider; 29 import org.eclipse.team.core.TeamException; 30 import org.eclipse.team.internal.ccvs.core.*; 31 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 32 import org.eclipse.team.internal.ccvs.core.syncinfo.FolderSyncInfo; 33 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 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.tags.*; 38 import org.eclipse.team.internal.ui.Utils; 39 import org.eclipse.team.ui.IConfigurationWizard; 40 import org.eclipse.team.ui.synchronize.ModelSynchronizeParticipant; 41 import org.eclipse.ui.IWorkbench; 42 43 47 public class SharingWizard extends Wizard implements IConfigurationWizard, ICVSWizard { 48 private IProject project; 50 51 private ConfigurationWizardAutoconnectPage autoconnectPage; 53 54 private RepositorySelectionPage locationPage; 56 57 private ConfigurationWizardMainPage createLocationPage; 59 60 private ModuleSelectionPage modulePage; 62 63 private TagSelectionWizardPage tagPage; 65 66 private SharingWizardSyncPage syncPage; 68 69 private ICVSRepositoryLocation location; 71 private boolean isNewLocation; 72 73 private ICVSRemoteFolder existingRemote; 75 76 public SharingWizard() { 77 IDialogSettings cvsSettings = CVSUIPlugin.getPlugin().getDialogSettings(); 78 IDialogSettings section = cvsSettings.getSection("SharingWizard"); if (section == null) { 80 section = cvsSettings.addNewSection("SharingWizard"); } 82 setDialogSettings(section); 83 setNeedsProgressMonitor(true); 84 setWindowTitle(CVSUIMessages.SharingWizard_title); 85 } 86 87 public void addPages() { 88 ImageDescriptor sharingImage = CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_SHARE); 89 boolean autoconnect = false; 90 if (doesCVSDirectoryExist()) { 91 autoconnectPage = new ConfigurationWizardAutoconnectPage("autoconnectPage", CVSUIMessages.SharingWizard_autoConnectTitle, sharingImage); if (autoconnectPage.setProject(project)) { 93 autoconnectPage.setDescription(CVSUIMessages.SharingWizard_autoConnectTitleDescription); 94 addPage(autoconnectPage); 95 autoconnect = true; 96 } 97 } 98 if (!autoconnect) { 99 ICVSRepositoryLocation[] locations = CVSUIPlugin.getPlugin().getRepositoryManager().getKnownRepositoryLocations(); 100 if (locations.length > 0) { 101 locationPage = new RepositorySelectionPage("importPage", CVSUIMessages.SharingWizard_importTitle, sharingImage); locationPage.setDescription(CVSUIMessages.SharingWizard_importTitleDescription); 103 addPage(locationPage); 104 } 105 createLocationPage = new ConfigurationWizardMainPage("createLocationPage", CVSUIMessages.SharingWizard_enterInformation, sharingImage); createLocationPage.setDescription(CVSUIMessages.SharingWizard_enterInformationDescription); 107 createLocationPage.setCVSWizard(this); 108 createLocationPage.setDialogSettings(NewLocationWizard.getLocationDialogSettings()); 109 addPage(createLocationPage); 110 modulePage = new ModuleSelectionPage("modulePage", CVSUIMessages.SharingWizard_enterModuleName, sharingImage); modulePage.setDescription(CVSUIMessages.SharingWizard_enterModuleNameDescription); 112 modulePage.setCVSWizard(this); 113 modulePage.setProject(project); 114 modulePage.setHelpContxtId(IHelpContextIds.SHARING_MODULE_PAGE); 115 addPage(modulePage); 116 117 addTagPage(sharingImage); 118 addSyncPage(sharingImage); 119 } 120 } 121 122 private void addTagPage(ImageDescriptor sharingImage) { 123 tagPage = new TagSelectionWizardPage("tagPage", CVSUIMessages.SharingWizard_selectTagTitle, 125 sharingImage, 126 CVSUIMessages.SharingWizard_selectTag, 127 TagSource.EMPTY, TagSourceWorkbenchAdapter.INCLUDE_HEAD_TAG | TagSourceWorkbenchAdapter.INCLUDE_BRANCHES); 129 tagPage.setCVSWizard(this); 130 tagPage.setHelpContxtId(IHelpContextIds.SHARING_TAG_SELETION_PAGE); 131 addPage(tagPage); 132 } 133 134 private void addSyncPage(ImageDescriptor sharingImage) { 135 syncPage = new SharingWizardSyncPage("syncPagePage", CVSUIMessages.SharingWizard_23, 137 sharingImage, 138 CVSUIMessages.SharingWizard_24); 139 syncPage.setProject(project); 140 syncPage.setCVSWizard(this); 141 addPage(syncPage); 142 } 143 144 public boolean canFinish() { 145 IWizardPage page = getContainer().getCurrentPage(); 146 return (page == autoconnectPage || page == syncPage); 147 } 148 149 public IWizardPage getNextPage(IWizardPage page) { 150 return getNextPage(page, true ); 153 } 154 155 public IWizardPage getNextPage(IWizardPage page, boolean aboutToShow) { 156 if (page == autoconnectPage) return null; 157 if (page == locationPage) { 158 if (locationPage.getLocation() == null) { 159 return createLocationPage; 160 } else { 161 if (aboutToShow) { 162 try { 163 modulePage.setLocation(getLocation()); 164 } catch (TeamException e1) { 165 CVSUIPlugin.log(e1); 166 } 167 } 168 return modulePage; 169 } 170 } 171 if (page == createLocationPage) { 172 if (aboutToShow) { 173 try { 174 modulePage.setLocation(getLocation()); 175 } catch (TeamException e1) { 176 CVSUIPlugin.log(e1); 177 } 178 } 179 return modulePage; 180 } 181 try { 182 if (page == modulePage) { 183 if (aboutToShow) { 184 ICVSRemoteFolder remoteFolder = getRemoteFolder(); 185 if (exists(remoteFolder)) { 186 prepareTagPage(remoteFolder); 187 return tagPage; 188 } else { 189 try { 190 populateSyncPage(false ); 191 } catch (InvocationTargetException e) { 192 CVSUIPlugin.openError(getShell(), null, null, e); 193 if (!RepositoryProvider.isShared(project)) { 194 return null; 196 } 197 } 198 return syncPage; 199 } 200 } else { 201 return syncPage; 202 } 203 } 204 if (page == tagPage) { 205 if (aboutToShow) { 206 populateSyncPage(true ); 207 } 208 return syncPage; 209 } 210 } catch (InvocationTargetException e) { 211 CVSUIPlugin.openError(getShell(), null, null, e); 213 } catch (InterruptedException e) { 214 } 216 return null; 217 } 218 219 222 public boolean performFinish() { 223 final boolean[] result = new boolean[] { true }; 224 if (isAutoconnect()) { 225 try { 226 getContainer().run(true , true , new IRunnableWithProgress() { 227 public void run(IProgressMonitor monitor) throws InvocationTargetException { 228 try { 229 result[0] = autoconnectCVSProject(monitor); 230 } catch (TeamException e) { 231 throw new InvocationTargetException (e); 232 } finally { 233 monitor.done(); 234 } 235 } 236 }); 237 } catch (InterruptedException e) { 238 return true; 239 } catch (InvocationTargetException e) { 240 CVSUIPlugin.openError(getContainer().getShell(), null, null, e); 241 result[0] = false; 242 } 243 } 244 if (result[0] && isNewLocation) { 246 KnownRepositories.getInstance().addRepository(location, true ); 247 } 248 249 final Shell parentShell= getShell().getParent().getShell(); 250 if (getContainer().getCurrentPage() == syncPage) { 251 syncPage.saveSettings(); 252 if (syncPage.commitChanges()) { 253 Display.getCurrent().asyncExec(new Runnable () { 254 public void run() { 255 try { 256 CommitWizard.run(null, parentShell, new IResource[] { syncPage.getProject() }); 257 } catch (CVSException e) { 258 } 260 } 261 }); 262 } 263 } 264 return result[0]; 265 } 266 267 270 public boolean performCancel() { 271 boolean disposeLocation = isNewLocation; 272 ICVSRepositoryLocation location; 273 try { 274 location = getLocation(); 275 } catch (TeamException e) { 276 CVSUIPlugin.log(e); 277 return true; 278 } 279 if (location == null) return true; 280 if (getContainer().getCurrentPage() == syncPage 282 && RepositoryProvider.getProvider(project) != null) { 283 if (promptToKeepMapping()) { 285 disposeLocation = false; 287 if (isNewLocation) { 289 KnownRepositories.getInstance().addRepository(location, true ); 290 } 291 } else { 292 try { 293 getContainer().run(true, true, new IRunnableWithProgress() { 294 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 295 new DisconnectOperation(null, new IProject[] { project }, true) 296 .run(monitor); 297 } 298 }); 299 } catch (InvocationTargetException e) { 300 CVSUIPlugin.log(IStatus.ERROR, e.getMessage(), e.getTargetException()); 301 } catch (InterruptedException e) { 302 } 304 } 305 } 306 if (disposeLocation) { 308 KnownRepositories.getInstance().disposeRepository(location); 309 } 310 return super.performCancel(); 311 } 312 313 private boolean promptToKeepMapping() { 314 return (MessageDialog.openQuestion(getShell(), CVSUIMessages.SharingWizard_26, NLS.bind(CVSUIMessages.SharingWizard_27, new String [] { project.getName() }))); } 316 317 private void reconcileProject(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 318 new ReconcileProjectOperation(getShell(), project, getRemoteFolder()).run(monitor); 319 } 320 321 324 private ICVSRepositoryLocation getLocation() throws TeamException { 325 if (autoconnectPage != null) { 327 return recordLocation(autoconnectPage.getLocation()); 328 } 329 330 if (locationPage != null) { 332 ICVSRepositoryLocation newLocation = locationPage.getLocation(); 333 if (newLocation != null) { 334 return recordLocation(newLocation); 335 } 336 } 337 338 final ICVSRepositoryLocation[] locations = new ICVSRepositoryLocation[] { null }; 340 final CVSException[] exception = new CVSException[] { null }; 341 getShell().getDisplay().syncExec(new Runnable () { 342 public void run() { 343 try { 344 locations[0] = createLocationPage.getLocation(); 345 } catch (CVSException e) { 346 exception[0] = e; 347 } 348 } 349 }); 350 if (exception[0] != null) { 351 throw exception[0]; 352 } 353 return recordLocation(locations[0]); 354 } 355 356 private ICVSRepositoryLocation recordLocation(ICVSRepositoryLocation newLocation) { 357 if (newLocation == null) return location; 358 if (location == null || !newLocation.equals(location)) { 359 if (location != null && isNewLocation) { 360 KnownRepositories.getInstance().disposeRepository(location); 362 } 363 location = newLocation; 364 isNewLocation = !KnownRepositories.getInstance().isKnownRepository(newLocation.getLocation(false)); 365 if (isNewLocation) { 366 location = KnownRepositories.getInstance().addRepository(location, false ); 368 } 369 } 370 return location; 371 } 372 373 376 public void init(IWorkbench workbench, IProject project) { 377 this.project = project; 378 } 379 380 private boolean doesCVSDirectoryExist() { 381 Shell shell = null; 384 if (getContainer() != null) { 385 shell = getContainer().getShell(); 386 } 387 final boolean[] isCVSFolder = new boolean[] { false }; 388 try { 389 CVSUIPlugin.runWithRefresh(shell, new IResource[] { project }, new IRunnableWithProgress() { 390 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 391 try { 392 ICVSFolder folder = (ICVSFolder)CVSWorkspaceRoot.getCVSResourceFor(project); 393 FolderSyncInfo info = folder.getFolderSyncInfo(); 394 isCVSFolder[0] = info != null; 395 } catch (final TeamException e) { 396 throw new InvocationTargetException (e); 397 } 398 } 399 }, null); 400 } catch (InvocationTargetException e) { 401 CVSUIPlugin.openError(shell, null, null, e); 402 } catch (InterruptedException e) { 403 } 405 return isCVSFolder[0]; 406 } 407 408 411 boolean isAutoconnect() { 412 return autoconnectPage != null && doesCVSDirectoryExist(); 413 } 414 415 418 boolean autoconnectCVSProject(IProgressMonitor monitor) throws TeamException { 419 try { 420 monitor.beginTask(null, 100); 421 422 FolderSyncInfo info = autoconnectPage.getFolderSyncInfo(); 423 if (info == null) { 424 return false; 426 } 427 428 ICVSRepositoryLocation location = getLocation(); 430 431 boolean validate = autoconnectPage.getValidate(); 433 if (validate) { 434 try { 436 location.validateConnection(Policy.subMonitorFor(monitor, 50)); 437 } catch (final TeamException e) { 438 final boolean[] keep = new boolean[] { false }; 440 getShell().getDisplay().syncExec(new Runnable () { 441 public void run() { 442 keep[0] = MessageDialog.openQuestion(getContainer().getShell(), 443 CVSUIMessages.SharingWizard_validationFailedTitle, 444 NLS.bind(CVSUIMessages.SharingWizard_validationFailedText, (new Object [] {e.getStatus().getMessage()}))); 445 } 446 }); 447 if (!keep[0]) { 448 return false; 449 } 450 } 452 } 453 454 CVSWorkspaceRoot.setSharing(project, info, Policy.subMonitorFor(monitor, 50)); 456 return true; 457 } finally { 458 monitor.done(); 459 } 460 } 461 462 private boolean shareProject(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 463 monitor.beginTask(null, 100); 464 ICVSRepositoryLocation location = null; 465 try { 466 location = getLocation(); 467 location.validateConnection(Policy.subMonitorFor(monitor, 50)); 468 } catch (TeamException e) { 469 CVSUIPlugin.openError(getShell(), null, null, e, CVSUIPlugin.PERFORM_SYNC_EXEC); 470 if (isNewLocation && location != null) location.flushUserInfo(); 471 return false; 472 } 473 474 ShareProjectOperation op = new ShareProjectOperation(null, location, project, getRemoteFolder().getRepositoryRelativePath()); 476 op.setShell(getShell()); 477 op.run(Policy.subMonitorFor(monitor, 50)); 478 return true; 479 } 480 481 private CVSTag getTag() { 482 if (tagPage == null || tagPage.getSelectedTag() == null) { 483 return CVSTag.DEFAULT; 484 } 485 return tagPage.getSelectedTag(); 486 } 487 488 private ICVSRemoteFolder getRemoteFolder() { 489 ICVSRemoteFolder folder = modulePage.getSelectedModule(); 490 return (ICVSRemoteFolder)folder.forTag(getTag()); 491 } 492 493 private boolean exists(ICVSRemoteFolder folder, IProgressMonitor monitor) throws TeamException { 494 if (existingRemote != null && existingRemote.equals(folder)) return true; 495 if (folder.exists(monitor)) { 496 existingRemote = folder; 497 return true; 498 } else { 499 existingRemote = null; 500 return false; 501 } 502 } 503 504 private boolean exists(final ICVSRemoteFolder folder) throws InvocationTargetException , InterruptedException { 505 final boolean[] result = new boolean[] { false }; 506 getContainer().run(true, true, new IRunnableWithProgress() { 507 public void run(IProgressMonitor monitor) 508 throws InvocationTargetException , InterruptedException { 509 try { 510 result[0] = exists(folder, monitor); 511 } catch (TeamException e) { 512 throw new InvocationTargetException (e); 513 } 514 } 515 }); 516 return result[0]; 517 } 518 519 private void populateSyncPage(final boolean exists) throws InvocationTargetException , InterruptedException { 520 getContainer().run(true, true, new IRunnableWithProgress() { 521 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 522 monitor.beginTask(null, IProgressMonitor.UNKNOWN); 523 if (exists) { 524 reconcileProject(Policy.subMonitorFor(monitor, 50)); 525 } else { 526 shareProject(Policy.subMonitorFor(monitor, 50)); 527 } 528 try { 529 getParticipant().getContext().refresh(Utils.getResourceMappings(new IProject[] { project }), Policy.subMonitorFor(monitor, 50)); 530 } catch (CoreException e) { 531 throw new InvocationTargetException (e); 532 } 533 if (monitor.isCanceled()) { 534 throw new InterruptedException (); 535 } 536 monitor.done(); 537 } 538 }); 539 } 540 541 544 public IWizardPage getPreviousPage(IWizardPage page) { 545 if (page == syncPage) { 546 return null; 548 } 549 return super.getPreviousPage(page); 550 } 551 552 private void prepareTagPage(ICVSRemoteFolder remote) { 553 tagPage.setTagSource(TagSource.create(remote)); 554 tagPage.setDescription(NLS.bind(CVSUIMessages.SharingWizard_25, new String [] { remote.getRepositoryRelativePath() })); 555 } 556 557 private ModelSynchronizeParticipant getParticipant() { 558 return syncPage.getParticipant(); 559 } 560 } 561 | Popular Tags |