1 12 package org.eclipse.team.internal.ccvs.ui.wizards; 13 14 import java.util.ArrayList ; 15 import java.util.Arrays ; 16 import java.util.List ; 17 import java.util.Properties ; 18 19 import org.eclipse.core.runtime.*; 20 import org.eclipse.jface.dialogs.*; 21 import org.eclipse.jface.dialogs.Dialog; 22 import org.eclipse.jface.preference.PreferenceDialog; 23 import org.eclipse.jface.resource.ImageDescriptor; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.SelectionAdapter; 26 import org.eclipse.swt.events.SelectionEvent; 27 import org.eclipse.swt.graphics.FontMetrics; 28 import org.eclipse.swt.graphics.GC; 29 import org.eclipse.swt.layout.*; 30 import org.eclipse.swt.widgets.*; 31 import org.eclipse.team.internal.ccvs.core.*; 32 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation; 33 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories; 34 import org.eclipse.team.internal.ccvs.ui.*; 35 import org.eclipse.ui.PlatformUI; 36 import org.eclipse.ui.dialogs.PreferencesUtil; 37 38 41 public class ConfigurationWizardMainPage extends CVSWizardPage { 42 private static final String ANONYMOUS_USER = "anonymous"; 44 private boolean showValidate; 45 private boolean validate; 46 47 49 private Combo connectionMethodCombo; 51 private Combo userCombo; 53 private Text passwordText; 55 private Text portText; 57 private Button useDefaultPort; 58 private Button useCustomPort; 59 private Combo hostCombo; 61 private Combo repositoryPathCombo; 63 private Button validateButton; 65 private Button allowCachingButton; 67 private boolean allowCaching = false; 68 69 private static final int COMBO_HISTORY_LENGTH = 5; 70 71 private Properties properties = null; 72 73 private ICVSRepositoryLocation location; 77 78 private ICVSRepositoryLocation oldLocation; 81 82 private static final String STORE_USERNAME_ID = 84 "ConfigurationWizardMainPage.STORE_USERNAME_ID"; private static final String STORE_HOSTNAME_ID = 86 "ConfigurationWizardMainPage.STORE_HOSTNAME_ID"; private static final String STORE_PATH_ID = 88 "ConfigurationWizardMainPage.STORE_PATH_ID"; private static final String STORE_DONT_VALIDATE_ID = 90 "ConfigurationWizardMainPage.STORE_DONT_VALIDATE_ID"; 92 private IDialogSettings settings; 94 95 102 public ConfigurationWizardMainPage(String pageName, String title, ImageDescriptor titleImage) { 103 super(pageName, title, titleImage); 104 } 105 114 private String [] addToHistory(String [] history, String newEntry) { 115 ArrayList l = new ArrayList (Arrays.asList(history)); 116 addToHistory(l, newEntry); 117 String [] r = new String [l.size()]; 118 l.toArray(r); 119 return r; 120 } 121 protected IDialogSettings getDialogSettings() { 122 return settings; 123 } 124 protected void setDialogSettings(IDialogSettings settings) { 125 this.settings = settings; 126 } 127 135 private void addToHistory(List history, String newEntry) { 136 history.remove(newEntry); 137 history.add(0,newEntry); 138 139 if (history.size() > COMBO_HISTORY_LENGTH) 142 history.remove(COMBO_HISTORY_LENGTH); 143 } 144 149 public void createControl(Composite parent) { 150 Composite composite = createComposite(parent, 2, false); 151 PlatformUI.getWorkbench().getHelpSystem().setHelp(composite, IHelpContextIds.SHARING_NEW_REPOSITORY_PAGE); 153 154 Listener listener = new Listener() { 155 public void handleEvent(Event event) { 156 if (location != null) { 157 oldLocation = location; 158 location = null; 159 } 160 if (event.widget == hostCombo) { 161 String hostText = hostCombo.getText(); 162 if (hostText.length() > 0 && hostText.charAt(0) == ':') { 163 try { 164 CVSRepositoryLocation newLocation = CVSRepositoryLocation.fromString(hostText); 165 connectionMethodCombo.setText(newLocation.getMethod().getName()); 166 repositoryPathCombo.setText(newLocation.getRootDirectory()); 167 int port = newLocation.getPort(); 168 if (port == ICVSRepositoryLocation.USE_DEFAULT_PORT) { 169 useDefaultPort.setSelection(true); 170 useCustomPort.setSelection(false); 171 } else { 172 useCustomPort.setSelection(true); 173 useDefaultPort.setSelection(false); 174 portText.setText(String.valueOf(port)); 175 } 176 177 userCombo.setText(newLocation.getUsername()); 178 hostCombo.setText(newLocation.getHost()); 180 } catch (CVSException e) { 181 CVSUIPlugin.log(e); 182 } 183 } 184 } 185 updateWidgetEnablements(); 186 } 187 }; 188 189 Group g = createGroup(composite, CVSUIMessages.ConfigurationWizardMainPage_Location_1); 190 191 createLabel(g, CVSUIMessages.ConfigurationWizardMainPage_host); 193 hostCombo = createEditableCombo(g); 194 hostCombo.addListener(SWT.Selection, listener); 195 hostCombo.addListener(SWT.Modify, listener); 196 197 createLabel(g, CVSUIMessages.ConfigurationWizardMainPage_repositoryPath); 199 repositoryPathCombo = createEditableCombo(g); 200 repositoryPathCombo.addListener(SWT.Selection, listener); 201 repositoryPathCombo.addListener(SWT.Modify, listener); 202 203 g = createGroup(composite, CVSUIMessages.ConfigurationWizardMainPage_Authentication_2); 204 205 createLabel(g, CVSUIMessages.ConfigurationWizardMainPage_userName); 207 userCombo = createEditableCombo(g); 208 userCombo.addListener(SWT.Selection, listener); 209 userCombo.addListener(SWT.Modify, listener); 210 211 createLabel(g, CVSUIMessages.ConfigurationWizardMainPage_password); 213 passwordText = createPasswordField(g); 214 passwordText.addListener(SWT.Modify, listener); 215 216 g = createGroup(composite, CVSUIMessages.ConfigurationWizardMainPage_Connection_3); 217 218 createLabel(g, CVSUIMessages.ConfigurationWizardMainPage_connection); 220 connectionMethodCombo = createCombo(g); 221 connectionMethodCombo.addListener(SWT.Selection, listener); 222 223 Composite portGroup = new Composite(g, SWT.NONE); 226 GridData data = new GridData(); 227 data.horizontalSpan = 2; 228 portGroup.setLayoutData(data); 229 GridLayout layout = new GridLayout(); 230 layout.numColumns = 2; 231 layout.marginHeight = 0; 232 layout.marginWidth = 0; 233 portGroup.setLayout(layout); 234 useDefaultPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_useDefaultPort, 2); 235 useCustomPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_usePort, 1); 236 useCustomPort.addListener(SWT.Selection, listener); 237 portText = createTextField(portGroup); 238 portText.addListener(SWT.Modify, listener); 239 240 if (showValidate) { 242 Composite validateButtonTabGroup = new Composite(composite, SWT.NONE); 243 data = new GridData(); 244 data.horizontalSpan = 2; 245 validateButtonTabGroup.setLayoutData(data); 246 validateButtonTabGroup.setLayout(new FillLayout()); 247 248 validateButton = new Button(validateButtonTabGroup, SWT.CHECK); 249 validateButton.setText(CVSUIMessages.ConfigurationWizardAutoconnectPage_validate); 250 validateButton.addListener(SWT.Selection, new Listener() { 251 public void handleEvent(Event e) { 252 validate = validateButton.getSelection(); 253 } 254 }); 255 } 256 257 allowCachingButton = new Button(composite, SWT.CHECK); 258 allowCachingButton.setText(CVSUIMessages.UserValidationDialog_6); 259 data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL); 260 data.horizontalSpan = 3; 261 allowCachingButton.setLayoutData(data); 262 allowCachingButton.addSelectionListener(new SelectionAdapter() { 263 public void widgetSelected(SelectionEvent e) { 264 allowCaching = allowCachingButton.getSelection(); 265 } 266 }); 267 268 Composite warningComposite = new Composite(composite, SWT.NONE); 269 layout = new GridLayout(); 270 layout.numColumns = 2; 271 layout.marginHeight = 0; 272 layout.marginHeight = 0; 273 warningComposite.setLayout(layout); 274 data = new GridData(GridData.FILL_HORIZONTAL); 275 data.horizontalSpan = 3; 276 warningComposite.setLayoutData(data); 277 Label warningLabel = new Label(warningComposite, SWT.NONE); 278 warningLabel.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); 279 warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING)); 280 Label warningText = new Label(warningComposite, SWT.WRAP); 281 warningText.setText(CVSUIMessages.UserValidationDialog_7); 282 data = new GridData(GridData.FILL_HORIZONTAL); 283 data.widthHint = 300; 284 GC gc= new GC(composite); 285 gc.setFont(parent.getFont()); 286 FontMetrics fontMetrics= gc.getFontMetrics(); 287 gc.dispose(); 288 data.heightHint= Dialog.convertHeightInCharsToPixels(fontMetrics, 3); 289 warningText.setLayoutData(data); 290 291 Link extPrefLink = new Link(composite, SWT.NONE); 292 extPrefLink.setText(CVSUIMessages.ConfigurationWizardMainPage_7); 293 extPrefLink.addSelectionListener(new SelectionAdapter() { 294 295 public void widgetSelected(SelectionEvent e) { 296 PreferenceDialog prefDialog = PreferencesUtil.createPreferenceDialogOn(getShell(), 297 "org.eclipse.team.cvs.ui.ExtMethodPreferencePage", new String [] { 299 "org.eclipse.team.cvs.ui.cvs", "org.eclipse.team.cvs.ui.ExtMethodPreferencePage", "org.eclipse.jsch.ui.SSHPreferences", "org.eclipse.ui.net.NetPreferences"}, null); 304 prefDialog.open(); 305 } 306 307 }); 308 309 310 initializeValues(); 311 updateWidgetEnablements(); 312 hostCombo.setFocus(); 313 314 setControl(composite); 315 Dialog.applyDialogFont(parent); 316 } 317 323 protected Combo createEditableCombo(Composite parent) { 324 Combo combo = new Combo(parent, SWT.NULL); 325 GridData data = new GridData(GridData.FILL_HORIZONTAL); 326 data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH; 327 combo.setLayoutData(data); 328 return combo; 329 } 330 331 protected Group createGroup(Composite parent, String text) { 332 Group group = new Group(parent, SWT.NULL); 333 group.setText(text); 334 GridData data = new GridData(GridData.FILL_HORIZONTAL); 335 data.horizontalSpan = 2; 336 338 group.setLayoutData(data); 339 GridLayout layout = new GridLayout(); 340 layout.numColumns = 2; 341 group.setLayout(layout); 342 return group; 343 } 344 345 348 private Properties createProperties() { 349 Properties result = new Properties (); 350 result.setProperty("connection", connectionMethodCombo.getText()); result.setProperty("user", userCombo.getText()); result.setProperty("password", passwordText.getText()); result.setProperty("host", hostCombo.getText()); if (useCustomPort.getSelection()) { 355 result.setProperty("port", portText.getText()); } 357 result.setProperty("root", repositoryPathCombo.getText()); return result; 359 } 360 361 369 public ICVSRepositoryLocation getLocation() throws CVSException { 370 if (location == null) { 371 if (!isPageComplete()) return null; 372 location = CVSRepositoryLocation.fromProperties(createProperties()); 373 if (location.equals(oldLocation)) { 374 location = oldLocation; 375 } 376 location.setAllowCaching(allowCaching); 377 oldLocation = null; 378 saveWidgetValues(); 379 } 380 return location; 381 } 382 383 386 private void initializeValues() { 387 IDialogSettings settings = getDialogSettings(); 389 if (settings != null) { 390 String [] hostNames = settings.getArray(STORE_HOSTNAME_ID); 391 if (hostNames != null) { 392 for (int i = 0; i < hostNames.length; i++) { 393 hostCombo.add(hostNames[i]); 394 } 395 } 396 String [] paths = settings.getArray(STORE_PATH_ID); 397 if (paths != null) { 398 for (int i = 0; i < paths.length; i++) { 399 repositoryPathCombo.add(paths[i]); 400 } 401 } 402 String [] userNames = settings.getArray(STORE_USERNAME_ID); 403 if (userNames != null) { 404 for (int i = 0; i < userNames.length; i++) { 405 userCombo.add(userNames[i]); 406 } 407 } 408 userCombo.add(ANONYMOUS_USER); 409 if (showValidate) { 410 validate = !settings.getBoolean(STORE_DONT_VALIDATE_ID); 411 validateButton.setSelection(validate); 412 } 413 } 414 415 IConnectionMethod[] methods = CVSRepositoryLocation.getPluggedInConnectionMethods(); 417 for (int i = 0; i < methods.length; i++) { 418 connectionMethodCombo.add(methods[i].getName()); 419 } 420 421 connectionMethodCombo.select(0); 422 useDefaultPort.setSelection(true); 423 424 if(properties != null) { 425 String method = properties.getProperty("connection"); if (method == null) { 427 connectionMethodCombo.select(0); 428 } else { 429 connectionMethodCombo.select(connectionMethodCombo.indexOf(method)); 430 } 431 432 String user = properties.getProperty("user"); if (user != null) { 434 userCombo.setText(user); 435 } 436 437 String password = properties.getProperty("password"); if (password != null) { 439 passwordText.setText(password); 440 } 441 442 String host = properties.getProperty("host"); if (host != null) { 444 hostCombo.setText(host); 445 } 446 447 String port = properties.getProperty("port"); if (port != null) { 449 useCustomPort.setSelection(true); 450 portText.setText(port); 451 } 452 453 String repositoryPath = properties.getProperty("root"); if (repositoryPath != null) { 455 repositoryPathCombo.setText(repositoryPath); 456 } 457 } 458 } 459 462 private void saveWidgetValues() { 463 IDialogSettings settings = getDialogSettings(); 465 if (settings != null) { 466 String userName = userCombo.getText(); 467 if (!userName.equals(ANONYMOUS_USER)) { 468 String [] userNames = settings.getArray(STORE_USERNAME_ID); 469 if (userNames == null) userNames = new String [0]; 470 userNames = addToHistory(userNames, userName); 471 settings.put(STORE_USERNAME_ID, userNames); 472 } 473 String [] hostNames = settings.getArray(STORE_HOSTNAME_ID); 474 if (hostNames == null) hostNames = new String [0]; 475 hostNames = addToHistory(hostNames, hostCombo.getText()); 476 settings.put(STORE_HOSTNAME_ID, hostNames); 477 478 String [] paths = settings.getArray(STORE_PATH_ID); 479 if (paths == null) paths = new String [0]; 480 paths = addToHistory(paths, repositoryPathCombo.getText()); 481 settings.put(STORE_PATH_ID, paths); 482 483 if (showValidate) { 484 settings.put(STORE_DONT_VALIDATE_ID, !validate); 485 } 486 } 487 } 488 489 public void setShowValidate(boolean showValidate) { 490 this.showValidate = showValidate; 491 } 492 493 498 public void setProperties(Properties properties) { 499 this.properties = properties; 500 } 501 502 505 protected void updateWidgetEnablements() { 506 if (useDefaultPort.getSelection()) { 507 portText.setEnabled(false); 508 } else { 509 portText.setEnabled(true); 510 } 511 512 validateFields(); 513 } 514 518 private void validateFields() { 519 String user = userCombo.getText(); 520 IStatus status = validateUserName(user); 521 if (!isStatusOK(status)) { 522 return; 523 } 524 525 String host = hostCombo.getText(); 526 status = validateHost(host); 527 if (!isStatusOK(status)) { 528 return; 529 } 530 531 if (portText.isEnabled()) { 532 String port = portText.getText(); 533 status = validatePort(port); 534 if (!isStatusOK(status)) { 535 return; 536 } 537 } 538 539 String pathString = repositoryPathCombo.getText(); 540 status = validatePath(pathString); 541 if (!isStatusOK(status)) { 542 return; 543 } 544 545 try { 546 CVSRepositoryLocation l = CVSRepositoryLocation.fromProperties(createProperties()); 547 if (!l.equals(oldLocation) && KnownRepositories.getInstance().isKnownRepository(l.getLocation())) { 548 setErrorMessage(CVSUIMessages.ConfigurationWizardMainPage_0); 549 setPageComplete(false); 550 return; 551 } 552 } catch (CVSException e) { 553 CVSUIPlugin.log(e); 554 } 556 557 setErrorMessage(null); 559 setPageComplete(true); 560 } 561 562 private boolean isStatusOK(IStatus status) { 563 if (!status.isOK()) { 564 if (status.getCode() == REQUIRED_FIELD) { 565 setErrorMessage(null); 567 } else { 568 setErrorMessage(status.getMessage()); 569 } 570 setPageComplete(false); 571 return false; 572 } 573 return true; 574 } 575 576 public boolean getValidate() { 577 return validate; 578 } 579 public void setVisible(boolean visible) { 580 super.setVisible(visible); 581 if (visible) { 582 hostCombo.setFocus(); 583 } 584 } 585 586 public static final int REQUIRED_FIELD = 1; 587 public static final int INVALID_FIELD_CONTENTS = 2; 588 public static final IStatus validateUserName(String user) { 589 if (user.length() == 0) { 590 return new Status(IStatus.ERROR, CVSUIPlugin.ID, REQUIRED_FIELD, CVSUIMessages.ConfigurationWizardMainPage_1, null); 591 } 592 if ((user.indexOf('@') != -1) || (user.indexOf(':') != -1)) { 593 return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 594 CVSUIMessages.ConfigurationWizardMainPage_invalidUserName, null); 595 } 596 if (user.startsWith(" ") || user.endsWith(" ")) { return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 598 CVSUIMessages.ConfigurationWizardMainPage_6, null); 599 } 600 return Status.OK_STATUS; 601 } 602 public static final IStatus validateHost(String host) { 603 if (host.length() == 0) { 604 return new Status(IStatus.ERROR, CVSUIPlugin.ID, REQUIRED_FIELD, CVSUIMessages.ConfigurationWizardMainPage_2, null); 605 } 606 if (host.indexOf(':') != -1) { 607 return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 608 CVSUIMessages.ConfigurationWizardMainPage_invalidHostName, null); 609 } 610 if (host.startsWith(" ") || host.endsWith(" ")) { return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 612 CVSUIMessages.ConfigurationWizardMainPage_5, null); 613 } 614 return Status.OK_STATUS; 615 } 616 public static final IStatus validatePort(String port) { 617 if (port.length() == 0) { 618 return new Status(IStatus.ERROR, CVSUIPlugin.ID, REQUIRED_FIELD, CVSUIMessages.ConfigurationWizardMainPage_3, null); 619 } 620 try { 621 Integer.parseInt(port); 622 } catch (NumberFormatException e) { 623 return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 624 CVSUIMessages.ConfigurationWizardMainPage_invalidPort, null); 625 } 626 return Status.OK_STATUS; 627 } 628 public static final IStatus validatePath(String pathString) { 629 if (pathString.length() == 0) { 630 return new Status(IStatus.ERROR, CVSUIPlugin.ID, REQUIRED_FIELD, CVSUIMessages.ConfigurationWizardMainPage_4, null); 631 } 632 IPath path = new Path(null, pathString); 633 String [] segments = path.segments(); 634 for (int i = 0; i < segments.length; i++) { 635 String string = segments[i]; 636 if (string.charAt(0) == ' ' || string.charAt(string.length() -1) == ' ') { 637 return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 638 CVSUIMessages.ConfigurationWizardMainPage_invalidPathWithSpaces, null); 639 } 640 } 641 if (pathString.indexOf("//") != -1) { if (pathString.indexOf("//") == 2) { return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 646 CVSUIMessages.ConfigurationWizardMainPage_useNTFormat, null); 647 } else { 648 return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 649 CVSUIMessages.ConfigurationWizardMainPage_invalidPathWithSlashes, null); 650 } 651 } 652 if (pathString.endsWith("/")) { return new Status(IStatus.ERROR, CVSUIPlugin.ID, INVALID_FIELD_CONTENTS, 654 CVSUIMessages.ConfigurationWizardMainPage_invalidPathWithTrailingSlash, null); 655 } 656 return Status.OK_STATUS; 657 } 658 } 659 | Popular Tags |