1 19 20 package org.openide; 21 22 import java.awt.BorderLayout ; 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.beans.PropertyChangeListener ; 26 import java.beans.PropertyChangeSupport ; 27 import java.lang.reflect.InvocationTargetException ; 28 import javax.swing.BorderFactory ; 29 import javax.swing.JLabel ; 30 import javax.swing.JOptionPane ; 31 import javax.swing.JPanel ; 32 import javax.swing.JTextArea ; 33 import javax.swing.JTextField ; 34 import javax.swing.UIManager ; 35 import javax.swing.border.CompoundBorder ; 36 import javax.swing.border.EmptyBorder ; 37 import org.openide.awt.Mnemonics; 38 import org.openide.util.NbBundle; 39 40 60 public class NotifyDescriptor extends Object { 61 63 64 public static final String PROP_MESSAGE = "message"; 66 67 public static final String PROP_MESSAGE_TYPE = "messageType"; 69 70 public static final String PROP_OPTION_TYPE = "optionType"; 72 73 public static final String PROP_OPTIONS = "options"; 75 76 public static final String PROP_VALUE = "value"; 78 79 public static final String PROP_TITLE = "title"; 81 82 public static final String PROP_DETAIL = "detail"; 84 85 public static final String PROP_VALID = "valid"; 87 91 92 public static final Object YES_OPTION = new Integer (JOptionPane.YES_OPTION); 93 94 95 public static final Object NO_OPTION = new Integer (JOptionPane.NO_OPTION); 96 97 98 public static final Object CANCEL_OPTION = new Integer (JOptionPane.CANCEL_OPTION); 99 100 101 public static final Object OK_OPTION = new Integer (JOptionPane.OK_OPTION); 102 103 104 public static final Object CLOSED_OPTION = new Integer (JOptionPane.CLOSED_OPTION); 105 106 110 111 public static final int DEFAULT_OPTION = JOptionPane.DEFAULT_OPTION; 112 113 114 public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION; 115 116 117 public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION; 118 119 120 public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION; 121 122 126 127 public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE; 128 129 130 public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE; 131 132 133 public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE; 134 135 136 public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE; 137 138 139 public static final int PLAIN_MESSAGE = JOptionPane.PLAIN_MESSAGE; 140 141 142 private static final int MAXIMUM_TEXT_WIDTH = 100; 143 144 145 private static final int SIZE_PREFERRED_WIDTH = 300; 146 147 148 private static final int SIZE_PREFERRED_HEIGHT = 100; 149 private Object message; 150 151 152 private int messageType = PLAIN_MESSAGE; 153 154 155 private int optionType; 156 157 158 private Object [] options; 159 160 161 private Object [] adOptions; 162 163 164 private Object value; 165 166 167 private Object defaultValue; 168 169 170 private String title; 171 172 173 private boolean valid = true; 174 175 176 177 179 180 private PropertyChangeSupport changeSupport; 181 182 205 public NotifyDescriptor( 206 Object message, String title, int optionType, int messageType, Object [] options, Object initialValue 207 ) { 208 this.message = message; 209 this.messageType = messageType; 210 this.options = options; 211 this.optionType = optionType; 212 this.title = title; 213 this.value = initialValue; 214 this.defaultValue = initialValue; 215 } 216 217 222 protected void initialize() { 223 } 224 225 227 final void getterCalled() { 228 boolean init = false; 229 230 synchronized (this) { 231 if (changeSupport == null) { 232 changeSupport = new java.beans.PropertyChangeSupport (this); 233 init = true; 234 } 235 } 236 237 if (init) { 238 initialize(); 239 } 240 } 241 242 246 252 public final boolean isValid() { 253 getterCalled(); 254 255 return valid; 256 } 257 258 262 public final void setValid(boolean newValid) { 263 boolean oldValid = valid; 264 valid = newValid; 265 firePropertyChange( 266 PROP_VALID, oldValid ? Boolean.TRUE : Boolean.FALSE, newValid ? Boolean.TRUE : Boolean.FALSE 267 ); 268 } 269 270 285 public void setMessage(Object newMessage) { 286 Object oldMessage = message; 287 288 if (newMessage instanceof String ) { 289 JTextArea area = new JTextArea ((String ) newMessage); 291 area.setPreferredSize(new Dimension (SIZE_PREFERRED_WIDTH, SIZE_PREFERRED_HEIGHT)); 292 area.setBackground(UIManager.getColor("Label.background")); area.setBorder(BorderFactory.createEmptyBorder()); 294 area.setLineWrap(true); 295 area.setWrapStyleWord(true); 296 area.setEditable(false); 297 area.getAccessibleContext().setAccessibleName(NbBundle.getMessage(NotifyDescriptor.class, "ACN_NotifyDescriptor_MessageJTextArea")); area.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(NotifyDescriptor.class, "ACD_NotifyDescriptor_MessageJTextArea")); newMessage = area; 300 } 301 302 message = newMessage; 303 firePropertyChange(PROP_MESSAGE, oldMessage, newMessage); 304 } 305 306 312 public Object getMessage() { 313 getterCalled(); 314 315 return message; 316 } 317 318 334 public void setMessageType(int newType) { 335 if ( 336 (newType != ERROR_MESSAGE) && (newType != INFORMATION_MESSAGE) && (newType != WARNING_MESSAGE) && 337 (newType != QUESTION_MESSAGE) && (newType != PLAIN_MESSAGE) 338 ) { 339 throw new IllegalArgumentException ( 340 "Message type must be one of the following:" +" ERROR_MESSAGE, INFORMATION_MESSAGE," +" WARNING_MESSAGE, QUESTION_MESSAGE or PLAIN_MESSAGE." 344 ); 345 } 346 347 int oldType = messageType; 348 messageType = newType; 349 firePropertyChange(PROP_MESSAGE_TYPE, new Integer (oldType), new Integer (messageType)); 350 } 351 352 359 public int getMessageType() { 360 getterCalled(); 361 362 return messageType; 363 } 364 365 380 public void setOptionType(int newType) { 381 if ( 382 (newType != DEFAULT_OPTION) && (newType != YES_NO_OPTION) && (newType != YES_NO_CANCEL_OPTION) && 383 (newType != OK_CANCEL_OPTION) 384 ) { 385 throw new IllegalArgumentException ( 386 "Option type must be one of the following:" +" DEFAULT_OPTION, YES_NO_OPTION," +" YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION." 390 ); 391 } 392 393 int oldType = optionType; 394 optionType = newType; 395 firePropertyChange(PROP_OPTION_TYPE, new Integer (oldType), new Integer (optionType)); 396 } 397 398 405 public int getOptionType() { 406 getterCalled(); 407 408 return optionType; 409 } 410 411 427 public void setOptions(Object [] newOptions) { 428 Object [] oldOptions = options; 429 options = newOptions; 430 firePropertyChange(PROP_OPTIONS, oldOptions, newOptions); 431 } 432 433 439 public Object [] getOptions() { 440 getterCalled(); 441 442 if (options != null) { 443 return (Object []) options.clone(); 444 } 445 446 return options; 447 } 448 449 467 public void setAdditionalOptions(Object [] newOptions) { 468 Object [] oldOptions = adOptions; 469 adOptions = newOptions; 470 firePropertyChange(PROP_OPTIONS, oldOptions, newOptions); 471 } 472 473 479 public Object [] getAdditionalOptions() { 480 getterCalled(); 481 482 if (adOptions != null) { 483 return (Object []) adOptions.clone(); 484 } 485 486 return null; 487 } 488 489 493 void setValueWithoutPCH(Object newValue) { 494 value = newValue; 495 } 496 497 505 public void setValue(Object newValue) { 506 Object oldValue = value; 507 setValueWithoutPCH(newValue); 508 firePropertyChange(PROP_VALUE, oldValue, newValue); 509 } 510 511 518 public Object getValue() { 519 getterCalled(); 520 521 return value; 522 } 523 524 530 public Object getDefaultValue() { 531 return defaultValue; 532 } 533 534 541 public void setTitle(String newTitle) { 542 Object oldTitle = title; 543 title = newTitle; 544 firePropertyChange(PROP_TITLE, oldTitle, newTitle); 545 } 546 547 554 public String getTitle() { 555 getterCalled(); 556 557 return title; 558 } 559 560 591 592 596 601 public void addPropertyChangeListener(PropertyChangeListener listener) { 602 getterCalled(); 603 changeSupport.addPropertyChangeListener(listener); 604 } 605 606 611 public void removePropertyChangeListener(PropertyChangeListener listener) { 612 if (changeSupport != null) { 613 changeSupport.removePropertyChangeListener(listener); 614 } 615 } 616 617 624 protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) { 625 if (changeSupport != null) { 626 changeSupport.firePropertyChange(propertyName, oldValue, newValue); 627 } 628 } 629 630 635 protected static String getTitleForType(int messageType) { 636 switch (messageType) { 637 case ERROR_MESSAGE: 638 return NbBundle.getMessage(NotifyDescriptor.class, "NTF_ErrorTitle"); 639 640 case WARNING_MESSAGE: 641 return NbBundle.getMessage(NotifyDescriptor.class, "NTF_WarningTitle"); 642 643 case QUESTION_MESSAGE: 644 return NbBundle.getMessage(NotifyDescriptor.class, "NTF_QuestionTitle"); 645 646 case INFORMATION_MESSAGE: 647 return NbBundle.getMessage(NotifyDescriptor.class, "NTF_InformationTitle"); 648 649 case PLAIN_MESSAGE: 650 return NbBundle.getMessage(NotifyDescriptor.class, "NTF_PlainTitle"); 651 } 652 653 return ""; } 655 656 660 public static class Message extends NotifyDescriptor { 661 667 public Message(Object message) { 668 this(message, INFORMATION_MESSAGE); 669 } 670 671 678 public Message(Object message, int messageType) { 679 super( 680 message, NotifyDescriptor.getTitleForType(messageType), DEFAULT_OPTION, messageType, 681 new Object [] { OK_OPTION }, OK_OPTION 682 ); 683 } 684 } 685 686 694 public static class Confirmation extends NotifyDescriptor { 695 701 public Confirmation(Object message) { 702 this(message, YES_NO_CANCEL_OPTION); 703 } 704 705 712 public Confirmation(Object message, String title) { 713 this(message, title, YES_NO_CANCEL_OPTION); 714 } 715 716 723 public Confirmation(Object message, int optionType) { 724 this(message, optionType, QUESTION_MESSAGE); 725 } 726 727 735 public Confirmation(Object message, String title, int optionType) { 736 this(message, title, optionType, QUESTION_MESSAGE); 737 } 738 739 747 public Confirmation(Object message, int optionType, int messageType) { 748 super( 749 message, NotifyDescriptor.getTitleForType(messageType), optionType, messageType, 750 (optionType == DEFAULT_OPTION) ? new Object [] { OK_OPTION } : null, OK_OPTION 751 ); 752 } 753 754 763 public Confirmation(Object message, String title, int optionType, int messageType) { 764 super( 765 message, title, optionType, messageType, 766 (optionType == DEFAULT_OPTION) ? new Object [] { OK_OPTION } : null, OK_OPTION 767 ); 768 } 769 } 770 771 777 public static final class Exception extends Confirmation { 778 static final long serialVersionUID = -3387516993124229948L; 779 780 785 public Exception(Throwable detail) { 786 this(detail, detail.getMessage()); 787 788 if (detail instanceof InvocationTargetException ) { 790 Throwable target = ((InvocationTargetException ) detail).getTargetException(); 791 this.setMessage(target); 792 793 Object msgObj = this.getMessage(); 794 if ((msgObj == null) || "".equals(msgObj)) { 796 String msg = target.getMessage(); 797 msg = org.openide.util.Utilities.wrapString( 798 msg, MAXIMUM_TEXT_WIDTH, java.text.BreakIterator.getCharacterInstance(), false 799 ); 800 this.setMessage(msg); 801 } 802 } 803 804 Object obj = this.getMessage(); 805 if ((obj == null) || "".equals(obj)) { this.setMessage( 808 NbBundle.getMessage( 809 NotifyDescriptor.class, "NTF_ExceptionalException", detail.getClass().getName(), 810 System.getProperty("netbeans.user") + java.io.File.separator + "system" 811 ) 812 ); this.setTitle(NbBundle.getMessage(NotifyDescriptor.class, "NTF_ExceptionalExceptionTitle")); 814 } 815 } 816 817 823 public Exception(Throwable detail, Object message) { 824 super(message, DEFAULT_OPTION, ERROR_MESSAGE); 825 826 this.setTitle(NbBundle.getMessage(NotifyDescriptor.class, "NTF_ExceptionTitle")); 829 } 830 } 831 832 835 public static class InputLine extends NotifyDescriptor { 836 839 protected JTextField textField; 840 841 845 public InputLine(final String text, final String title) { 846 this(text, title, OK_CANCEL_OPTION, PLAIN_MESSAGE); 847 } 848 849 856 public InputLine(final String text, final String title, final int optionType, final int messageType) { 857 super(null, title, optionType, messageType, null, null); 858 super.setMessage(createDesign(text)); 859 } 860 861 865 public String getInputText() { 866 return textField.getText(); 867 } 868 869 873 public void setInputText(final String text) { 874 textField.setText(text); 875 textField.selectAll(); 876 } 877 878 882 protected Component createDesign(final String text) { 883 int index; 884 JPanel panel = new JPanel (); 885 886 JLabel textLabel = new JLabel (); 887 Mnemonics.setLocalizedText(textLabel, text); 888 889 textLabel.setBorder(new EmptyBorder (0, 0, 0, 10)); 890 panel.setLayout(new BorderLayout ()); 891 panel.setBorder(new EmptyBorder (11, 12, 1, 11)); 892 textField = new JTextField (25); 893 panel.add(BorderLayout.WEST, textLabel); 894 panel.add(BorderLayout.CENTER, textField); 895 textLabel.setLabelFor(textField); 896 textField.setBorder(new CompoundBorder (textField.getBorder(), new EmptyBorder (2, 0, 2, 0))); 897 textField.requestFocus(); 898 899 javax.swing.KeyStroke enter = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0); 900 javax.swing.text.Keymap map = textField.getKeymap(); 901 902 map.removeKeyStrokeBinding(enter); 903 904 914 panel.getAccessibleContext().setAccessibleDescription( 915 NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputPanel") 916 ); 917 textField.getAccessibleContext().setAccessibleDescription( 918 NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputField") 919 ); 920 921 return panel; 922 } 923 } 924 } 926 | Popular Tags |