00001 package Sct.gui; 00002 00003 import java.awt.event.ActionEvent; 00004 import java.awt.event.ActionListener; 00005 import java.awt.event.WindowAdapter; 00006 import java.awt.event.WindowEvent; 00007 00008 import javax.swing.JButton; 00009 import javax.swing.JDialog; 00010 import javax.swing.JLabel; 00011 import javax.swing.JPanel; 00012 import javax.swing.JTextField; 00013 import javax.swing.Box; 00014 00015 00019 public class SelectionDialog extends JDialog { 00020 JTextField textFields[]; 00021 String fields[]; 00022 String defaults[]; 00023 00024 boolean valid; 00025 00032 public SelectionDialog(java.awt.Frame owner, String title, String fs[], String ds[]) { 00033 super(owner, title, true); 00034 00035 fields = fs; 00036 defaults = ds; 00037 00038 valid = true; 00039 00040 buildGUI(); 00041 } 00042 00046 private void buildGUI() { 00047 getContentPane().removeAll(); 00048 00049 Box allBox = Box.createVerticalBox(); 00050 Box box = Box.createVerticalBox(); 00051 00052 textFields = new JTextField[fields.length]; 00053 00054 for(int f=0; f<fields.length; f++) { 00055 JPanel panel = new JPanel(); 00056 panel.add(new JLabel(fields[f] + ":")); 00057 textFields[f] = new JTextField(defaults[f], 5); 00058 panel.add(textFields[f]); 00059 box.add(panel); 00060 } 00061 00062 allBox.add(box); 00063 00064 JPanel buttonPanel = new JPanel(); 00065 00066 JButton cancel = new JButton("Cancel"); 00067 cancel.addActionListener(new ActionListener() { 00068 public void actionPerformed(ActionEvent e) { 00069 valid = false; 00070 dispose(); 00071 } 00072 }); 00073 buttonPanel.add(cancel); 00074 00075 JButton okButton = new JButton("OK"); 00076 okButton.addActionListener(new ActionListener() { 00077 public void actionPerformed(ActionEvent e) { 00078 dispose(); 00079 } 00080 }); 00081 buttonPanel.add(okButton); 00082 00083 allBox.add(buttonPanel); 00084 getContentPane().add(allBox); 00085 } 00086 00091 public int getIntFieldValue(int index) throws InvalidDialogException, CancelledDialogException { 00092 if(!valid) throw new CancelledDialogException(); 00093 try { 00094 return Integer.decode(textFields[index].getText()).intValue(); 00095 } catch(NumberFormatException n) { 00096 System.out.println("NumberFormatException: " + n); 00097 throw new InvalidDialogException(); 00098 } 00099 } 00100 00105 public String getStringFieldValue(int index) throws InvalidDialogException, CancelledDialogException { 00106 if(!valid) throw new CancelledDialogException(); 00107 return textFields[index].getText(); 00108 } 00109 00113 public static class InvalidDialogException extends Exception { 00114 } 00115 00119 public static class CancelledDialogException extends Exception { 00120 } 00121 }