1 14 package org.wings; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 import org.wings.border.SBorder; 19 import org.wings.border.SEmptyBorder; 20 import org.wings.plaf.OptionPaneCG; 21 import org.wings.session.SessionManager; 22 23 import javax.swing.*; 24 import java.awt.*; 25 import java.awt.event.ActionEvent ; 26 import java.awt.event.ActionListener ; 27 import java.util.StringTokenizer ; 28 29 33 public class SOptionPane 34 extends SDialog 35 implements ActionListener { 36 private final transient static Log log = LogFactory.getLog(SOptionPane.class); 37 38 41 public static final String YES_ACTION = "YES"; 42 43 46 public static final String NO_ACTION = "NO"; 47 48 51 public static final String OK_ACTION = "OK"; 52 53 56 public static final String CANCEL_ACTION = "CANCEL"; 57 58 61 public static final String UNKNOWN_ACTION = "UNKNOWN"; 62 63 66 public static final int OK_OPTION = JOptionPane.OK_OPTION; 67 68 71 public static final int CANCEL_OPTION = JOptionPane.CANCEL_OPTION; 72 73 76 public static final int YES_OPTION = JOptionPane.YES_OPTION; 77 78 81 public static final int NO_OPTION = JOptionPane.NO_OPTION; 82 83 public static final int RESET_OPTION = 999; 84 85 89 public static final int DEFAULT_OPTION = JOptionPane.DEFAULT_OPTION; 90 91 94 public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION; 95 96 99 public static final int OK_CANCEL_RESET_OPTION = OK_CANCEL_OPTION + 1000; 100 101 104 public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION; 105 106 109 public static final int YES_NO_RESET_OPTION = YES_NO_OPTION + 1000; 110 111 114 public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION; 115 116 119 public static final int YES_NO_CANCEL_RESET_OPTION = YES_NO_CANCEL_OPTION + 1000; 120 121 128 public static final int ERROR_MESSAGE = javax.swing.JOptionPane.ERROR_MESSAGE; 129 132 public static final int INFORMATION_MESSAGE = javax.swing.JOptionPane.INFORMATION_MESSAGE; 133 136 public static final int WARNING_MESSAGE = javax.swing.JOptionPane.WARNING_MESSAGE; 137 140 public static final int QUESTION_MESSAGE = javax.swing.JOptionPane.QUESTION_MESSAGE; 141 144 public static final int PLAIN_MESSAGE = javax.swing.JOptionPane.PLAIN_MESSAGE; 145 146 149 private final SContainer contents = new SPanel(new SBorderLayout()); 150 151 154 private final SContainer optionData = new SPanel(new SFlowDownLayout()); 155 156 159 private final SContainer images = new SPanel(); 160 161 164 protected final SContainer optionButtons = new SPanel(new SFlowLayout(SConstants.RIGHT)); 165 166 169 protected final SButton optionOK = createButton("OK"); 170 171 174 protected final SButton optionCancel = createButton("Cancel"); 175 176 179 protected final SButton optionYes = createButton("Yes"); 180 181 184 protected final SButton optionNo = createButton("No"); 185 186 final SBorder empty = new SEmptyBorder(new Insets(6, 24, 6, 24)); 187 188 191 private static final SIcon messageImage = (SIcon) SessionManager.getSession() 192 .getCGManager().getObject("SOptionPane.messageIcon", SIcon.class); 193 194 197 private static final SIcon questionImage = (SIcon) SessionManager.getSession() 198 .getCGManager().getObject("SOptionPane.questionIcon", SIcon.class); 199 200 203 private static final SIcon yesnoImage = (SIcon) SessionManager.getSession() 204 .getCGManager().getObject("SOptionPane.yesnoIcon", SIcon.class); 205 206 209 private static final SIcon errorImage = (SIcon) SessionManager.getSession() 210 .getCGManager().getObject("SOptionPane.errorIcon", SIcon.class); 211 212 protected final SLabel imageLabel = new SLabel(); 216 217 225 protected Object selected = null; 226 227 230 protected SIcon icon; 231 232 235 protected Object message; 236 237 240 protected Object [] options; 241 242 245 protected Object initialValue; 246 247 250 protected int messageType; 251 private Object inputValue; 252 253 257 public SOptionPane() { 258 this(null); 259 } 260 261 266 public SOptionPane(Object message) { 267 this(message, PLAIN_MESSAGE); 268 } 269 270 279 public SOptionPane(Object message, int messageType) { 280 this(message, messageType, DEFAULT_OPTION); 281 } 282 283 295 public SOptionPane(Object message, int messageType, int optionType) { 296 this(message, messageType, optionType, null); 297 } 298 299 312 public SOptionPane(Object message, int messageType, int optionType, SIcon icon) { 313 this(message, messageType, optionType, icon, null); 314 } 315 316 339 public SOptionPane(Object message, int messageType, int optionType, 340 SIcon icon, Object [] options) { 341 this(message, messageType, optionType, icon, options, null); 342 } 343 344 361 public SOptionPane(Object message, int messageType, int optionType, 362 SIcon icon, Object [] options, Object initialValue) { 363 this.message = message; 364 this.options = options; 365 this.initialValue = initialValue; 366 this.icon = icon; 367 SGridLayout layout = new SGridLayout(1); 368 layout.setBorder(1); 369 setLayout(layout); 370 initPanel(); 371 setOptionType(optionType); 372 setMessageType(messageType); 373 } 376 377 384 public final Object getValue() { 385 return selected; 386 } 387 388 public Object getInputValue() { 389 return inputValue; 390 } 391 392 private final void initPanel() { 393 optionButtons.add(optionOK, "OK"); 394 optionButtons.add(optionYes, "YES"); 395 optionButtons.add(optionCancel, "CANCEL"); 396 optionButtons.add(optionNo, "NO"); 397 398 405 images.add(imageLabel); 406 imageLabel.setToolTipText(null); 407 408 optionData.setBorder(empty); 409 contents.add(optionData, SBorderLayout.CENTER); 410 contents.add(images, SBorderLayout.WEST); 411 add(contents); 412 413 add(optionButtons); 414 } 415 416 421 protected final SButton createButton(String label) { 422 SButton b = new SButton(label); 423 b.setName(getName() + label); 424 b.addActionListener(this); 425 return b; 426 } 427 428 public void actionPerformed(ActionEvent e) { 429 log.debug("action " + e); 430 hide(); 431 selected = e.getSource(); 432 433 if (e.getSource() == optionOK) { 434 fireActionPerformed(OK_ACTION); 435 } else if (e.getSource() == optionYes) { 436 fireActionPerformed(YES_ACTION); 437 } else if (e.getSource() == optionCancel) { 438 fireActionPerformed(CANCEL_ACTION); 439 } else if (e.getSource() == optionNo) { 440 fireActionPerformed(NO_ACTION); 441 } else { 442 fireActionPerformed(UNKNOWN_ACTION); 443 } 444 } 445 446 protected void resetOptions() { 447 optionOK.setVisible(false); 448 optionYes.setVisible(false); 449 optionCancel.setVisible(false); 450 optionNo.setVisible(false); 451 452 imageLabel.setVisible(false); 456 } 457 458 SContainer customButtons = null; 459 460 public void setOptions(Object [] options) { 461 resetOptions(); 462 463 if (customButtons == null) 464 customButtons = new SPanel(); 465 466 for (int i = 0; i < options.length; i++) { 467 if (options[i] instanceof SComponent) { 468 if (options[i] instanceof SAbstractButton) 469 ((SAbstractButton) options[i]).addActionListener(this); 470 customButtons.add((SComponent) options[i]); 471 } else { 472 SButton b = new SButton(options[i].toString()); 473 b.addActionListener(this); 474 customButtons.add(b); 475 } 476 } 477 478 add(customButtons); 479 } 480 481 491 public void setMessageType(int newType) { 492 switch (newType) { 493 case ERROR_MESSAGE: 494 { 495 imageLabel.setIcon(errorImage); 496 imageLabel.setToolTipText("Error"); 497 break; 498 } 499 case INFORMATION_MESSAGE: 500 { 501 imageLabel.setIcon(messageImage); 503 imageLabel.setToolTipText("Information"); 504 break; 505 } 506 case WARNING_MESSAGE: 507 { 508 imageLabel.setIcon(yesnoImage); 509 imageLabel.setToolTipText("Warning"); 510 break; 512 } 513 case QUESTION_MESSAGE: 514 { 515 imageLabel.setIcon(questionImage); 517 imageLabel.setToolTipText("Question"); 518 break; 519 } 520 case PLAIN_MESSAGE: 521 default: 522 { 523 imageLabel.setIcon(null); 524 imageLabel.setToolTipText(null); 525 } 527 } 528 529 messageType = newType; 530 } 531 532 538 public int getMessageType() { 539 return messageType; 540 } 541 542 public void setOptionType(int newType) { 543 resetOptions(); 544 545 switch (newType) { 546 case DEFAULT_OPTION: 547 optionOK.setVisible(true); 548 break; 549 550 case OK_CANCEL_OPTION: 551 optionOK.setVisible(true); 552 optionCancel.setVisible(true); 553 break; 554 555 case OK_CANCEL_RESET_OPTION: 556 optionOK.setVisible(true); 557 optionCancel.setVisible(true); 558 break; 559 560 case YES_NO_OPTION: 561 optionYes.setVisible(true); 562 optionNo.setVisible(true); 563 break; 564 565 case YES_NO_RESET_OPTION: 566 optionYes.setVisible(true); 567 optionNo.setVisible(true); 568 break; 569 570 case YES_NO_CANCEL_OPTION: 571 optionYes.setVisible(true); 572 optionNo.setVisible(true); 573 optionCancel.setVisible(true); 574 break; 575 576 case YES_NO_CANCEL_RESET_OPTION: 577 optionYes.setVisible(true); 578 optionNo.setVisible(true); 579 optionCancel.setVisible(true); 580 break; 581 } 582 } 583 584 public void showOption(SComponent c, String title, Object message) { 585 if (title != null) 586 setTitle(title); 587 588 optionData.removeAll(); 589 if (message instanceof SComponent) { 590 optionData.add((SComponent) message); 591 } else { 592 StringTokenizer stt = new StringTokenizer (message.toString(), "\n"); 593 while (stt.hasMoreElements()) { 594 optionData.add(new SLabel(stt.nextElement().toString())); 595 } 596 } 597 show(c); 598 } 599 600 public static void showMessageDialog(SComponent parent, Object message, 601 ActionListener al) { 602 showMessageDialog(parent, message, null, 0, al); 603 } 604 605 public static void showMessageDialog(SComponent parent, Object message) { 606 showMessageDialog(parent, message, null, 0, null); 607 } 608 609 public static void showMessageDialog(SComponent parent, Object message, String title) { 610 showMessageDialog(parent, message, title, 0, null); 611 } 612 613 public static void showMessageDialog(SComponent parent, Object message, 614 String title, int messageType, ActionListener al) { 615 SOptionPane p = new SOptionPane(); 616 617 p.showPlainMessage(parent, message, title); 618 p.addActionListener(al); 619 } 620 621 public static void showPlainMessageDialog(SComponent parent, Object message, 622 String title) { 623 showPlainMessageDialog(parent, message, title, 0, null); 624 } 625 626 627 public static void showPlainMessageDialog(SComponent parent, Object message, 628 String title, int messageType, 629 ActionListener al) { 630 SOptionPane p = new SOptionPane(); 631 632 p.showPlainMessage(parent, message, title); 633 p.addActionListener(al); 634 p.setMessageType(PLAIN_MESSAGE); 635 } 637 638 public void showPlainMessage(SComponent parent, Object message, String title) { 639 showOption(parent, title, message); 640 641 setOptionType(DEFAULT_OPTION); 642 setMessageType(PLAIN_MESSAGE); 643 } 645 646 public void showQuestion(SComponent parent, Object message, String title) { 647 showOption(parent, title, message); 648 649 setOptionType(OK_CANCEL_OPTION); 650 setMessageType(QUESTION_MESSAGE); 651 } 653 654 public void showInput(SComponent parent, Object message, 655 SComponent inputElement, String title) { 656 showOption(parent, title, message); 657 optionData.add(inputElement); 658 inputValue = inputElement; 659 660 setOptionType(OK_CANCEL_OPTION); 661 setMessageType(QUESTION_MESSAGE); 662 } 664 665 669 670 public static void showInputDialog(SComponent parent, Object question, 671 String title, SComponent inputElement, 672 ActionListener al) { 673 SOptionPane p = new SOptionPane(); 674 675 p.showInput(parent, question, inputElement, title); 676 p.addActionListener(al); 677 } 678 679 public static void showQuestionDialog(SComponent parent, Object question, 680 String title, ActionListener al) { 681 SOptionPane p = new SOptionPane(); 682 683 p.showQuestion(parent, question, title); 684 p.addActionListener(al); 685 } 686 687 public static void showPlainQuestionDialog(SComponent parent, Object question, 688 String title, ActionListener al) { 689 SOptionPane p = new SOptionPane(); 690 691 p.showQuestion(parent, question, title); 692 p.addActionListener(al); 693 p.setMessageType(PLAIN_MESSAGE); 694 } 696 697 698 public static void showConfirmDialog(SComponent parent, Object message, 699 String title) { 700 showConfirmDialog(parent, message, title, 0, null); 701 } 702 703 public static void showConfirmDialog(SComponent parent, Object message, 704 String title, ActionListener al) { 705 showConfirmDialog(parent, message, title, 0, al); 706 } 707 708 public static void showConfirmDialog(SComponent parent, Object message, 709 String title, int type) { 710 showConfirmDialog(parent, message, title, type, null); 711 } 712 713 public static void showConfirmDialog(SComponent parent, Object message, 714 String title, int type, ActionListener al) { 715 showConfirmDialog(parent, message, title, type, al, null); 716 } 717 718 public static void showConfirmDialog(SComponent parent, Object message, 719 String title, int type, ActionListener al, 720 SLayoutManager layout) { 721 SOptionPane p = new SOptionPane(); 722 if (layout != null) { 723 p.optionButtons.setLayout(layout); 724 } 726 p.addActionListener(al); 727 p.showQuestion(parent, message, title, type); 728 } 729 730 public void showYesNo(SComponent parent, Object question, String title) { 731 showOption(parent, title, question); 732 setOptionType(YES_NO_OPTION); 733 setMessageType(INFORMATION_MESSAGE); 734 } 736 737 public void showQuestion(SComponent parent, Object question, String title, int type) { 738 showOption(parent, title, question); 739 setOptionType(type); 740 setMessageType(QUESTION_MESSAGE); 741 } 742 743 public static void showYesNoDialog(SComponent parent, Object question, 744 String title, ActionListener al) { 745 SOptionPane p = new SOptionPane(); 746 p.addActionListener(al); 747 748 p.showYesNo(parent, question, title); 749 } 750 751 public void setCG(OptionPaneCG cg) { 752 super.setCG(cg); 753 } 754 } 755 | Popular Tags |