KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > NotifyDescriptor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.awt.Component JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.beans.PropertyChangeSupport JavaDoc;
27 import java.lang.reflect.InvocationTargetException JavaDoc;
28 import javax.swing.BorderFactory JavaDoc;
29 import javax.swing.JLabel JavaDoc;
30 import javax.swing.JOptionPane JavaDoc;
31 import javax.swing.JPanel JavaDoc;
32 import javax.swing.JTextArea JavaDoc;
33 import javax.swing.JTextField JavaDoc;
34 import javax.swing.UIManager JavaDoc;
35 import javax.swing.border.CompoundBorder JavaDoc;
36 import javax.swing.border.EmptyBorder JavaDoc;
37 import org.openide.awt.Mnemonics;
38 import org.openide.util.NbBundle;
39
40 /**
41 * This class provides a description of a user notification to be displayed.
42 * <p>Simple example of usage:
43 * <pre>
44 * NotifyDescriptor d =
45 * new NotifyDescriptor.Message("Hello...", NotifyDescriptor.INFORMATION_MESSAGE);
46 * DialogDisplayer.getDefault().notify(d);
47 * </pre>
48 * or to get a result:
49 * <pre>
50 * NotifyDescriptor d =
51 * new NotifyDescriptor.Confirmation("Really do this?!", "Dialog Title",
52 * NotifyDescriptor.OK_CANCEL_OPTION);
53 * if (DialogDisplayer.getDefault().notify(d) == NotifyDescriptor.OK_OPTION) {
54 * // really do it...
55 * }
56 * </pre>
57 * @see DialogDisplayer#notify
58 * @author David Peroutka, Jaroslav Tulach
59 */

60 public class NotifyDescriptor extends Object JavaDoc {
61     // Property constants
62

63     /** Name of property for the message to be displayed. */
64     public static final String JavaDoc PROP_MESSAGE = "message"; // NOI18N
65

66     /** Name of property for the type of message to use. */
67     public static final String JavaDoc PROP_MESSAGE_TYPE = "messageType"; // NOI18N
68

69     /** Name of property for the style of options available. */
70     public static final String JavaDoc PROP_OPTION_TYPE = "optionType"; // NOI18N
71

72     /** Name of property for the exact list of options. */
73     public static final String JavaDoc PROP_OPTIONS = "options"; // NOI18N
74

75     /** Name of property for the value the user selected. */
76     public static final String JavaDoc PROP_VALUE = "value"; // NOI18N
77

78     /** Name of property for the dialog title. */
79     public static final String JavaDoc PROP_TITLE = "title"; // NOI18N
80

81     /** Name of property for the detail message reported. */
82     public static final String JavaDoc PROP_DETAIL = "detail"; // NOI18N
83

84     /** Name of property for the OK button validation. */
85     public static final String JavaDoc PROP_VALID = "valid"; // NOI18N
86

87     //
88
// Return values
89
//
90

91     /** Return value if YES is chosen. */
92     public static final Object JavaDoc YES_OPTION = new Integer JavaDoc(JOptionPane.YES_OPTION);
93
94     /** Return value if NO is chosen. */
95     public static final Object JavaDoc NO_OPTION = new Integer JavaDoc(JOptionPane.NO_OPTION);
96
97     /** Return value if CANCEL is chosen. */
98     public static final Object JavaDoc CANCEL_OPTION = new Integer JavaDoc(JOptionPane.CANCEL_OPTION);
99
100     /** Return value if OK is chosen. */
101     public static final Object JavaDoc OK_OPTION = new Integer JavaDoc(JOptionPane.OK_OPTION);
102
103     /** Return value if user closes the window without pressing any button. */
104     public static final Object JavaDoc CLOSED_OPTION = new Integer JavaDoc(JOptionPane.CLOSED_OPTION);
105
106     //
107
// Option types
108
//
109

110     /** Option type used by default. */
111     public static final int DEFAULT_OPTION = JOptionPane.DEFAULT_OPTION;
112
113     /** Option type used for negatable confirmations. */
114     public static final int YES_NO_OPTION = JOptionPane.YES_NO_OPTION;
115
116     /** Option type used for negatable and cancellable confirmations. */
117     public static final int YES_NO_CANCEL_OPTION = JOptionPane.YES_NO_CANCEL_OPTION;
118
119     /** Option type used for cancellable confirmations. */
120     public static final int OK_CANCEL_OPTION = JOptionPane.OK_CANCEL_OPTION;
121
122     //
123
// Message types
124
//
125

126     /** Message type for error messages. */
127     public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
128
129     /** Message type for information messages. */
130     public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
131
132     /** Message type for warning messages. */
133     public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE;
134
135     /** Message type for questions. */
136     public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;
137
138     /** Plain message type using no icon. */
139     public static final int PLAIN_MESSAGE = JOptionPane.PLAIN_MESSAGE;
140
141     /** Maximum text width to which the text is wrapped */
142     private static final int MAXIMUM_TEXT_WIDTH = 100;
143
144     /** preferred width of text area */
145     private static final int SIZE_PREFERRED_WIDTH = 300;
146
147     /** preferred height of text area */
148     private static final int SIZE_PREFERRED_HEIGHT = 100;
149     private Object JavaDoc message;
150
151     /** The message type. */
152     private int messageType = PLAIN_MESSAGE;
153
154     /** The option type specifying the user-selectable options. */
155     private int optionType;
156
157     /** The option object specifying the user-selectable options. */
158     private Object JavaDoc[] options;
159
160     /** The option object specifying the additional user-selectable options. */
161     private Object JavaDoc[] adOptions;
162
163     /** The user's choice value object. */
164     private Object JavaDoc value;
165
166     /** The default initial value. */
167     private Object JavaDoc defaultValue;
168
169     /** The title string for the report. */
170     private String JavaDoc title;
171
172     /** Is OK button valid (enabled). */
173     private boolean valid = true;
174
175     /** The object specifying the detail object. */
176
177     // private Object detail;
178

179     /** Property change support. */
180     private PropertyChangeSupport JavaDoc changeSupport;
181
182     /**
183     * Creates a new notify descriptor with specified information to report.
184     *
185     * If <code>optionType</code> is {@link #YES_NO_OPTION} or {@link #YES_NO_CANCEL_OPTION}
186     * and the <code>options</code> parameter is <code>null</code>, then the options are
187     * supplied by the look and feel.
188     *
189     * The <code>messageType</code> parameter is primarily used to supply a
190     * default icon from the look and feel.
191     *
192     * @param message the object to display
193     * @param title the title string for the dialog
194     * @param optionType indicates which options are available
195     * @param messageType indicates what type of message should be displayed
196     * @param options an array of objects indicating the possible choices
197     * @param initialValue the object that represents the default value
198     *
199     * @see #getMessage
200     * @see #getMessageType
201     * @see #getOptions
202     * @see #getOptionType
203     * @see #getValue
204     */

205     public NotifyDescriptor(
206         Object JavaDoc message, String JavaDoc title, int optionType, int messageType, Object JavaDoc[] options, Object JavaDoc 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     /** Method that is called before a value is returned from any of
218      * getter methods in this object.
219      *
220      * Allows subclasses to do some additional initialization actions.
221      */

222     protected void initialize() {
223     }
224
225     /** Checks for initialization.
226      */

227     final void getterCalled() {
228         boolean init = false;
229
230         synchronized (this) {
231             if (changeSupport == null) {
232                 changeSupport = new java.beans.PropertyChangeSupport JavaDoc(this);
233                 init = true;
234             }
235         }
236
237         if (init) {
238             initialize();
239         }
240     }
241
242     //
243
// Getters/setters for properties.
244
//
245

246     /**
247     * Return true if OK button is valid (enabled), otherwise return false.
248     * @see #setValid
249     *
250     * @return validity status of OK button.
251     */

252     public final boolean isValid() {
253         getterCalled();
254
255         return valid;
256     }
257
258     /** Set validity of OK button.
259      * @see #isValid
260      * @param newValid validity status of OK button
261      */

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     /**
271     * Define a descriptive message to be reported. In the most common
272     * usage, the message is just a <code>String</code>. However, the type
273     * of this parameter is actually <code>Object</code>. Its interpretation depends on
274     * its type:
275     * <dl compact>
276     * <dt><code>Object[]</code><dd> A recursively interpreted series of messages.
277     * <dt>{@link Component}<dd> The <code>Component</code> is displayed in the dialog.
278     * <dt>{@link javax.swing.Icon}<dd> The <code>Icon</code> is wrapped in a {@link JLabel} and displayed in the dialog.
279     * <dt>anything else<dd> The {@link Object#toString string representation} of the object.
280     * </dl>
281     *
282     * @param newMessage the <code>Object</code> to report
283     * @see #getMessage
284     */

285     public void setMessage(Object JavaDoc newMessage) {
286         Object JavaDoc oldMessage = message;
287
288         if (newMessage instanceof String JavaDoc) {
289             // bugfix #25457, use JTextArea for word-wrapping
290
JTextArea JavaDoc area = new JTextArea JavaDoc((String JavaDoc) newMessage);
291             area.setPreferredSize(new Dimension JavaDoc(SIZE_PREFERRED_WIDTH, SIZE_PREFERRED_HEIGHT));
292             area.setBackground(UIManager.getColor("Label.background")); // NOI18N
293
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")); // NOI18N
298
area.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(NotifyDescriptor.class, "ACD_NotifyDescriptor_MessageJTextArea")); // NOI18N
299
newMessage = area;
300         }
301
302         message = newMessage;
303         firePropertyChange(PROP_MESSAGE, oldMessage, newMessage);
304     }
305
306     /**
307     * Get the message object.
308     * @see #setMessage
309     *
310     * @return the <code>Object</code> that is to be reported
311     */

312     public Object JavaDoc getMessage() {
313         getterCalled();
314
315         return message;
316     }
317
318     /**
319     * Define the style of the message. The look and feel manager may lay out
320     * the dialog differently depending on this value, and will often provide
321     * a default icon. The possible values are:
322     * <ul>
323     * <li>{@link #ERROR_MESSAGE}
324     * <li>{@link #INFORMATION_MESSAGE}
325     * <li>{@link #WARNING_MESSAGE}
326     * <li>{@link #QUESTION_MESSAGE}
327     * <li>{@link #PLAIN_MESSAGE}
328     * </ul>
329     *
330     * @param newType the kind of message
331     *
332     * @see #getMessageType
333     */

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 JavaDoc(
340                 "Message type must be one of the following:" // NOI18N
341
+" ERROR_MESSAGE, INFORMATION_MESSAGE," // NOI18N
342
+" WARNING_MESSAGE, QUESTION_MESSAGE or PLAIN_MESSAGE." // NOI18N
343

344             );
345         }
346
347         int oldType = messageType;
348         messageType = newType;
349         firePropertyChange(PROP_MESSAGE_TYPE, new Integer JavaDoc(oldType), new Integer JavaDoc(messageType));
350     }
351
352     /**
353     * Get the message type.
354     *
355     * @return the message type
356     *
357     * @see #setMessageType
358     */

359     public int getMessageType() {
360         getterCalled();
361
362         return messageType;
363     }
364
365     /**
366     * Define the set of options. The option type is used by the look and
367     * feel to determine what options to show (unless explicit options are supplied):
368     * <ul>
369     * <li>{@link #DEFAULT_OPTION}
370     * <li>{@link #YES_NO_OPTION}
371     * <li>{@link #YES_NO_CANCEL_OPTION}
372     * <li>{@link #OK_CANCEL_OPTION}
373     * </ul>
374     *
375     * @param newType the options the look and feel is to display
376     *
377     * @see #getOptionType
378     * @see #setOptions
379     */

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 JavaDoc(
386                 "Option type must be one of the following:" // NOI18N
387
+" DEFAULT_OPTION, YES_NO_OPTION," // NOI18N
388
+" YES_NO_CANCEL_OPTION or OK_CANCEL_OPTION." // NOI18N
389

390             );
391         }
392
393         int oldType = optionType;
394         optionType = newType;
395         firePropertyChange(PROP_OPTION_TYPE, new Integer JavaDoc(oldType), new Integer JavaDoc(optionType));
396     }
397
398     /**
399     * Get the type of options that are to be displayed.
400     *
401     * @return the option type
402     *
403     * @see #setOptionType
404     */

405     public int getOptionType() {
406         getterCalled();
407
408         return optionType;
409     }
410
411     /**
412     * Define an explicit description of the set of user-selectable options.
413     * The usual value for the options parameter is an array of
414     * <code>String</code>s. But the parameter type is an array of <code>Object</code>s. Its
415     * interpretation depends on its type:
416     * <dl compact>
417     * <dt>{@link Component}<dd>The component is added to the button row directly.
418     * <dt>{@link javax.swing.Icon}<dd>A {@link javax.swing.JButton} is created with this icon as its label.
419     * <dt>anything else<dd>The <code>Object</code> is {@link Object#toString converted} to a string and the result is used to
420     * label a <code>JButton</code>.
421     * </dl>
422     *
423     * @param newOptions an array of user-selectable options
424     *
425     * @see #getOptions
426     */

427     public void setOptions(Object JavaDoc[] newOptions) {
428         Object JavaDoc[] oldOptions = options;
429         options = newOptions;
430         firePropertyChange(PROP_OPTIONS, oldOptions, newOptions);
431     }
432
433     /**
434     * Get the explicit choices the user can make.
435     * @return the array of <code>Object</code>s that give the user's choices
436     *
437     * @see #setOptions
438     */

439     public Object JavaDoc[] getOptions() {
440         getterCalled();
441
442         if (options != null) {
443             return (Object JavaDoc[]) options.clone();
444         }
445
446         return options;
447     }
448
449     /**
450     * Define an explicit description of the set of additional user-selectable options.
451     * Additional options are supposed to be used for help button, etc.
452     * <P>
453     * The usual value for the options parameter is an array of
454     * <code>String</code>s. But the parameter type is an array of <code>Object</code>s. Its
455     * interpretation depends on its type:
456     * <dl compact>
457     * <dt>{@link Component}<dd>The component is added to the button row directly.
458     * <dt>{@link javax.swing.Icon}<dd>A {@link javax.swing.JButton} is created with this icon as its label.
459     * <dt>anything else<dd>The <code>Object</code> is {@link Object#toString converted} to a string and the result is used to
460     * label a <code>JButton</code>.
461     * </dl>
462     *
463     * @param newOptions an array of user-selectable options
464     *
465     * @see #getOptions
466     */

467     public void setAdditionalOptions(Object JavaDoc[] newOptions) {
468         Object JavaDoc[] oldOptions = adOptions;
469         adOptions = newOptions;
470         firePropertyChange(PROP_OPTIONS, oldOptions, newOptions);
471     }
472
473     /**
474     * Get the explicit additional choices the user can make.
475     * @return the array of <code>Object</code>s that give the user's choices
476     *
477     * @see #setOptions
478     */

479     public Object JavaDoc[] getAdditionalOptions() {
480         getterCalled();
481
482         if (adOptions != null) {
483             return (Object JavaDoc[]) adOptions.clone();
484         }
485
486         return null;
487     }
488
489     /**
490      * Sets the value w/o firing property change. The caller this is responsible
491      * to notify this change.
492      */

493     void setValueWithoutPCH(Object JavaDoc newValue) {
494         value = newValue;
495     }
496
497     /**
498     * Set the value the user has chosen and fires appropriate property change.
499     * You probably do not want to call this yourself, of course.
500     *
501     * @param newValue the chosen value
502     *
503     * @see #getValue
504     */

505     public void setValue(Object JavaDoc newValue) {
506         Object JavaDoc oldValue = value;
507         setValueWithoutPCH(newValue);
508         firePropertyChange(PROP_VALUE, oldValue, newValue);
509     }
510
511     /**
512     * Get the value the user has selected.
513     *
514     * @return an <code>Object</code> indicating the option selected by the user
515     *
516     * @see #setValue
517     */

518     public Object JavaDoc getValue() {
519         getterCalled();
520
521         return value;
522     }
523
524     /**
525     * Get the default value of descriptor.
526     *
527     * @return an <code>Object</code> that represents the default value
528     * @since 5.9
529     */

530     public Object JavaDoc getDefaultValue() {
531         return defaultValue;
532     }
533
534     /**
535     * Set the title string for this report description.
536     *
537     * @param newTitle the title of this description
538     *
539     * @see #getTitle
540     */

541     public void setTitle(String JavaDoc newTitle) {
542         Object JavaDoc oldTitle = title;
543         title = newTitle;
544         firePropertyChange(PROP_TITLE, oldTitle, newTitle);
545     }
546
547     /**
548     * Get the title string for this report description.
549     *
550     * @return the title of this description
551     *
552     * @see #setTitle
553     */

554     public String JavaDoc getTitle() {
555         getterCalled();
556
557         return title;
558     }
559
560     /**
561     * Define a detail message to be reported. In the most common usage,
562     * this message is just a <code>String</code>. However, the type of this
563     * parameter is actually <code>Object</code>. Its interpretation depends on its type:
564     * <dl compact>
565     * <dt><code>Object[]</code><dd> A recursively interpreted series of messages.
566     * <dt><code>Throwable</code><dd> A stack trace is displayed.
567     * <dt>anything else<dd> The {@link Object#toString string representation} of the object is used.
568     * </dl>
569     *
570     * @param newDetail the detail object of this description
571     *
572     * @see #getDetail
573     *
574     public void setDetail(Object newDetail) {
575       Object oldDetail = detail;
576       detail = newDetail;
577       firePropertyChange(PROP_DETAIL, oldDetail, newDetail);
578     }
579
580     /**
581     * Get the detail object for this description.
582     *
583     * @return details of this description
584     *
585     * @see #setTitle
586     *
587     public Object getDetail() {
588       return detail;
589       }
590       */

591
592     //
593
// Support for reporting bound property changes.
594
//
595

596     /**
597     * Add a {@link PropertyChangeListener} to the listener list.
598     *
599     * @param listener the <code>PropertyChangeListener</code> to be added
600     */

601     public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
602         getterCalled();
603         changeSupport.addPropertyChangeListener(listener);
604     }
605
606     /**
607     * Remove a {@link PropertyChangeListener} from the listener list.
608     *
609     * @param listener the <code>PropertyChangeListener</code> to be removed
610     */

611     public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
612         if (changeSupport != null) {
613             changeSupport.removePropertyChangeListener(listener);
614         }
615     }
616
617     /**
618     * Fire a {@link PropertyChangeEvent} to each listener.
619     *
620     * @param propertyName the programmatic name of the property that was changed
621     * @param oldValue the old value of the property
622     * @param newValue the new value of the property
623     */

624     protected void firePropertyChange(String JavaDoc propertyName, Object JavaDoc oldValue, Object JavaDoc newValue) {
625         if (changeSupport != null) {
626             changeSupport.firePropertyChange(propertyName, oldValue, newValue);
627         }
628     }
629
630     /**
631     * Get the title to use for the indicated type.
632     * @param messageType the type of message
633     * @return the title to use
634     */

635     protected static String JavaDoc 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 ""; // NOI18N
654
}
655
656     /**
657     * Provides information about the results of a command. Offers
658     * no user choices; the user can only acknowledge the message.
659     */

660     public static class Message extends NotifyDescriptor {
661         /**
662         * Create an informational report about the results of a command.
663         *
664         * @param message the message object
665         * @see NotifyDescriptor#NotifyDescriptor
666         */

667         public Message(Object JavaDoc message) {
668             this(message, INFORMATION_MESSAGE);
669         }
670
671         /**
672         * Create a report about the results of a command.
673         *
674         * @param message the message object
675         * @param messageType the type of message to be displayed
676         * @see NotifyDescriptor#NotifyDescriptor
677         */

678         public Message(Object JavaDoc message, int messageType) {
679             super(
680                 message, NotifyDescriptor.getTitleForType(messageType), DEFAULT_OPTION, messageType,
681                 new Object JavaDoc[] { OK_OPTION }, OK_OPTION
682             );
683         }
684     }
685
686     /**
687     * Provides a description of a possible action and requests confirmation from the user before proceeding.
688     * This should be used to alert the user to a condition
689     * or situation that requires the user's decision before proceeding, such
690     * as an impending action with potentially destructive or irreversible
691     * consequences. It is conventionally in the form of a question: for example,
692     * "Save changes to TestForm?"
693     */

694     public static class Confirmation extends NotifyDescriptor {
695         /**
696         * Create a yes/no/cancel question with default title.
697         *
698         * @param message the message object
699         * @see NotifyDescriptor#NotifyDescriptor
700         */

701         public Confirmation(Object JavaDoc message) {
702             this(message, YES_NO_CANCEL_OPTION);
703         }
704
705         /**
706         * Create a yes/no/cancel question.
707         *
708         * @param message the message object
709         * @param title the dialog title
710         * @see NotifyDescriptor#NotifyDescriptor
711         */

712         public Confirmation(Object JavaDoc message, String JavaDoc title) {
713             this(message, title, YES_NO_CANCEL_OPTION);
714         }
715
716         /**
717         * Create a question with default title.
718         *
719         * @param message the message object
720         * @param optionType the type of options to display to the user
721         * @see NotifyDescriptor#NotifyDescriptor
722         */

723         public Confirmation(Object JavaDoc message, int optionType) {
724             this(message, optionType, QUESTION_MESSAGE);
725         }
726
727         /**
728         * Create a question.
729         *
730         * @param message the message object
731         * @param title the dialog title
732         * @param optionType the type of options to display to the user
733         * @see NotifyDescriptor#NotifyDescriptor
734         */

735         public Confirmation(Object JavaDoc message, String JavaDoc title, int optionType) {
736             this(message, title, optionType, QUESTION_MESSAGE);
737         }
738
739         /**
740         * Create a confirmation with default title.
741         *
742         * @param message the message object
743         * @param optionType the type of options to display to the user
744         * @param messageType the type of message to use
745         * @see NotifyDescriptor#NotifyDescriptor
746         */

747         public Confirmation(Object JavaDoc message, int optionType, int messageType) {
748             super(
749                 message, NotifyDescriptor.getTitleForType(messageType), optionType, messageType,
750                 (optionType == DEFAULT_OPTION) ? new Object JavaDoc[] { OK_OPTION } : null, OK_OPTION
751             );
752         }
753
754         /**
755         * Create a confirmation.
756         *
757         * @param message the message object
758         * @param title the dialog title
759         * @param optionType the type of options to display to the user
760         * @param messageType the type of message to use
761         * @see NotifyDescriptor#NotifyDescriptor
762         */

763         public Confirmation(Object JavaDoc message, String JavaDoc title, int optionType, int messageType) {
764             super(
765                 message, title, optionType, messageType,
766                 (optionType == DEFAULT_OPTION) ? new Object JavaDoc[] { OK_OPTION } : null, OK_OPTION
767             );
768         }
769     }
770
771     /**
772     * Provides a description of an exception that occurred during
773     * execution of the IDE. The best is to use this class together with
774     * {@link DialogDisplayer#notifyLater} as that allows an exception
775     * to be notified from any thread.
776     */

777     public static final class Exception extends Confirmation {
778         static final long serialVersionUID = -3387516993124229948L;
779
780         /**
781         * Create an exception report with default message.
782         *
783         * @param detail the detail object
784         */

785         public Exception(Throwable JavaDoc detail) {
786             this(detail, detail.getMessage());
787
788             // handle InvocationTargetExceptions
789
if (detail instanceof InvocationTargetException JavaDoc) {
790                 Throwable JavaDoc target = ((InvocationTargetException JavaDoc) detail).getTargetException();
791                 this.setMessage(target);
792
793                 Object JavaDoc msgObj = this.getMessage();
794                 if ((msgObj == null) || "".equals(msgObj)) { // NOI18N
795

796                     String JavaDoc 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 JavaDoc obj = this.getMessage();
805             // emphasize user-non-friendly exceptions
806
if ((obj == null) || "".equals(obj)) { // NOI18N
807
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                 ); // NOI18N
813
this.setTitle(NbBundle.getMessage(NotifyDescriptor.class, "NTF_ExceptionalExceptionTitle"));
814             }
815         }
816
817         /**
818         * Create an exception report.
819         *
820         * @param detail the detail object
821         * @param message the message object
822         */

823         public Exception(Throwable JavaDoc detail, Object JavaDoc message) {
824             super(message, DEFAULT_OPTION, ERROR_MESSAGE);
825
826             // customize descriptor
827
// this.setDetail(detail);
828
this.setTitle(NbBundle.getMessage(NotifyDescriptor.class, "NTF_ExceptionTitle"));
829         }
830     }
831
832     /** Notification providing for a line of text input.
833     * @author Dafe Simonek
834     */

835     public static class InputLine extends NotifyDescriptor {
836         /**
837         * The text field used to enter the input.
838         */

839         protected JTextField JavaDoc textField;
840
841         /** Construct dialog with the specified title and label text.
842         * @param text label text
843         * @param title title of the dialog
844         */

845         public InputLine(final String JavaDoc text, final String JavaDoc title) {
846             this(text, title, OK_CANCEL_OPTION, PLAIN_MESSAGE);
847         }
848
849         /** Construct dialog with the specified title, label text, option and
850         * message types.
851         * @param text label text
852         * @param title title of the dialog
853         * @param optionType option type (ok, cancel, ...)
854         * @param messageType message type (question, ...)
855         */

856         public InputLine(final String JavaDoc text, final String JavaDoc title, final int optionType, final int messageType) {
857             super(null, title, optionType, messageType, null, null);
858             super.setMessage(createDesign(text));
859         }
860
861         /**
862         * Get the text which the user typed into the input line.
863         * @return the text entered by the user
864         */

865         public String JavaDoc getInputText() {
866             return textField.getText();
867         }
868
869         /**
870         * Set the text on the input line.
871         * @param text the new text
872         */

873         public void setInputText(final String JavaDoc text) {
874             textField.setText(text);
875             textField.selectAll();
876         }
877
878         /** Make a component representing the input line.
879         * @param text a label for the input line
880         * @return the component
881         */

882         protected Component JavaDoc createDesign(final String JavaDoc text) {
883             int index;
884             JPanel JavaDoc panel = new JPanel JavaDoc();
885
886             JLabel JavaDoc textLabel = new JLabel JavaDoc();
887             Mnemonics.setLocalizedText(textLabel, text);
888
889             textLabel.setBorder(new EmptyBorder JavaDoc(0, 0, 0, 10));
890             panel.setLayout(new BorderLayout JavaDoc());
891             panel.setBorder(new EmptyBorder JavaDoc(11, 12, 1, 11));
892             textField = new JTextField JavaDoc(25);
893             panel.add(BorderLayout.WEST, textLabel);
894             panel.add(BorderLayout.CENTER, textField);
895             textLabel.setLabelFor(textField);
896             textField.setBorder(new CompoundBorder JavaDoc(textField.getBorder(), new EmptyBorder JavaDoc(2, 0, 2, 0)));
897             textField.requestFocus();
898
899             javax.swing.KeyStroke JavaDoc enter = javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ENTER, 0);
900             javax.swing.text.Keymap JavaDoc map = textField.getKeymap();
901
902             map.removeKeyStrokeBinding(enter);
903
904             /*
905
906                   textField.addActionListener (new java.awt.event.ActionListener () {
907                       public void actionPerformed (java.awt.event.ActionEvent evt) {
908             System.out.println("action: " + evt);
909                         InputLine.this.setValue (OK_OPTION);
910                       }
911                     }
912                   );
913             */

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      // end of InputLine
925
}
926
Popular Tags