00001 package guiUtilities;
00002
00003 import javax.swing.JOptionPane;
00004 import javax.swing.JDialog;
00005 import javax.swing.JTextField;
00006 import java.beans.*;
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
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
00048 String msgString1 = title;
00049 Object[] array = {msgString1, optionsScrollPane};
00050
00051
00052
00053 Object[] options = {btnString1, btnString2};
00054
00055
00056 optionPane = new JOptionPane(array,
00057 JOptionPane.PLAIN_MESSAGE,
00058 JOptionPane.YES_NO_OPTION,
00059 null,
00060 options,
00061 options[0]);
00062
00063
00064 setContentPane(optionPane);
00065
00066
00067 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
00068 addWindowListener(new WindowAdapter() {
00069 public void windowClosing(WindowEvent we) {
00070
00071
00072
00073
00074
00075 optionPane.setValue(new Integer(
00076 JOptionPane.CLOSED_OPTION));
00077 }
00078 });
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
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
00111 return;
00112 }
00113
00114
00115
00116
00117
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 }