Back to index

wims  3.65+svn20090927
ComponentDialog.java
Go to the documentation of this file.
00001 /* A top-level dialog window containing an arbitrary component.
00002 
00003  Copyright (c) 1998-2005 The Regents of the University of California.
00004  All rights reserved.
00005  Permission is hereby granted, without written agreement and without
00006  license or royalty fees, to use, copy, modify, and distribute this
00007  software and its documentation for any purpose, provided that the above
00008  copyright notice and the following two paragraphs appear in all copies
00009  of this software.
00010 
00011  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00012  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00013  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00014  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00015  SUCH DAMAGE.
00016 
00017  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00018  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00020  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00021  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00022  ENHANCEMENTS, OR MODIFICATIONS.
00023 
00024  PT_COPYRIGHT_VERSION_2
00025  COPYRIGHTENDKEY
00026  */
00027 package ptolemy.gui;
00028 
00029 import java.awt.Component;
00030 import java.awt.Dimension;
00031 import java.awt.Font;
00032 import java.awt.Frame;
00033 import java.awt.Toolkit;
00034 import java.awt.event.WindowAdapter;
00035 import java.awt.event.WindowEvent;
00036 import java.beans.PropertyChangeEvent;
00037 import java.beans.PropertyChangeListener;
00038 
00039 import javax.swing.Box;
00040 import javax.swing.BoxLayout;
00041 import javax.swing.JDialog;
00042 import javax.swing.JOptionPane;
00043 import javax.swing.JPanel;
00044 import javax.swing.JTextArea;
00045 
00048 
00073 public class ComponentDialog extends JDialog {
00082     public ComponentDialog(Frame owner, String title, Component component) {
00083         this(owner, title, component, null, null);
00084     }
00085 
00099     public ComponentDialog(Frame owner, String title, Component component,
00100             String[] buttons) {
00101         this(owner, title, component, buttons, null);
00102     }
00103 
00120     public ComponentDialog(Frame owner, String title, Component component,
00121             String[] buttons, String message) {
00122         super(owner, title, true);
00123 
00124         // Create a panel that contains the optional message
00125         // and the specified component, separated by a spacing rigid area.
00126         JPanel panel = new JPanel();
00127 
00128         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
00129 
00130         if (message != null) {
00131             _messageArea = new JTextArea(message);
00132             _messageArea.setFont(new Font("SansSerif", Font.PLAIN, 12));
00133             _messageArea.setEditable(false);
00134             _messageArea.setLineWrap(true);
00135             _messageArea.setWrapStyleWord(true);
00136             _messageArea.setBackground(getContentPane().getBackground());
00137 
00138             // Left Justify.
00139             _messageArea.setAlignmentX(0.0f);
00140             panel.add(_messageArea);
00141             panel.add(Box.createRigidArea(new Dimension(0, 10)));
00142         }
00143 
00144         panel.add(component);
00145         contents = component;
00146 
00147         if (buttons != null) {
00148             _buttons = buttons;
00149         } else {
00150             _buttons = _defaultButtons;
00151         }
00152 
00153         _optionPane = new JOptionPane(panel, JOptionPane.QUESTION_MESSAGE,
00154                 JOptionPane.YES_NO_OPTION, null, _buttons, _buttons[0]);
00155 
00156         // The following code is based on Sun's CustomDialog example...
00157         _optionPane.addPropertyChangeListener(new PropertyChangeListener() {
00158             public void propertyChange(PropertyChangeEvent e) {
00159                 String prop = e.getPropertyName();
00160 
00161                 // PropertyChange is an extremely non-selective listener,
00162                 // so we have to filter...
00163                 if (isVisible()
00164                         && (e.getSource() == _optionPane)
00165                         && (prop.equals(JOptionPane.VALUE_PROPERTY) || prop
00166                                 .equals(JOptionPane.INPUT_VALUE_PROPERTY))) {
00167                     Object value = _optionPane.getValue();
00168 
00169                     // Ignore reset.
00170                     if (value == JOptionPane.UNINITIALIZED_VALUE) {
00171                         return;
00172                     }
00173 
00174                     // Reset the JOptionPane's value.
00175                     // If you don't do this, then if the user
00176                     // presses the same button next time, no
00177                     // property change event will be fired.
00178                     // Note that this seems to trigger the listener
00179                     // again, so the previous line is essential.
00180                     _optionPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
00181 
00182                     if (value instanceof String) {
00183                         // A button was pressed...
00184                         _buttonPressed = (String) value;
00185                     }
00186 
00187                     // Close the window.
00188                     setVisible(false);
00189 
00190                     // Take any action that might be associated with
00191                     // window closing.
00192                     _handleClosing();
00193 
00194                     // Java's AWT yields random results if we do this.
00195                     // And anyway, it doesn't work.  Components still don't
00196                     // have their ComponentListener methods called to indicate
00197                     // that they have become invisible.
00198                     // dispose();
00199                 }
00200             }
00201         });
00202 
00203         getContentPane().add(_optionPane);
00204         pack();
00205         setResizable(false);
00206 
00207         if (owner != null) {
00208             setLocationRelativeTo(owner);
00209         } else {
00210             // Center on screen.  According to the Java docs,
00211             // passing null to setLocationRelationTo() _may_ result
00212             // in centering on the screen, but it is not required to.
00213             Toolkit tk = Toolkit.getDefaultToolkit();
00214             setLocation((tk.getScreenSize().width - getSize().width) / 2, (tk
00215                     .getScreenSize().height - getSize().height) / 2);
00216         }
00217 
00218         // NOTE: Java's AWT may yield random results if we do the following.
00219         // And anyway, it doesn't work.  Components still don't
00220         // have their ComponentListener methods called to indicate
00221         // that they have become invisible.
00222         setDefaultCloseOperation(DISPOSE_ON_CLOSE);
00223 
00224         // Catch closing events so that components are notified if
00225         // the window manager is used to close the window.
00226         addWindowListener(new WindowAdapter() {
00227             public void windowClosing(WindowEvent e) {
00228                 _handleClosing();
00229             }
00230         });
00231 
00232         // Make the window visible.
00233         setVisible(true);
00234     }
00235 
00238 
00243     public String buttonPressed() {
00244         return _buttonPressed;
00245     }
00246 
00252     public void setMessage(String message) {
00253         if (_messageArea != null) {
00254             _messageArea.setText(message);
00255         }
00256     }
00257 
00260 
00266     protected void _handleClosing() {
00267         if ((contents instanceof CloseListener) && !_doneHandleClosing) {
00268             _doneHandleClosing = true;
00269             ((CloseListener) contents).windowClosed(this, _buttonPressed);
00270         }
00271     }
00272 
00275 
00278     public Component contents;
00279 
00282 
00284     protected String _buttonPressed = "";
00285 
00288 
00290     private static String[] _buttons;
00291 
00293     private static String[] _defaultButtons = { "OK", "Cancel" };
00294 
00296     private boolean _doneHandleClosing = false;
00297 
00299     private JOptionPane _optionPane;
00300 
00302     private JTextArea _messageArea;
00303 }