|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide; import java.beans.*; import java.lang.reflect.InvocationTargetException; import javax.swing.JOptionPane; import java.awt.BorderLayout; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.border.*; import javax.swing.JPanel; import java.awt.Component; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.JTextArea; import javax.swing.UIManager; import org.openide.util.NbBundle; /** * This class provides a description of a user notification to be displayed. *
Object to report
* @see #getMessage
*/
public void setMessage(Object newMessage) {
Object oldMessage = message;
if (newMessage instanceof String) {
// bugfix #25457, use JTextArea for word-wrapping
JTextArea area = new JTextArea ((String)newMessage);
area.setPreferredSize (new Dimension(SIZE_PREFERRED_WIDTH, SIZE_PREFERRED_HEIGHT));
area.setBackground (UIManager.getColor ("Label.background")); // NOI18N
area.setBorder (BorderFactory.createEmptyBorder ());
area.setLineWrap (true);
area.setWrapStyleWord (true);
area.setEditable (false);
newMessage = area;
}
message = newMessage;
firePropertyChange(PROP_MESSAGE, oldMessage, newMessage);
}
/**
* Get the message object.
* @see #setMessage
*
* @return the Object that is to be reported
*/
public Object getMessage() {
getterCalled ();
return message;
}
/**
* Define the style of the message. The look and feel manager may lay out
* the dialog differently depending on this value, and will often provide
* a default icon. The possible values are:
*
String s. But the parameter type is an array of Object s. Its
* interpretation depends on its type:
*
Object s that give the user's choices
*
* @see #setOptions
*/
public Object[] getOptions() {
getterCalled ();
if (options != null) {
return (Object[])options.clone ();
}
return options;
}
/**
* Define an explicit description of the set of additional user-selectable options.
* Additional options are supposed to be used for help button, etc.
*
* The usual value for the options parameter is an array of
*
Object s that give the user's choices
*
* @see #setOptions
*/
public Object[] getAdditionalOptions() {
getterCalled ();
if (adOptions != null) {
return (Object[])adOptions.clone ();
}
return null;
}
/**
* Sets the value w/o firing property change. The caller this is responsible
* to notify this change.
*/
void setValueWithoutPCH (Object newValue) {
value = newValue;
}
/**
* Set the value the user has chosen and fires appropriate property change.
* You probably do not want to call this yourself, of course.
*
* @param newValue the chosen value
*
* @see #getValue
*/
public void setValue(Object newValue) {
Object oldValue = value;
setValueWithoutPCH (newValue);
firePropertyChange(PROP_VALUE, oldValue, newValue);
}
/**
* Get the value the user has selected.
*
* @return an Object indicating the option selected by the user
*
* @see #setValue
*/
public Object getValue() {
getterCalled ();
return value;
}
/**
* Set the title string for this report description.
*
* @param newTitle the title of this description
*
* @see #getTitle
*/
public void setTitle(String newTitle) {
Object oldTitle = title;
title = newTitle;
firePropertyChange(PROP_TITLE, oldTitle, newTitle);
}
/**
* Get the title string for this report description.
*
* @return the title of this description
*
* @see #setTitle
*/
public String getTitle() {
getterCalled ();
return title;
}
/**
* Define a detail message to be reported. In the most common usage,
* this message is just a String . However, the type of this
* parameter is actually Object . Its interpretation depends on its type:
*
PropertyChangeListener to be added
*/
public void addPropertyChangeListener(PropertyChangeListener listener) {
getterCalled ();
changeSupport.addPropertyChangeListener(listener);
}
/**
* Remove a {@link PropertyChangeListener} from the listener list.
*
* @param listener the PropertyChangeListener to be removed
*/
public void removePropertyChangeListener(PropertyChangeListener listener) {
if (changeSupport != null) {
changeSupport.removePropertyChangeListener(listener);
}
}
/**
* Fire a {@link PropertyChangeEvent} to each listener.
*
* @param propertyName the programmatic name of the property that was changed
* @param oldValue the old value of the property
* @param newValue the new value of the property
*/
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
if (changeSupport != null) {
changeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}
/**
* Get the title to use for the indicated type.
* @param messageType the type of message
* @return the title to use
*/
protected static String getTitleForType(int messageType) {
switch(messageType) {
case ERROR_MESSAGE:
return NbBundle.getMessage (NotifyDescriptor.class, "NTF_ErrorTitle");
case WARNING_MESSAGE:
return NbBundle.getMessage (NotifyDescriptor.class, "NTF_WarningTitle");
case QUESTION_MESSAGE:
return NbBundle.getMessage (NotifyDescriptor.class, "NTF_QuestionTitle");
case INFORMATION_MESSAGE:
return NbBundle.getMessage (NotifyDescriptor.class, "NTF_InformationTitle");
case PLAIN_MESSAGE:
return NbBundle.getMessage (NotifyDescriptor.class, "NTF_PlainTitle");
}
return ""; // NOI18N
}
/**
* Provides information about the results of a command. Offers
* no user choices; the user can only acknowledge the message.
*/
public static class Message extends NotifyDescriptor
{
/**
* Create an informational report about the results of a command.
*
* @param message the message object
* @see NotifyDescriptor#NotifyDescriptor
*/
public Message(Object message) {
this(message, INFORMATION_MESSAGE);
}
/**
* Create a report about the results of a command.
*
* @param message the message object
* @param messageType the type of message to be displayed
* @see NotifyDescriptor#NotifyDescriptor
*/
public Message(Object message, int messageType) {
super(
message,
NotifyDescriptor.getTitleForType(messageType),
DEFAULT_OPTION,
messageType,
new Object[] { OK_OPTION },
OK_OPTION
);
}
}
/**
* Provides a description of a possible action and requests confirmation from the user before proceeding.
* This should be used to alert the user to a condition
* or situation that requires the user's decision before proceeding, such
* as an impending action with potentially destructive or irreversible
* consequences. It is conventionally in the form of a question: for example,
* "Save changes to TestForm?"
*/
public static class Confirmation extends NotifyDescriptor
{
/**
* Create a yes/no/cancel question with default title.
*
* @param message the message object
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message) {
this(message, YES_NO_CANCEL_OPTION);
}
/**
* Create a yes/no/cancel question.
*
* @param message the message object
* @param title the dialog title
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message, String title) {
this(message, title, YES_NO_CANCEL_OPTION);
}
/**
* Create a question with default title.
*
* @param message the message object
* @param optionType the type of options to display to the user
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message, int optionType) {
this(message, optionType, QUESTION_MESSAGE);
}
/**
* Create a question.
*
* @param message the message object
* @param title the dialog title
* @param optionType the type of options to display to the user
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message, String title, int optionType) {
this(message, title, optionType, QUESTION_MESSAGE);
}
/**
* Create a confirmation with default title.
*
* @param message the message object
* @param optionType the type of options to display to the user
* @param messageType the type of message to use
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message, int optionType, int messageType) {
super(
message,
NotifyDescriptor.getTitleForType(messageType),
optionType,
messageType,
optionType == DEFAULT_OPTION ? new Object[] { OK_OPTION } : null,
OK_OPTION
);
}
/**
* Create a confirmation.
*
* @param message the message object
* @param title the dialog title
* @param optionType the type of options to display to the user
* @param messageType the type of message to use
* @see NotifyDescriptor#NotifyDescriptor
*/
public Confirmation(Object message, String title, int optionType, int messageType) {
super(
message, title, optionType, messageType,
optionType == DEFAULT_OPTION ? new Object[] { OK_OPTION } : null,
OK_OPTION
);
}
}
/**
* Provides a description of an exception that occurred during
* execution of the IDE.
* @deprecated Better to use {@link ErrorManager#notify}.
*/
public static final class Exception extends Confirmation
{
static final long serialVersionUID =-3387516993124229948L;
/**
* Create an exception report with default message.
*
* @param detail the detail object
*/
public Exception(Throwable detail) {
this(detail, detail.getMessage());
// handle InvocationTargetExceptions
if (detail instanceof InvocationTargetException) {
Throwable target = ((InvocationTargetException)detail).getTargetException();
this.setMessage (target);
if (this.getMessage() == null || "".equals(this.getMessage())) { // NOI18N
String msg = target.getMessage();
msg = org.openide.util.Utilities.wrapString (msg, MAXIMUM_TEXT_WIDTH, java.text.BreakIterator.getCharacterInstance(), false);
this.setMessage(msg);
}
}
// emphasize user-non-friendly exceptions
if (this.getMessage() == null || "".equals(this.getMessage())) { // NOI18N
this.setMessage(NbBundle.getMessage(NotifyDescriptor.class,
"NTF_ExceptionalException", detail.getClass().getName(), System.getProperty("netbeans.user") + java.io.File.separator + "system")); // NOI18N
this.setTitle(NbBundle.getMessage (NotifyDescriptor.class, "NTF_ExceptionalExceptionTitle"));
}
}
/**
* Create an exception report.
*
* @param detail the detail object
* @param message the message object
*/
public Exception(Throwable detail, Object message) {
super(message, DEFAULT_OPTION, ERROR_MESSAGE);
// customize descriptor
// this.setDetail(detail);
this.setTitle(NbBundle.getMessage (NotifyDescriptor.class, "NTF_ExceptionTitle"));
}
}
/** Notification providing for a line of text input.
* @author Dafe Simonek
*/
public static class InputLine extends NotifyDescriptor {
/**
* The text field used to enter the input.
*/
protected JTextField textField;
/** Construct dialog with the specified title and label text.
* @param text label text
* @param title title of the dialog
*/
public InputLine (final String text, final String title) {
this(text, title, OK_CANCEL_OPTION, PLAIN_MESSAGE);
}
/** Construct dialog with the specified title, label text, option and
* message types.
* @param text label text
* @param title title of the dialog
* @param optionType option type (ok, cancel, ...)
* @param messageType message type (question, ...)
*/
public InputLine (final String text, final String title, final int optionType,
final int messageType) {
super(null, title, optionType, messageType, null, null);
super.setMessage(createDesign(text));
}
/**
* Get the text which the user typed into the input line.
* @return the text entered by the user
*/
public String getInputText () {
return textField.getText ();
}
/**
* Set the text on the input line.
* @param text the new text
*/
public void setInputText (final String text) {
textField.setText(text);
textField.selectAll ();
}
/** Make a component representing the input line.
* @param text a label for the input line
* @return the component
*/
protected Component createDesign (final String text) {
int index;
char mnemonic = 0;
String txt;
JPanel panel = new JPanel();
if(text != null && (index = text.indexOf('&')) >= 0 && index + 1 < text.length()) {
mnemonic = text.charAt(index + 1);
txt = text.substring(0, index) + text.substring(index + 1);
} else {
txt = text;
}
JLabel textLabel = new JLabel(txt);
if(txt != null && txt.length() > 0) {
textLabel.setDisplayedMnemonic(mnemonic == 0 ? text.charAt(0) : mnemonic);
}
textLabel.setBorder(new EmptyBorder(0, 0, 0, 10));
panel.setLayout(new BorderLayout());
panel.setBorder(new EmptyBorder(11, 12, 1, 11));
textField = new JTextField (25);
panel.add (BorderLayout.WEST, textLabel);
panel.add (BorderLayout.EAST, textField);
textLabel.setLabelFor(textField);
textField.setBorder(new CompoundBorder(textField.getBorder(), new EmptyBorder(2, 0, 2, 0)));
textField.requestFocus();
javax.swing.KeyStroke enter = javax.swing.KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_ENTER, 0
);
javax.swing.text.Keymap map = textField.getKeymap ();
map.removeKeyStrokeBinding (enter);
/*
textField.addActionListener (new java.awt.event.ActionListener () {
public void actionPerformed (java.awt.event.ActionEvent evt) {
System.out.println("action: " + evt);
InputLine.this.setValue (OK_OPTION);
}
}
);
*/
panel.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputPanel"));
textField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(NotifyDescriptor.class, "ACSD_InputField"));
return panel;
}
} // end of InputLine
}
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.