Back to index

wims  3.65+svn20090927
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | Private Attributes | Static Private Attributes
ptolemy.gui.ComponentDialog Class Reference

This class is a modal dialog box that contains an arbitrary component. More...

Inheritance diagram for ptolemy.gui.ComponentDialog:
Inheritance graph
[legend]
Collaboration diagram for ptolemy.gui.ComponentDialog:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ComponentDialog (Frame owner, String title, Component component)
 Construct a dialog with the specified owner, title, and component.
 ComponentDialog (Frame owner, String title, Component component, String[] buttons)
 Construct a dialog with the specified owner, title, component, and buttons.
 ComponentDialog (Frame owner, String title, Component component, String[] buttons, String message)
 Construct a dialog with the specified owner, title, component, buttons, and message.
String buttonPressed ()
 Return the label of the button that triggered closing the dialog, or an empty string if none.
void setMessage (String message)
 Change the message that was specified in the constructor to read as specified.

Public Attributes

Component contents
 The component contained by this dialog.

Protected Member Functions

void _handleClosing ()
 If the contents of this dialog implements the CloseListener interface, then notify it that the window has closed, unless notification has already been done (it is guaranteed to be done only once).

Protected Attributes

String _buttonPressed = ""
 The label of the button pushed to dismiss the dialog.

Private Attributes

boolean _doneHandleClosing = false
 Indicator that we have notified of window closing.
JOptionPane _optionPane
 The pane with the buttons.
JTextArea _messageArea
 The container for messages.

Static Private Attributes

static String[] _buttons
 Button labels.
static String[] _defaultButtons = { "OK", "Cancel" }
 Default button labels.

Detailed Description

This class is a modal dialog box that contains an arbitrary component.

It can be used, for example, to put an instance of Query in a top-level dialog box. The general way to use this class is to create the component that you wish to have contained in the dialog. Then pass that component to the constructor of this class. The dialog is modal, so the statement that creates the dialog will not return until the user dismisses the dialog. The method buttonPressed() can then be called to find out whether the user clicked the OK button or the Cancel button (or any other button specified in the constructor). Then you can access the component to determine what values were set by the user.

If the component that is added implements the CloseListener interface, then that component is notified when this dialog closes.

See also:
CloseListener
Author:
Edward A. Lee
Version:
Id:
ComponentDialog.java,v 1.34 2005/07/08 19:59:07 cxh Exp
Since:
Ptolemy II 0.4 .ProposedRating Yellow (eal) .AcceptedRating Yellow (janneck)

Definition at line 73 of file ComponentDialog.java.


Constructor & Destructor Documentation

ptolemy.gui.ComponentDialog.ComponentDialog ( Frame  owner,
String  title,
Component  component 
) [inline]

Construct a dialog with the specified owner, title, and component.

An "OK" and a "Cancel" button are added to the dialog. The dialog is placed relative to the owner.

Parameters:
ownerThe object that, per the user, appears to be generating the dialog.
titleThe title of the dialog.
componentThe component to insert in the dialog.

Definition at line 82 of file ComponentDialog.java.

                                                                           {
        this(owner, title, component, null, null);
    }
ptolemy.gui.ComponentDialog.ComponentDialog ( Frame  owner,
String  title,
Component  component,
String[]  buttons 
) [inline]

Construct a dialog with the specified owner, title, component, and buttons.

The first button is the "default" in that it is the one activated by "Enter" or "Return" keys. If the last argument is null, then an "OK" and a "Cancel" button will be created. The dialog is placed relative to the owner.

Parameters:
ownerThe object that, per the user, appears to be generating the dialog.
titleThe title of the dialog.
componentThe component to insert in the dialog.
buttonsAn array of labels for buttons at the bottom of the dialog.

Definition at line 99 of file ComponentDialog.java.

                              {
        this(owner, title, component, buttons, null);
    }
ptolemy.gui.ComponentDialog.ComponentDialog ( Frame  owner,
String  title,
Component  component,
String[]  buttons,
String  message 
) [inline]

Construct a dialog with the specified owner, title, component, buttons, and message.

The message is placed above the component. The first button is the "default" in that it is the one activated by "Enter" or "Return" keys. If the buttons argument is null, then an "OK" and a "Cancel" button will be created. The dialog is placed relative to the owner.

Parameters:
ownerThe object that, per the user, appears to be generating the dialog.
titleThe title of the dialog.
componentThe component to insert in the dialog.
buttonsAn array of labels for buttons at the bottom of the dialog.
messageA message to place above the component, or null if no message is needed.

Definition at line 120 of file ComponentDialog.java.

                                              {
        super(owner, title, true);

        // Create a panel that contains the optional message
        // and the specified component, separated by a spacing rigid area.
        JPanel panel = new JPanel();

        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

        if (message != null) {
            _messageArea = new JTextArea(message);
            _messageArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
            _messageArea.setEditable(false);
            _messageArea.setLineWrap(true);
            _messageArea.setWrapStyleWord(true);
            _messageArea.setBackground(getContentPane().getBackground());

            // Left Justify.
            _messageArea.setAlignmentX(0.0f);
            panel.add(_messageArea);
            panel.add(Box.createRigidArea(new Dimension(0, 10)));
        }

        panel.add(component);
        contents = component;

        if (buttons != null) {
            _buttons = buttons;
        } else {
            _buttons = _defaultButtons;
        }

        _optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE,
                JOptionPane.YES_NO_OPTION, null, _buttons, _buttons[0]);

        // The following code is based on Sun's CustomDialog example...
        _optionPane.addPropertyChangeListener(new PropertyChangeListener() {
            public void propertyChange(PropertyChangeEvent e) {
                String prop = e.getPropertyName();

                // PropertyChange is an extremely non-selective listener,
                // so we have to filter...
                if (isVisible()
                        && (e.getSource() == _optionPane)
                        && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
                                .equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
                    Object value = _optionPane.getValue();

                    // Ignore reset.
                    if (value == JOptionPane.UNINITIALIZED_VALUE) {
                        return;
                    }

                    // Reset the JOptionPane's value.
                    // If you don't do this, then if the user
                    // presses the same button next time, no
                    // property change event will be fired.
                    // Note that this seems to trigger the listener
                    // again, so the previous line is essential.
                    _optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);

                    if (value instanceof String) {
                        // A button was pressed...
                        _buttonPressed = (String) value;
                    }

                    // Close the window.
                    setVisible(false);

                    // Take any action that might be associated with
                    // window closing.
                    _handleClosing();

                    // Java's AWT yields random results if we do this.
                    // And anyway, it doesn't work.  Components still don't
                    // have their ComponentListener methods called to indicate
                    // that they have become invisible.
                    // dispose();
                }
            }
        });

        getContentPane().add(_optionPane);
        pack();
        setResizable(false);

        if (owner != null) {
            setLocationRelativeTo(owner);
        } else {
            // Center on screen.  According to the Java docs,
            // passing null to setLocationRelationTo() _may_ result
            // in centering on the screen, but it is not required to.
            Toolkit tk = Toolkit.getDefaultToolkit();
            setLocation((tk.getScreenSize().width - getSize().width) / 2, (tk
                    .getScreenSize().height - getSize().height) / 2);
        }

        // NOTE: Java's AWT may yield random results if we do the following.
        // And anyway, it doesn't work.  Components still don't
        // have their ComponentListener methods called to indicate
        // that they have become invisible.
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        // Catch closing events so that components are notified if
        // the window manager is used to close the window.
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                _handleClosing();
            }
        });

        // Make the window visible.
        setVisible(true);
    }

Here is the call graph for this function:


Member Function Documentation

void ptolemy.gui.ComponentDialog._handleClosing ( ) [inline, protected]

If the contents of this dialog implements the CloseListener interface, then notify it that the window has closed, unless notification has already been done (it is guaranteed to be done only once).

Definition at line 266 of file ComponentDialog.java.

                                    {
        if ((contents instanceof CloseListener) && !_doneHandleClosing) {
            _doneHandleClosing = true;
            ((CloseListener) contents).windowClosed(this, _buttonPressed);
        }
    }

Here is the caller graph for this function:

Return the label of the button that triggered closing the dialog, or an empty string if none.

Returns:
The label of the button pressed.

Definition at line 243 of file ComponentDialog.java.

                                  {
        return _buttonPressed;
    }

Here is the caller graph for this function:

void ptolemy.gui.ComponentDialog.setMessage ( String  message) [inline]

Change the message that was specified in the constructor to read as specified.

If no message was specified in the constructor, then do nothing.

Parameters:
messageThe new message.

Definition at line 252 of file ComponentDialog.java.

                                           {
        if (_messageArea != null) {
            _messageArea.setText(message);
        }
    }

Member Data Documentation

The label of the button pushed to dismiss the dialog.

Definition at line 284 of file ComponentDialog.java.

String [] ptolemy.gui.ComponentDialog._buttons [static, private]

Button labels.

Definition at line 290 of file ComponentDialog.java.

String [] ptolemy.gui.ComponentDialog._defaultButtons = { "OK", "Cancel" } [static, private]

Default button labels.

Definition at line 293 of file ComponentDialog.java.

Indicator that we have notified of window closing.

Definition at line 296 of file ComponentDialog.java.

The container for messages.

Definition at line 302 of file ComponentDialog.java.

The pane with the buttons.

Definition at line 299 of file ComponentDialog.java.

The component contained by this dialog.

Definition at line 278 of file ComponentDialog.java.


The documentation for this class was generated from the following file: