Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 package gui;
00033 
00034 import java.lang.*;    
00035 import java.awt.*;     
00036 import java.io.*;      
00037 import java.awt.event.*;   
00038 import java.util.*;
00039 
00040 public class SelectionDialog extends Dialog
00041 {
00042   
00043 
00044   protected java.awt.List selectionList;
00045   protected String result = null;
00046 
00047   
00048 
00049 
00050   public SelectionDialog(Frame dw, String label, Iterator e)
00051   {
00052     this(dw,label);
00053     setInitialValue(e);
00054   }
00055    
00056   public SelectionDialog(Frame dw, String label)
00057   {
00058     super(dw, "Dialog", true);
00059 
00060     Panel p1 = new Panel();
00061     p1.add(new Label(label));
00062 
00063     Panel p2 = new Panel();
00064     selectionList = new java.awt.List(5,false);
00065     selectionList.addItemListener(new ItemListener()
00066                     { public void itemStateChanged(ItemEvent e) {
00067                       objectSelected(); }});
00068     p2.add(selectionList);
00069 
00070     
00071 
00072     Panel p3 = new Panel();
00073     Button okButton = new Button("Ok");
00074     okButton.addActionListener(new ActionListener()
00075                                { public void actionPerformed(ActionEvent e)
00076                                    { okPressed();}});
00077     Button cancelButton = new Button("Cancel");
00078     cancelButton.addActionListener(new ActionListener()
00079                                    { public void 
00080                                        actionPerformed(ActionEvent e)
00081                                        { cancelPressed();}});
00082     p3.add(okButton);
00083     p3.add(cancelButton);
00084 
00085     add("North",p1);
00086     add("Center",p2);
00087     add("South",p3);
00088 
00089     
00090     pack();
00091     setLocation(defaultLocation());
00092   }
00093 
00094   public Point defaultLocation()
00095   {
00096     Point newLoc;
00097     Toolkit tk = Toolkit.getDefaultToolkit();
00098     Dimension screenSize = tk.getScreenSize();
00099     int w = this.getBounds().width;
00100     int h = this.getBounds().height;
00101     if ((w < screenSize.width) && (h < screenSize.height)) {
00102       newLoc = new Point((screenSize.width-w)/2,
00103                   (screenSize.height-h)/2);
00104     } else {
00105       newLoc = new Point(0,0);
00106     }
00107     return newLoc;
00108   }
00109 
00110   
00111 
00112   public void objectSelected() {
00113     result = selectionList.getSelectedItem();
00114   }
00115 
00116   public void okPressed()
00117   {
00118     closeDialog();
00119   }
00120 
00121   public void cancelPressed()
00122   {
00123     result = null;
00124     closeDialog();
00125   }
00126 
00127   public void closeDialog()
00128   {
00129     setVisible(false);
00130     dispose();
00131   }
00132 
00133   
00134 
00135   public String result()
00136   {
00137     return result;
00138   }
00139 
00140   public void select(String str) {
00141     String [] items = selectionList.getItems();
00142     for (int i=0; i<items.length; i++) {
00143       if (items[i].equals(str)) {
00144         selectionList.select(i);
00145         return;
00146       }
00147     }
00148   }
00149 
00150   public void setInitialValue(Iterator e)
00151   {
00152     if (selectionList.getItemCount() > 0) {
00153       selectionList.removeAll();
00154     }
00155     while (e.hasNext()) {
00156       selectionList.add((e.next()).toString());
00157     }    
00158   }
00159 
00160 }
00161