1 11 12 package org.eclipse.jface.dialogs; 13 14 import org.eclipse.jface.preference.IPreferenceStore; 15 import org.eclipse.jface.resource.JFaceResources; 16 import org.eclipse.swt.SWT; 17 import org.eclipse.swt.events.SelectionAdapter; 18 import org.eclipse.swt.events.SelectionEvent; 19 import org.eclipse.swt.graphics.Image; 20 import org.eclipse.swt.layout.GridData; 21 import org.eclipse.swt.widgets.Button; 22 import org.eclipse.swt.widgets.Composite; 23 import org.eclipse.swt.widgets.Control; 24 import org.eclipse.swt.widgets.Shell; 25 26 49 public class MessageDialogWithToggle extends MessageDialog { 50 51 55 public static final String ALWAYS = "always"; 57 61 public static final String NEVER = "never"; 63 67 public static final String PROMPT = "prompt"; 69 94 public static MessageDialogWithToggle openError(Shell parent, String title, 95 String message, String toggleMessage, boolean toggleState, 96 IPreferenceStore store, String key) { 97 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 98 title, null, message, ERROR, new String [] { IDialogConstants.OK_LABEL }, 0, toggleMessage, toggleState); 104 dialog.prefStore = store; 105 dialog.prefKey = key; 106 dialog.open(); 107 return dialog; 108 } 109 110 136 public static MessageDialogWithToggle openInformation(Shell parent, 137 String title, String message, String toggleMessage, 138 boolean toggleState, IPreferenceStore store, String key) { 139 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 140 title, null, message, INFORMATION, 142 new String [] { IDialogConstants.OK_LABEL }, 0, toggleMessage, toggleState); 145 dialog.prefStore = store; 146 dialog.prefKey = key; 147 dialog.open(); 148 return dialog; 149 } 150 151 176 public static MessageDialogWithToggle openOkCancelConfirm(Shell parent, 177 String title, String message, String toggleMessage, 178 boolean toggleState, IPreferenceStore store, String key) { 179 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 180 title, null, message, QUESTION, new String [] { IDialogConstants.OK_LABEL, 182 IDialogConstants.CANCEL_LABEL }, 0, toggleMessage, toggleState); 184 dialog.prefStore = store; 185 dialog.prefKey = key; 186 dialog.open(); 187 return dialog; 188 } 189 190 215 public static MessageDialogWithToggle openWarning(Shell parent, 216 String title, String message, String toggleMessage, 217 boolean toggleState, IPreferenceStore store, String key) { 218 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 219 title, null, message, WARNING, new String [] { IDialogConstants.OK_LABEL }, 221 0, toggleMessage, toggleState); 223 dialog.prefStore = store; 224 dialog.prefKey = key; 225 dialog.open(); 226 return dialog; 227 } 228 229 254 public static MessageDialogWithToggle openYesNoCancelQuestion(Shell parent, 255 String title, String message, String toggleMessage, 256 boolean toggleState, IPreferenceStore store, String key) { 257 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 258 title, null, message, QUESTION, new String [] { IDialogConstants.YES_LABEL, 260 IDialogConstants.NO_LABEL, 261 IDialogConstants.CANCEL_LABEL }, 0, toggleMessage, toggleState); 264 dialog.prefStore = store; 265 dialog.prefKey = key; 266 dialog.open(); 267 return dialog; 268 } 269 270 296 public static MessageDialogWithToggle openYesNoQuestion(Shell parent, 297 String title, String message, String toggleMessage, 298 boolean toggleState, IPreferenceStore store, String key) { 299 MessageDialogWithToggle dialog = new MessageDialogWithToggle(parent, 300 title, null, message, QUESTION, new String [] { IDialogConstants.YES_LABEL, 302 IDialogConstants.NO_LABEL }, 0, toggleMessage, toggleState); 304 dialog.prefStore = store; 305 dialog.prefKey = key; 306 dialog.open(); 307 return dialog; 308 } 309 310 317 private String prefKey = null; 318 319 326 private IPreferenceStore prefStore = null; 327 328 332 private Button toggleButton = null; 333 334 339 private String toggleMessage; 340 341 344 private boolean toggleState; 345 346 383 public MessageDialogWithToggle(Shell parentShell, String dialogTitle, 384 Image image, String message, int dialogImageType, 385 String [] dialogButtonLabels, int defaultIndex, 386 String toggleMessage, boolean toggleState) { 387 super(parentShell, dialogTitle, image, message, dialogImageType, 388 dialogButtonLabels, defaultIndex); 389 this.toggleMessage = toggleMessage; 390 this.toggleState = toggleState; 391 setButtonLabels(dialogButtonLabels); 392 } 393 394 397 protected void buttonPressed(int buttonId) { 398 super.buttonPressed(buttonId); 399 400 if (buttonId != IDialogConstants.CANCEL_ID && toggleState 401 && prefStore != null && prefKey != null) { 402 switch (buttonId) { 403 case IDialogConstants.YES_ID: 404 case IDialogConstants.YES_TO_ALL_ID: 405 case IDialogConstants.PROCEED_ID: 406 case IDialogConstants.OK_ID: 407 prefStore.setValue(prefKey, ALWAYS); 408 break; 409 case IDialogConstants.NO_ID: 410 case IDialogConstants.NO_TO_ALL_ID: 411 prefStore.setValue(prefKey, NEVER); 412 break; 413 } 414 } 415 } 416 417 420 protected void createButtonsForButtonBar(Composite parent) { 421 final String [] buttonLabels = getButtonLabels(); 422 final Button[] buttons = new Button[buttonLabels.length]; 423 final int defaultButtonIndex = getDefaultButtonIndex(); 424 425 int suggestedId = IDialogConstants.INTERNAL_ID; 426 for (int i = 0; i < buttonLabels.length; i++) { 427 String label = buttonLabels[i]; 428 int id = mapButtonLabelToButtonID(label, suggestedId); 431 432 if (id == suggestedId) { 434 suggestedId++; 435 } 436 437 Button button = createButton(parent, id, label, 438 defaultButtonIndex == i); 439 buttons[i] = button; 440 441 } 442 setButtons(buttons); 443 } 444 445 448 protected Control createDialogArea(Composite parent) { 449 Composite dialogAreaComposite = (Composite) super 450 .createDialogArea(parent); 451 setToggleButton(createToggleButton(dialogAreaComposite)); 452 return dialogAreaComposite; 453 } 454 455 464 protected Button createToggleButton(Composite parent) { 465 final Button button = new Button(parent, SWT.CHECK | SWT.LEFT); 466 467 GridData data = new GridData(SWT.NONE); 468 data.horizontalSpan = 2; 469 button.setLayoutData(data); 470 button.setFont(parent.getFont()); 471 472 button.addSelectionListener(new SelectionAdapter() { 473 474 public void widgetSelected(SelectionEvent e) { 475 toggleState = button.getSelection(); 476 } 477 478 }); 479 480 return button; 481 } 482 483 488 protected Button getToggleButton() { 489 return toggleButton; 490 } 491 492 498 public IPreferenceStore getPrefStore() { 499 return prefStore; 500 } 501 502 508 public String getPrefKey() { 509 return prefKey; 510 } 511 512 519 public boolean getToggleState() { 520 return toggleState; 521 } 522 523 531 public void setPrefKey(String prefKey) { 532 this.prefKey = prefKey; 533 } 534 535 542 public void setPrefStore(IPreferenceStore prefStore) { 543 this.prefStore = prefStore; 544 } 545 546 554 protected void setToggleButton(Button button) { 555 if (button == null) { 556 throw new NullPointerException ( 557 "A message dialog with toggle may not have a null toggle button.");} 559 if (!button.isDisposed()) { 560 final String text; 561 if (toggleMessage == null) { 562 text = JFaceResources 563 .getString("MessageDialogWithToggle.defaultToggleMessage"); } else { 565 text = toggleMessage; 566 } 567 button.setText(text); 568 button.setSelection(toggleState); 569 } 570 571 this.toggleButton = button; 572 } 573 574 582 protected void setToggleMessage(String message) { 583 this.toggleMessage = message; 584 585 if ((toggleButton != null) && (!toggleButton.isDisposed())) { 586 final String text; 587 if (toggleMessage == null) { 588 text = JFaceResources 589 .getString("MessageDialogWithToggle.defaultToggleMessage"); } else { 591 text = toggleMessage; 592 } 593 toggleButton.setText(text); 594 } 595 } 596 597 605 public void setToggleState(boolean toggleState) { 606 this.toggleState = toggleState; 607 608 if ((toggleButton != null) && (!toggleButton.isDisposed())) { 610 toggleButton.setSelection(toggleState); 611 } 612 } 613 614 622 private int mapButtonLabelToButtonID(String buttonLabel, int defaultId) { 623 if (IDialogConstants.OK_LABEL.equals(buttonLabel)) { 625 return IDialogConstants.OK_ID; 626 } 627 628 if (IDialogConstants.YES_LABEL.equals(buttonLabel)) { 629 return IDialogConstants.YES_ID; 630 } 631 632 if (IDialogConstants.NO_LABEL.equals(buttonLabel)) { 633 return IDialogConstants.NO_ID; 634 } 635 636 if (IDialogConstants.CANCEL_LABEL.equals(buttonLabel)) { 637 return IDialogConstants.CANCEL_ID; 638 } 639 640 if (IDialogConstants.YES_TO_ALL_LABEL.equals(buttonLabel)) { 641 return IDialogConstants.YES_TO_ALL_ID; 642 } 643 644 if (IDialogConstants.SKIP_LABEL.equals(buttonLabel)) { 645 return IDialogConstants.SKIP_ID; 646 } 647 648 if (IDialogConstants.STOP_LABEL.equals(buttonLabel)) { 649 return IDialogConstants.STOP_ID; 650 } 651 652 if (IDialogConstants.ABORT_LABEL.equals(buttonLabel)) { 653 return IDialogConstants.ABORT_ID; 654 } 655 656 if (IDialogConstants.RETRY_LABEL.equals(buttonLabel)) { 657 return IDialogConstants.RETRY_ID; 658 } 659 660 if (IDialogConstants.IGNORE_LABEL.equals(buttonLabel)) { 661 return IDialogConstants.IGNORE_ID; 662 } 663 664 if (IDialogConstants.PROCEED_LABEL.equals(buttonLabel)) { 665 return IDialogConstants.PROCEED_ID; 666 } 667 668 if (IDialogConstants.OPEN_LABEL.equals(buttonLabel)) { 669 return IDialogConstants.OPEN_ID; 670 } 671 672 if (IDialogConstants.CLOSE_LABEL.equals(buttonLabel)) { 673 return IDialogConstants.CLOSE_ID; 674 } 675 676 if (IDialogConstants.BACK_LABEL.equals(buttonLabel)) { 677 return IDialogConstants.BACK_ID; 678 } 679 680 if (IDialogConstants.NEXT_LABEL.equals(buttonLabel)) { 681 return IDialogConstants.NEXT_ID; 682 } 683 684 if (IDialogConstants.FINISH_LABEL.equals(buttonLabel)) { 685 return IDialogConstants.FINISH_ID; 686 } 687 688 if (IDialogConstants.HELP_LABEL.equals(buttonLabel)) { 689 return IDialogConstants.HELP_ID; 690 } 691 692 if (IDialogConstants.NO_TO_ALL_LABEL.equals(buttonLabel)) { 693 return IDialogConstants.NO_TO_ALL_ID; 694 } 695 696 703 return defaultId; 704 } 705 } 706 | Popular Tags |