ChoiceDialog.java

00001 package guiUtilities;
00002 
00003 import javax.swing.JOptionPane;
00004 import javax.swing.JDialog;
00005 import javax.swing.JTextField;
00006 import java.beans.*; //property change stuff
00007 import java.awt.*;
00008 import java.awt.event.*;
00009 import javax.swing.*;
00010 
00011 
00012 public class ChoiceDialog extends JDialog
00013                    implements PropertyChangeListener {
00014 
00015     private JList objectJList;
00016     private Object[] objectList;
00017 
00018     private JOptionPane optionPane;
00019 
00020     private String btnString1 = "Enter";
00021     private String btnString2 = "Cancel";
00022     Object[] selectedObjects=null;
00023 
00028     public Object[] getSelectedObjects() {
00029         return selectedObjects;
00030     }
00031 
00033     public ChoiceDialog(Frame aFrame, String title, int width, int height, Object[] objectList, boolean canMultipleSelect) {
00034         super(aFrame, true);
00035         
00036         this.objectList=objectList;
00037 
00038         setTitle(title);
00039 
00040         objectJList = new JList(objectList);
00041  //       objectJList.setFont(displayScaleFont);
00042         if(canMultipleSelect) objectJList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
00043         else objectJList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
00044         JScrollPane optionsScrollPane = new JScrollPane(objectJList);
00045         optionsScrollPane.setMinimumSize(new Dimension(width, height));
00046 
00047         //Create an array of the text and components to be displayed.
00048         String msgString1 = title;
00049         Object[] array = {msgString1, optionsScrollPane};
00050 
00051         //Create an array specifying the number of dialog buttons
00052         //and their text.
00053         Object[] options = {btnString1, btnString2};
00054 
00055         //Create the JOptionPane.
00056         optionPane = new JOptionPane(array,
00057                                     JOptionPane.PLAIN_MESSAGE,
00058                                     JOptionPane.YES_NO_OPTION,
00059                                     null,
00060                                     options,
00061                                     options[0]);
00062 
00063         //Make this dialog display it.
00064         setContentPane(optionPane);
00065 
00066         //Handle window closing correctly.
00067         setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
00068         addWindowListener(new WindowAdapter() {
00069                 public void windowClosing(WindowEvent we) {
00070                 /*
00071                  * Instead of directly closing the window,
00072                  * we're going to change the JOptionPane's
00073                  * value property.
00074                  */
00075                     optionPane.setValue(new Integer(
00076                                         JOptionPane.CLOSED_OPTION));
00077             }
00078         });
00079 
00080         //Ensure the text field always gets the first focus.
00081   //      addComponentListener(new ComponentAdapter() {
00082   //          public void componentShown(ComponentEvent ce) {
00083   //              textField.requestFocusInWindow();
00084   //          }
00085   //      });
00086 
00087         //Register an event handler that puts the text into the option pane.
00088   //      textField.addActionListener(this);
00089 
00090         //Register an event handler that reacts to option pane state changes.
00091         optionPane.addPropertyChangeListener(this);
00092     }
00093 
00095     public void actionPerformed(ActionEvent e) {
00096         optionPane.setValue(btnString1);
00097     }
00098 
00100     public void propertyChange(PropertyChangeEvent e) {
00101         String prop = e.getPropertyName();
00102 
00103         if (isVisible()
00104          && (e.getSource() == optionPane)
00105          && (JOptionPane.VALUE_PROPERTY.equals(prop) ||
00106              JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
00107             Object value = optionPane.getValue();
00108 
00109             if (value == JOptionPane.UNINITIALIZED_VALUE) {
00110                 //ignore reset
00111                 return;
00112             }
00113 
00114             //Reset the JOptionPane's value.
00115             //If you don't do this, then if the user
00116             //presses the same button next time, no
00117             //property change event will be fired.
00118             optionPane.setValue(
00119                     JOptionPane.UNINITIALIZED_VALUE);
00120 
00121             if (btnString1.equals(value)) {
00122                 selectedObjects = objectJList.getSelectedValues();
00123                 clearAndHide();
00124                 }
00125             else if(btnString2.equals(value)) {
00126                clearAndHide();
00127                }
00128         }
00129     }
00130 
00132     public void clearAndHide() {
00133         setVisible(false);
00134     }
00135 }

Generated on Mon Feb 6 14:12:08 2006 for SCT DAQ/DCS Software - Java by  doxygen 1.4.6