SelectionDialog.java
Go to the documentation of this file.
00001 /*
00002  * (c) copyright 2008, Technische Universitaet Graz and Technische Universitaet Wien
00003  *
00004  * This file is part of jdiagengine.
00005  *
00006  * jdiagengine is free software: you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation, either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * jdiagengine is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  * You should have received a copy of the GNU General Public License
00016  * along with jdiagengine. If not, see <http://www.gnu.org/licenses/>.
00017  *
00018  * Authors: Joerg Weber, Franz Wotawa
00019  * Contact: jweber@ist.tugraz.at (preferred), or fwotawa@ist.tugraz.at
00020  *
00021  */
00022 
00023 
00024 /*
00025  * SelectionDialog: Provides a dialog with one selection list
00026  *
00027  * @version 0.1, DATE: 20.05.1999
00028  * @author Franz Wotawa
00029  *
00030  */
00031 
00032 package gui;
00033 
00034 import java.lang.*;    // Java language classes 
00035 import java.awt.*;     // User Interface components
00036 import java.io.*;      // IO specific classes (File, Stream,..)
00037 import java.awt.event.*;   // Event handling
00038 import java.util.*;
00039 
00040 public class SelectionDialog extends Dialog
00041 {
00042   // Instance variables ...
00043 
00044   protected java.awt.List selectionList;
00045   protected String result = null;
00046 
00047   // Instance creation
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     // Create buttons
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     // Initialize this dialog to its preferred size
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   // Action handling
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   // Accessing
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 


tug_ist_diagnosis_engine
Author(s): Safdar Zaman, Gerald Steinbauer
autogenerated on Mon Jan 6 2014 11:51:16