1 12 package org.eclipse.ui.internal.ide.dialogs; 13 14 import java.util.Set ; 15 16 import org.eclipse.core.resources.IPathVariableManager; 17 import org.eclipse.core.resources.IResource; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.Path; 20 import org.eclipse.jface.dialogs.Dialog; 21 import org.eclipse.jface.dialogs.IDialogConstants; 22 import org.eclipse.jface.dialogs.IMessageProvider; 23 import org.eclipse.jface.dialogs.TitleAreaDialog; 24 import org.eclipse.swt.SWT; 25 import org.eclipse.swt.events.ModifyEvent; 26 import org.eclipse.swt.events.ModifyListener; 27 import org.eclipse.swt.events.SelectionAdapter; 28 import org.eclipse.swt.events.SelectionEvent; 29 import org.eclipse.swt.layout.FormAttachment; 30 import org.eclipse.swt.layout.FormData; 31 import org.eclipse.swt.layout.FormLayout; 32 import org.eclipse.swt.layout.GridData; 33 import org.eclipse.swt.widgets.Button; 34 import org.eclipse.swt.widgets.Composite; 35 import org.eclipse.swt.widgets.Control; 36 import org.eclipse.swt.widgets.DirectoryDialog; 37 import org.eclipse.swt.widgets.FileDialog; 38 import org.eclipse.swt.widgets.Label; 39 import org.eclipse.swt.widgets.Shell; 40 import org.eclipse.swt.widgets.Text; 41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 42 43 49 public class PathVariableDialog extends TitleAreaDialog { 50 51 private Button okButton; 53 54 private Label variableNameLabel; 55 56 private Label variableValueLabel; 57 58 private Text variableNameField; 59 60 private Text variableValueField; 61 62 private Button fileButton; 63 64 private Button folderButton; 65 66 70 private int type; 71 72 76 private int variableType; 77 78 81 private String variableName; 82 83 86 private String variableValue; 87 88 92 private String originalName; 93 94 98 private boolean newVariable; 99 100 104 private IPathVariableManager pathVariableManager; 105 106 110 private Set namesInUse; 111 112 120 private int validationStatus; 121 122 126 private String validationMessage; 127 128 131 private boolean nameEntered = false; 132 133 136 private boolean locationEntered = false; 137 138 142 final private String standardMessage; 143 144 148 public final static int NEW_VARIABLE = 1; 149 150 154 public final static int EXISTING_VARIABLE = 2; 155 156 167 public PathVariableDialog(Shell parentShell, int type, int variableType, 168 IPathVariableManager pathVariableManager, Set namesInUse) { 169 super(parentShell); 170 setShellStyle(getShellStyle() | SWT.RESIZE); 171 this.type = type; 172 this.newVariable = type == NEW_VARIABLE; 173 this.variableName = ""; this.variableValue = ""; this.variableType = variableType; 176 this.pathVariableManager = pathVariableManager; 177 this.namesInUse = namesInUse; 178 179 if (newVariable) { 180 this.standardMessage = IDEWorkbenchMessages.PathVariableDialog_message_newVariable; 181 } else { 182 this.standardMessage = IDEWorkbenchMessages.PathVariableDialog_message_existingVariable; 183 } 184 } 185 186 191 protected void configureShell(Shell shell) { 192 super.configureShell(shell); 193 if (newVariable) { 194 shell.setText(IDEWorkbenchMessages.PathVariableDialog_shellTitle_newVariable); 195 } else { 196 shell 197 .setText(IDEWorkbenchMessages.PathVariableDialog_shellTitle_existingVariable); 198 } 199 } 200 201 206 protected Control createDialogArea(Composite parent) { 207 Composite parentComposite = (Composite) super.createDialogArea(parent); 209 210 initializeDialogUnits(parentComposite); 211 212 Composite contents = createComposite(parentComposite); 214 215 createWidgets(contents); 217 218 if (type == EXISTING_VARIABLE) { 220 nameEntered = locationEntered = true; 221 validateVariableValue(); 222 } 223 224 Dialog.applyDialogFont(parentComposite); 225 226 return contents; 227 } 228 229 235 private Composite createComposite(Composite parentComposite) { 236 Composite contents = new Composite(parentComposite, SWT.NONE); 238 239 FormLayout layout = new FormLayout(); 240 241 contents.setLayout(layout); 242 contents.setLayoutData(new GridData(GridData.FILL_BOTH)); 243 244 if (newVariable) { 245 setTitle(IDEWorkbenchMessages.PathVariableDialog_dialogTitle_newVariable); 246 } else { 247 setTitle(IDEWorkbenchMessages.PathVariableDialog_dialogTitle_existingVariable); 248 } 249 setMessage(standardMessage); 250 return contents; 251 } 252 253 258 private void createWidgets(Composite contents) { 259 FormData data; 260 261 String nameLabelText = IDEWorkbenchMessages.PathVariableDialog_variableName; 262 String valueLabelText = IDEWorkbenchMessages.PathVariableDialog_variableValue; 263 264 variableNameLabel = new Label(contents, SWT.LEFT); 266 variableNameLabel.setText(nameLabelText); 267 268 data = new FormData(); 269 data.top = new FormAttachment(0, 270 convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN)); 271 data.left = new FormAttachment(0, 272 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN)); 273 variableNameLabel.setLayoutData(data); 274 275 variableNameField = new Text(contents, SWT.SINGLE | SWT.BORDER); 277 variableNameField.setText(variableName); 278 variableNameField.addModifyListener(new ModifyListener() { 279 public void modifyText(ModifyEvent event) { 280 variableNameModified(); 281 } 282 }); 283 284 variableValueLabel = new Label(contents, SWT.LEFT); 286 variableValueLabel.setText(valueLabelText); 287 288 data = new FormData(); 289 data.left = new FormAttachment(0, 290 convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN)); 291 data.top = new FormAttachment(variableNameLabel, 292 convertVerticalDLUsToPixels(5)); 293 variableValueLabel.setLayoutData(data); 294 295 variableValueField = new Text(contents, SWT.SINGLE | SWT.BORDER); 297 variableValueField.setText(variableValue); 298 variableValueField.addModifyListener(new ModifyListener() { 299 public void modifyText(ModifyEvent event) { 300 variableValueModified(); 301 } 302 }); 303 304 fileButton = new Button(contents, SWT.PUSH); 306 fileButton.setText(IDEWorkbenchMessages.PathVariableDialog_file); 307 if ((variableType & IResource.FILE) == 0) { 308 fileButton.setEnabled(false); 309 } 310 311 data = setButtonFormLayoutData(fileButton); 312 data.top = new FormAttachment(variableValueLabel, 0, SWT.CENTER); 313 data.right = new FormAttachment(100, 314 -convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN)); 315 fileButton.setLayoutData(data); 316 317 fileButton.addSelectionListener(new SelectionAdapter() { 318 public void widgetSelected(SelectionEvent e) { 319 selectFile(); 320 } 321 }); 322 323 folderButton = new Button(contents, SWT.PUSH); 325 folderButton.setText(IDEWorkbenchMessages.PathVariableDialog_folder); 326 if ((variableType & IResource.FOLDER) == 0) { 327 folderButton.setEnabled(false); 328 } 329 330 data = setButtonFormLayoutData(folderButton); 331 data.top = new FormAttachment(fileButton, convertVerticalDLUsToPixels(2)); 332 data.right = new FormAttachment(100, 333 -convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN)); 334 folderButton.setLayoutData(data); 335 336 folderButton.addSelectionListener(new SelectionAdapter() { 337 public void widgetSelected(SelectionEvent e) { 338 selectFolder(); 339 } 340 }); 341 342 345 Label largerLabel = nameLabelText.length() > valueLabelText.length() ? variableNameLabel 347 : variableValueLabel; 348 349 data = new FormData(); 350 data.left = new FormAttachment(largerLabel, 351 convertHorizontalDLUsToPixels(5)); 352 data.right = new FormAttachment(fileButton, -convertHorizontalDLUsToPixels(5)); 353 data.top = new FormAttachment(variableNameLabel, 354 convertVerticalDLUsToPixels(5), SWT.CENTER); 355 variableNameField.setLayoutData(data); 356 357 358 data = new FormData(); 359 data.left = new FormAttachment(largerLabel, 360 convertHorizontalDLUsToPixels(5)); 361 data.right = new FormAttachment(fileButton, -convertHorizontalDLUsToPixels(5)); 362 data.top = new FormAttachment(variableValueLabel, 0, SWT.CENTER); 363 variableValueField.setLayoutData(data); 364 365 366 367 } 368 369 378 private FormData setButtonFormLayoutData(Button button) { 379 FormData data = new FormData(); 380 int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH); 381 data.width = Math.max(widthHint, button.computeSize(SWT.DEFAULT, 382 SWT.DEFAULT, true).x); 383 button.setLayoutData(data); 384 return data; 385 } 386 387 391 private void variableNameModified() { 392 variableName = variableNameField.getText(); 394 validationStatus = IMessageProvider.NONE; 395 okButton.setEnabled(validateVariableName() && validateVariableValue()); 396 nameEntered = true; 397 } 398 399 403 private void variableValueModified() { 404 variableValue = variableValueField.getText().trim(); 406 validationStatus = IMessageProvider.NONE; 407 okButton.setEnabled(validateVariableValue() && validateVariableName()); 408 locationEntered = true; 409 } 410 411 414 private void selectFolder() { 415 DirectoryDialog dialog = new DirectoryDialog(getShell()); 416 dialog.setText(IDEWorkbenchMessages.PathVariableDialog_selectFolderTitle); 417 dialog.setMessage(IDEWorkbenchMessages.PathVariableDialog_selectFolderMessage); 418 dialog.setFilterPath(variableValue); 419 String res = dialog.open(); 420 if (res != null) { 421 variableValue = new Path(res).makeAbsolute().toOSString(); 422 variableValueField.setText(variableValue); 423 } 424 } 425 426 429 private void selectFile() { 430 FileDialog dialog = new FileDialog(getShell()); 431 dialog.setText(IDEWorkbenchMessages.PathVariableDialog_selectFileTitle); 432 dialog.setFilterPath(variableValue); 433 String res = dialog.open(); 434 if (res != null) { 435 variableValue = new Path(res).makeAbsolute().toOSString(); 436 variableValueField.setText(variableValue); 437 } 438 } 439 440 445 protected void createButtonsForButtonBar(Composite parent) { 446 okButton = createButton(parent, IDialogConstants.OK_ID, 447 IDialogConstants.OK_LABEL, true); 448 okButton.setEnabled(type == EXISTING_VARIABLE); 449 450 createButton(parent, IDialogConstants.CANCEL_ID, 451 IDialogConstants.CANCEL_LABEL, false); 452 } 453 454 459 private boolean validateVariableName() { 460 boolean allowFinish = false; 461 462 if (validationStatus == IMessageProvider.ERROR) { 464 return false; 465 } 466 467 String message = standardMessage; 469 int newValidationStatus = IMessageProvider.NONE; 470 471 if (variableName.length() == 0) { 472 if (nameEntered) { 474 newValidationStatus = IMessageProvider.ERROR; 476 message = IDEWorkbenchMessages.PathVariableDialog_variableNameEmptyMessage; 477 } 478 } else { 479 IStatus status = pathVariableManager.validateName(variableName); 480 if (!status.isOK()) { 481 newValidationStatus = IMessageProvider.ERROR; 483 message = status.getMessage(); 484 } else if (namesInUse.contains(variableName) 485 && !variableName.equals(originalName)) { 486 message = IDEWorkbenchMessages.PathVariableDialog_variableAlreadyExistsMessage; 488 newValidationStatus = IMessageProvider.ERROR; 489 } else { 490 allowFinish = true; 491 } 492 } 493 494 if (validationStatus == IMessageProvider.NONE 497 || newValidationStatus == IMessageProvider.ERROR) { 498 validationStatus = newValidationStatus; 499 validationMessage = message; 500 } 501 if (allowFinish == false) { 504 setMessage(validationMessage, validationStatus); 505 } 506 return allowFinish; 507 } 508 509 514 private boolean validateVariableValue() { 515 boolean allowFinish = false; 516 517 if (validationStatus == IMessageProvider.ERROR) { 519 return false; 520 } 521 522 String message = standardMessage; 524 int newValidationStatus = IMessageProvider.NONE; 525 526 if (variableValue.length() == 0) { 527 if (locationEntered) { 529 newValidationStatus = IMessageProvider.ERROR; 531 message = IDEWorkbenchMessages.PathVariableDialog_variableValueEmptyMessage; 532 } 533 } else if (!Path.EMPTY.isValidPath(variableValue)) { 534 message = IDEWorkbenchMessages.PathVariableDialog_variableValueInvalidMessage; 536 newValidationStatus = IMessageProvider.ERROR; 537 } else if (!new Path(variableValue).isAbsolute()) { 538 message = IDEWorkbenchMessages.PathVariableDialog_pathIsRelativeMessage; 540 newValidationStatus = IMessageProvider.ERROR; 541 } else if (!IDEResourceInfoUtils.exists(variableValue)) { 542 message = IDEWorkbenchMessages.PathVariableDialog_pathDoesNotExistMessage; 544 newValidationStatus = IMessageProvider.WARNING; 545 allowFinish = true; 546 } else { 547 allowFinish = true; 548 } 549 550 if (validationStatus == IMessageProvider.NONE 553 || newValidationStatus > validationStatus) { 554 validationStatus = newValidationStatus; 555 validationMessage = message; 556 } 557 setMessage(validationMessage, validationStatus); 558 return allowFinish; 559 } 560 561 566 public String getVariableName() { 567 return variableName; 568 } 569 570 575 public String getVariableValue() { 576 return variableValue; 577 } 578 579 584 public void setVariableName(String variableName) { 585 this.variableName = variableName.trim(); 586 this.originalName = this.variableName; 587 } 588 589 594 public void setVariableValue(String variableValue) { 595 this.variableValue = variableValue; 596 } 597 598 } 599 | Popular Tags |