1 11 package org.eclipse.team.internal.ccvs.ui.repo; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Properties ; 18 19 import org.eclipse.core.resources.IProject; 20 import org.eclipse.core.resources.ResourcesPlugin; 21 import org.eclipse.core.runtime.*; 22 import org.eclipse.jface.dialogs.*; 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.util.PropertyChangeEvent; 25 import org.eclipse.jface.window.Window; 26 import org.eclipse.osgi.util.NLS; 27 import org.eclipse.swt.SWT; 28 import org.eclipse.swt.events.SelectionAdapter; 29 import org.eclipse.swt.events.SelectionEvent; 30 import org.eclipse.swt.layout.GridData; 31 import org.eclipse.swt.layout.GridLayout; 32 import org.eclipse.swt.widgets.*; 33 import org.eclipse.team.core.RepositoryProvider; 34 import org.eclipse.team.core.TeamException; 35 import org.eclipse.team.internal.ccvs.core.*; 36 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 37 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 38 import org.eclipse.team.internal.ccvs.ui.*; 39 import org.eclipse.team.internal.ccvs.ui.Policy; 40 import org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage; 41 import org.eclipse.team.internal.ui.dialogs.DetailsDialogWithProjects; 42 import org.eclipse.ui.PlatformUI; 43 import org.eclipse.ui.actions.WorkspaceModifyOperation; 44 import org.eclipse.ui.dialogs.PropertyPage; 45 46 public class CVSRepositoryPropertiesPage extends PropertyPage { 47 private static final String FAKE_PASSWORD = "*********"; ICVSRepositoryLocation location; 49 50 Text userText; 52 Text passwordText; 53 Combo methodType; 54 Text hostText; 55 Text pathText; 56 private Text portText; 58 private Button useDefaultPort; 59 private Button useCustomPort; 60 61 private Button allowCachingButton; 63 private boolean allowCaching = false; 64 65 boolean passwordChanged; 66 boolean connectionInfoChanged; 67 68 IUserInfo info; 69 70 private Button useLocationAsLabel; 72 private Button useCustomLabel; 73 private Text labelText; 74 75 78 protected Control createContents(Composite parent) { 79 initialize(); 80 81 Composite composite = new Composite(parent, SWT.NULL); 82 composite.setLayoutData(new GridData(GridData.FILL_BOTH)); 83 GridLayout layout = new GridLayout(); 84 layout.numColumns = 3; 85 composite.setLayout(layout); 86 87 Composite labelGroup = new Composite(composite, SWT.NONE); 90 GridData data = new GridData(); 91 data.horizontalSpan = 3; 92 labelGroup.setLayoutData(data); 93 layout = new GridLayout(); 94 layout.numColumns = 3; 95 layout.marginHeight = 0; 96 layout.marginWidth = 0; 97 labelGroup.setLayout(layout); 98 Listener labelListener = new Listener() { 99 public void handleEvent(Event event) { 100 updateWidgetEnablements(); 101 } 102 }; 103 useLocationAsLabel = createRadioButton(labelGroup, CVSUIMessages.CVSRepositoryPropertiesPage_useLocationAsLabel, 3); 104 useCustomLabel = createRadioButton(labelGroup, CVSUIMessages.CVSRepositoryPropertiesPage_useCustomLabel, 1); 105 useCustomLabel.addListener(SWT.Selection, labelListener); 106 labelText = createTextField(labelGroup); 107 labelText.addListener(SWT.Modify, labelListener); 108 109 createLabel(composite, "", 3); 112 createLabel(composite, CVSUIMessages.CVSPropertiesPage_connectionType, 1); 113 methodType = createCombo(composite); 114 115 createLabel(composite, CVSUIMessages.CVSPropertiesPage_user, 1); 116 userText = createTextField(composite); 117 118 createLabel(composite, CVSUIMessages.CVSPropertiesPage_password, 1); 119 passwordText = createPasswordField(composite); 120 121 createLabel(composite, CVSUIMessages.CVSPropertiesPage_host, 1); 122 hostText = createTextField(composite); 123 124 createLabel(composite, CVSUIMessages.CVSPropertiesPage_path, 1); 125 pathText = createTextField(composite); 126 127 Composite portGroup = new Composite(composite, SWT.NONE); 130 data = new GridData(); 131 data.horizontalSpan = 3; 132 portGroup.setLayoutData(data); 133 layout = new GridLayout(); 134 layout.numColumns = 3; 135 layout.marginHeight = 0; 136 layout.marginWidth = 0; 137 portGroup.setLayout(layout); 138 useDefaultPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_useDefaultPort, 3); 139 useCustomPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_usePort, 1); 140 portText = createTextField(portGroup); 141 142 createLabel(composite, "", 3); 145 allowCachingButton = new Button(composite, SWT.CHECK); 146 allowCachingButton.setText(CVSUIMessages.UserValidationDialog_6); 147 data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL); 148 data.horizontalSpan = 3; 149 allowCachingButton.setLayoutData(data); 150 allowCachingButton.addSelectionListener(new SelectionAdapter() { 151 public void widgetSelected(SelectionEvent e) { 152 allowCaching = allowCachingButton.getSelection(); 153 } 154 }); 155 156 Composite warningComposite = new Composite(composite, SWT.NONE); 157 layout = new GridLayout(); 158 layout.numColumns = 2; 159 layout.marginHeight = 0; 160 layout.marginHeight = 0; 161 warningComposite.setLayout(layout); 162 data = new GridData(GridData.FILL_HORIZONTAL); 163 data.horizontalSpan = 3; 164 warningComposite.setLayoutData(data); 165 Label warningLabel = new Label(warningComposite, SWT.NONE); 166 warningLabel.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 167 warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING)); 168 Label warningText = new Label(warningComposite, SWT.WRAP); 169 warningText.setText(CVSUIMessages.UserValidationDialog_7); 170 data = new GridData(GridData.FILL_HORIZONTAL); 171 data.widthHint = 300; 172 warningText.setLayoutData(data); 173 174 createLabel(composite, "", 3); 177 initializeValues(); 178 updateWidgetEnablements(); 179 Listener connectionInfoChangedListener = new Listener() { 180 public void handleEvent(Event event) { 181 connectionInfoChanged = true; 182 updateWidgetEnablements(); 183 } 184 }; 185 passwordText.addListener(SWT.Modify, new Listener() { 186 public void handleEvent(Event event) { 187 passwordChanged = !passwordText.getText().equals(FAKE_PASSWORD); 188 } 189 }); 190 userText.addListener(SWT.Modify, connectionInfoChangedListener); 191 methodType.addListener(SWT.Modify, connectionInfoChangedListener); 192 hostText.addListener(SWT.Modify, connectionInfoChangedListener); 193 portText.addListener(SWT.Modify, connectionInfoChangedListener); 194 useCustomPort.addListener(SWT.Selection, connectionInfoChangedListener); 195 pathText.addListener(SWT.Modify, connectionInfoChangedListener); 196 197 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.REPOSITORY_LOCATION_PROPERTY_PAGE); 198 Dialog.applyDialogFont(parent); 199 return composite; 200 } 201 202 208 protected Combo createCombo(Composite parent) { 209 Combo combo = new Combo(parent, SWT.READ_ONLY); 210 GridData data = new GridData(GridData.FILL_HORIZONTAL); 211 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 212 data.horizontalSpan = 2; 213 combo.setLayoutData(data); 214 return combo; 215 } 216 224 protected Label createLabel(Composite parent, String text, int span) { 225 Label label = new Label(parent, SWT.LEFT); 226 label.setText(text); 227 GridData data = new GridData(); 228 data.horizontalSpan = span; 229 data.horizontalAlignment = GridData.FILL; 230 label.setLayoutData(data); 231 return label; 232 } 233 239 protected Text createTextField(Composite parent) { 240 Text text = new Text(parent, SWT.SINGLE | SWT.BORDER); 241 return layoutTextField(text); 242 } 243 249 protected Text createPasswordField(Composite parent) { 250 Text text = new Text(parent, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD); 251 return layoutTextField(text); 252 } 253 259 protected Text layoutTextField(Text text) { 260 GridData data = new GridData(GridData.FILL_HORIZONTAL); 261 data.verticalAlignment = GridData.CENTER; 262 data.grabExcessVerticalSpace = false; 263 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 264 data.horizontalSpan = 2; 265 text.setLayoutData(data); 266 return text; 267 } 268 269 277 protected Button createRadioButton(Composite parent, String label, int span) { 278 Button button = new Button(parent, SWT.RADIO); 279 button.setText(label); 280 GridData data = new GridData(); 281 data.horizontalSpan = span; 282 button.setLayoutData(data); 283 return button; 284 } 285 288 private void initialize() { 289 location = null; 290 IAdaptable element = getElement(); 291 if (element instanceof ICVSRepositoryLocation) { 292 location = (ICVSRepositoryLocation)element; 293 } else { 294 Object adapter = element.getAdapter(ICVSRepositoryLocation.class); 295 if (adapter instanceof ICVSRepositoryLocation) { 296 location = (ICVSRepositoryLocation)adapter; 297 } 298 } 299 } 300 303 private void initializeValues() { 304 passwordChanged = false; 305 connectionInfoChanged = false; 306 307 IConnectionMethod[] methods = CVSRepositoryLocation.getPluggedInConnectionMethods(); 308 for (int i = 0; i < methods.length; i++) { 309 methodType.add(methods[i].getName()); 310 } 311 String method = location.getMethod().getName(); 312 methodType.select(methodType.indexOf(method)); 313 info = location.getUserInfo(true); 314 userText.setText(info.getUsername()); 315 passwordText.setText(FAKE_PASSWORD); 316 hostText.setText(location.getHost()); 317 int port = location.getPort(); 318 if (port == ICVSRepositoryLocation.USE_DEFAULT_PORT) { 319 useDefaultPort.setSelection(true); 320 useCustomPort.setSelection(false); 321 portText.setEnabled(false); 322 } else { 323 useDefaultPort.setSelection(false); 324 useCustomPort.setSelection(true); 325 portText.setText("" + port); } 327 pathText.setText(location.getRootDirectory()); 328 allowCachingButton.setSelection(location.getUserInfoCached()); 329 330 String label = null; 332 RepositoryRoot root = CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryRootFor(location); 333 label = root.getName(); 334 useLocationAsLabel.setSelection(label == null); 335 useCustomLabel.setSelection(!useLocationAsLabel.getSelection()); 336 if (label == null) { 337 label = location.getLocation(true); 338 } 339 labelText.setText(label); 340 } 341 342 private boolean performConnectionInfoChanges() { 343 if (!connectionInfoChanged) { 345 location.setAllowCaching(allowCaching); 346 if (!passwordChanged) { 347 ((CVSRepositoryLocation)location).updateCache(); 348 } 349 } 350 if (!passwordChanged && !connectionInfoChanged) return true; 352 353 try { 354 if (passwordChanged && !connectionInfoChanged) { 356 CVSRepositoryLocation oldLocation = (CVSRepositoryLocation)location; 357 oldLocation.setPassword(getNewPassword()); 358 if (allowCaching) { 359 oldLocation.updateCache(); 360 } 361 passwordChanged = false; 362 return true; 363 } 364 365 if (!(location.getHost().equals(hostText.getText()) && location.getRootDirectory().equals(pathText.getText()))) { 369 if (!MessageDialog.openConfirm(getShell(), 371 CVSUIMessages.CVSRepositoryPropertiesPage_0, 372 CVSUIMessages.CVSRepositoryPropertiesPage_1)) { 373 return false; 374 } 375 } 376 final boolean[] result = new boolean[] { false }; 377 final ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(getShell()); 378 progressMonitorDialog.run(false, false, new WorkspaceModifyOperation(null) { 379 public void execute(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 380 try { 381 CVSRepositoryLocation newLocation = CVSRepositoryLocation.fromProperties(createProperties()); 383 newLocation.setEncoding(location.getEncoding()); 384 location.setAllowCaching(allowCaching); 385 try { 386 List projects = new ArrayList (); 388 IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); 389 for (int i = 0; i < allProjects.length; i++) { 390 RepositoryProvider teamProvider = RepositoryProvider.getProvider(allProjects[i], CVSProviderPlugin.getTypeId()); 391 if (teamProvider != null) { 392 CVSTeamProvider cvsProvider = (CVSTeamProvider)teamProvider; 393 if (cvsProvider.getCVSWorkspaceRoot().getRemoteLocation().equals(location)) { 394 projects.add(allProjects[i]); 395 } 396 } 397 } 398 if (projects.size() > 0) { 399 DetailsDialogWithProjects dialog = new DetailsDialogWithProjects( 401 progressMonitorDialog.getShell(), 402 CVSUIMessages.CVSRepositoryPropertiesPage_Confirm_Project_Sharing_Changes_1, 403 CVSUIMessages.CVSRepositoryPropertiesPage_There_are_projects_in_the_workspace_shared_with_this_repository_2, 404 NLS.bind(CVSUIMessages.CVSRepositoryPropertiesPage_sharedProject, new String [] { location.toString() }), 405 (IProject[]) projects.toArray(new IProject[projects.size()]), 406 true, 407 Dialog.DLG_IMG_WARNING); 408 int r = dialog.open(); 409 if (r != Window.OK) { 410 result[0] = false; 411 return; 412 } 413 monitor.beginTask(null, 1000 * projects.size()); 414 try { 415 Iterator it = projects.iterator(); 416 while (it.hasNext()) { 417 IProject project = (IProject)it.next(); 418 RepositoryProvider teamProvider = RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId()); 419 CVSTeamProvider cvsProvider = (CVSTeamProvider)teamProvider; 420 cvsProvider.setRemoteRoot(newLocation, Policy.subMonitorFor(monitor, 1000)); 421 } 422 } finally { 423 monitor.done(); 424 } 425 } 426 427 CVSUIPlugin.getPlugin().getRepositoryManager().replaceRepositoryLocation(location, newLocation); 429 430 } finally { 431 newLocation = (CVSRepositoryLocation)KnownRepositories.getInstance().addRepository(newLocation, !KnownRepositories.getInstance().isKnownRepository(newLocation.getLocation())); 433 } 434 435 location = newLocation; 437 connectionInfoChanged = false; 438 passwordChanged = false; 439 } catch (TeamException e) { 440 throw new InvocationTargetException (e); 441 } 442 result[0] = true; 443 } 444 }); 445 return result[0]; 446 } catch (InvocationTargetException e) { 447 handle(e); 448 } catch (InterruptedException e) { 449 } 450 return false; 451 } 452 453 private void performNonConnectionInfoChanges() { 454 recordNewLabel((CVSRepositoryLocation)location); 455 } 456 459 public boolean performOk() { 460 if (performConnectionInfoChanges()) { 461 performNonConnectionInfoChanges(); 462 return true; 463 } 464 return false; 465 } 466 467 470 protected void performDefaults() { 471 super.performDefaults(); 472 initializeValues(); 473 } 474 475 478 protected void handle(Throwable e) { 479 CVSUIPlugin.openError(getShell(), null, null, e); 480 } 481 482 485 protected void updateWidgetEnablements() { 486 if (useLocationAsLabel.getSelection()) { 487 labelText.setEnabled(false); 488 } else { 489 labelText.setEnabled(true); 490 } 491 if (useDefaultPort.getSelection()) { 492 portText.setEnabled(false); 493 } else { 494 portText.setEnabled(true); 495 } 496 validateFields(); 497 } 498 499 private void validateFields() { 500 if (labelText.isEnabled()) { 501 if (labelText.getText().length() == 0) { 502 setValid(false); 503 return; 504 } 505 } 506 String user = userText.getText(); 507 IStatus status = ConfigurationWizardMainPage.validateUserName(user); 508 if (!isStatusOK(status)) { 509 return; 510 } 511 512 String host = hostText.getText(); 513 status = ConfigurationWizardMainPage.validateHost(host); 514 if (!isStatusOK(status)) { 515 return; 516 } 517 518 if (portText.isEnabled()) { 519 String port = portText.getText(); 520 status = ConfigurationWizardMainPage.validatePort(port); 521 if (!isStatusOK(status)) { 522 return; 523 } 524 } 525 526 String pathString = pathText.getText(); 527 status = ConfigurationWizardMainPage.validatePath(pathString); 528 if (!isStatusOK(status)) { 529 return; 530 } 531 532 try { 533 CVSRepositoryLocation l = CVSRepositoryLocation.fromProperties(createProperties()); 534 if (!l.equals(location) && KnownRepositories.getInstance().isKnownRepository(l.getLocation())) { 535 setErrorMessage(CVSUIMessages.ConfigurationWizardMainPage_0); 536 setValid(false); 537 return; 538 } 539 } catch (CVSException e) { 540 CVSUIPlugin.log(e); 541 } 543 544 setErrorMessage(null); 545 setValid(true); 546 } 547 548 private boolean isStatusOK(IStatus status) { 549 if (!status.isOK()) { 550 setErrorMessage(status.getMessage()); 551 setValid(false); 552 return false; 553 } 554 return true; 555 } 556 557 private void recordNewLabel(CVSRepositoryLocation location) { 558 String newLabel = getNewLabel(location); 559 if (newLabel == null) { 560 String oldLabel = getOldLabel(location); 561 if (oldLabel == null || oldLabel.equals(location.getLocation())) { 562 return; 563 } 564 } else if (newLabel.equals(getOldLabel(location))) { 565 return; 566 } 567 CVSUIPlugin.getPlugin().getRepositoryManager().setLabel(location, newLabel); 568 CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this, 569 CVSUIPlugin.P_DECORATORS_CHANGED, null, null)); 570 571 } 572 private String getOldLabel(CVSRepositoryLocation location) { 573 return CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryRootFor(location).getName(); 574 } 575 private String getNewLabel(CVSRepositoryLocation location) { 576 String label = null; 577 if (useCustomLabel.getSelection()) { 578 label = labelText.getText(); 579 if (label.equals(location.getLocation())) { 580 label = null; 581 } 582 } 583 return label; 584 } 585 String getNewPassword() { 586 return passwordText.getText(); 587 } 588 589 private Properties createProperties() { 590 Properties result = new Properties (); 591 result.setProperty("connection", methodType.getText()); result.setProperty("user", userText.getText()); if (passwordChanged) { 594 result.setProperty("password", passwordText.getText()); } 596 result.setProperty("host", hostText.getText()); if (useCustomPort.getSelection()) { 598 result.setProperty("port", portText.getText()); } 600 result.setProperty("root", pathText.getText()); return result; 602 } 603 } 604 605 | Popular Tags |