package contrib; import java.util.Arrays; import java.util.List; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; /** * @author Phill Perryman * * Allows the user to choose from a list of elements * */ public class Chooser { private Elementx[] choose; private String attributeName = "id"; /** * Construct a new Chooser based on a list of Elements */ public Chooser(List list) { choose = new Elementx[list.size()]; for (int i = 0; i < list.size(); i++) { choose[i]= (Elementx) list.get(i); } Arrays.sort(choose); } /** * Construct a new Chooser based on a list of child Elements * of the element */ public Chooser(Elementx e) { List list = e.getChildren(); choose = new Elementx[list.size()]; for (int i = 0; i < list.size(); i++) { choose[i]= (Elementx) list.get(i); } Arrays.sort(choose); } /** * The messagebox function displays a drop down list of the * elements as renderend by toString and allows the user * to choose one * * @param The frame to display in * @param Title of the dialog box * @param Messsage text * @return the selected Elementx or null if the dialog * is cancelled * */ public Elementx messagebox(JFrame frame, String titleId, String msgId) { JPanel panel = new JPanel(); JLabel label = new JLabel(msgId); panel.add(label); JComboBox desc = new JComboBox(choose); panel.add(desc); if (JOptionPane .showConfirmDialog( frame, panel, titleId, JOptionPane.OK_CANCEL_OPTION) != JOptionPane.OK_OPTION) { return null; }; return (Elementx) desc.getSelectedItem(); } }