1    package com.instantbank.common.uiutils;
2    
3    import javax.swing.JDialog;
4    import javax.swing.JPanel;
5    import javax.swing.JList;
6    import javax.swing.JScrollPane;
7    import javax.swing.JButton;
8    import javax.swing.ListSelectionModel;
9    import java.awt.Container;
10   import java.awt.BorderLayout;
11   import java.awt.event.ActionListener;
12   import java.awt.event.ActionEvent;
13   import javax.swing.border.EmptyBorder;
14   import javax.swing.border.TitledBorder;
15   import javax.swing.border.EtchedBorder;
16   
17   /**
18    * Modal JDialog that displays and allows to select one line in a list
19    * of strings.
20    *
21    * @author InstantBank (Rodrigo Lopez)
22    */
23   public class GetLineFromListDialog extends JDialog {
24     /**
25      * Central panel, contains the list.
26      */
27     private JPanel pnlCtrl;
28     /**
29      * Panel wher buttons are placed
30      */
31     private JPanel pnlInf;
32     /**
33      * The list of lines.
34      */
35     private JList lstLines;
36     /**
37      * Gives scrolling to the list.
38      */
39     private JScrollPane scrNames;
40     /**
41      * The OK button.
42      */
43     private JButton btnOK = new JButton("OK");
44     /**
45      * The Cancel button.
46      */
47     private JButton btnCancel = new JButton("Cancel");
48   
49     /**
50      * The answer is sent back in this array.
51      */
52     private Object[] answer;
53     /**
54      * The OK constant.
55      */
56     public static final int OK = 1;
57     /**
58      * The CANCEL constant
59      */
60     public static final int CANCEL = 2;
61   
62   
63     /**
64      * GetLineFromListDialog constructor.
65      *
66      * @param title Title for the dialog.
67      * @param msg Title for the list inside the dialog.
68      * @param lines Lines displayed inside the list.
69      * @param answer Array of 2 objects provided by the caller, filled with
70      * the following rules:
71      * <ul>
72      *   <li> If the CANCEL button is pressed, <tt>Integer(CANCEL)</tt>
73      *        is placed in <tt>answer[0]</tt>.
74      *   <li> If the OK button is pressed, <tt>Integer(OK)</tt>
75      *        is placed in <tt>answer[0]</tt>. Moreover,
76      *   <ul>
77      *       <li> Null is placed in <tt>answer[1]</tt> if there is no
78      *            line selected in the list.
79      *       <li> The selected line (String) is placed in <tt>answer[1]</tt>.
80      *   </ul>
81      * </ul>
82      */
83     public GetLineFromListDialog(String title, String msg, String[] lines,
84                                  Object[] answer) {
85   
86       this.setModal(true);
87       this.answer = answer;
88       this.setTitle(title);
89       this.setSize(300, 250);
90       Container ctp = this.getContentPane();
91       ctp.setLayout(new BorderLayout());
92   
93       pnlCtrl = new JPanel();
94       pnlCtrl.setBorder(new EmptyBorder(5, 5, 5, 5));
95       pnlCtrl.setLayout(new BorderLayout());
96   
97       lstLines = new JList();
98       lstLines.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
99       lstLines.setListData(lines);
100      scrNames = new JScrollPane(lstLines);
101      scrNames.setBorder(new TitledBorder(new EtchedBorder(), msg));
102  
103      pnlCtrl.add(scrNames, BorderLayout.CENTER);
104      ctp.add(pnlCtrl, BorderLayout.CENTER);
105  
106      pnlInf = new JPanel();
107      pnlInf.add(btnOK);
108      pnlInf.add(btnCancel);
109      ctp.add(pnlInf, BorderLayout.SOUTH);
110  
111      btnOK.addActionListener(
112        new ActionListener() {
113          public void actionPerformed(ActionEvent e) {
114            if(lstLines.isSelectionEmpty()) {
115              //No line was selected, so...
116              GetLineFromListDialog.this.answer[1] = null;
117            }
118            else {
119              GetLineFromListDialog.this.answer[1] =
120                lstLines.getSelectedValue().toString();
121            }
122            GetLineFromListDialog.this.answer[0] = new Integer(OK);
123            GetLineFromListDialog.this.dispose();
124          }
125        });
126  
127      btnCancel.addActionListener(
128        new ActionListener() {
129          public void actionPerformed(ActionEvent e) {
130            GetLineFromListDialog.this.answer[0] = new Integer(CANCEL);
131            GetLineFromListDialog.this.dispose();
132          }
133        });
134  
135    }
136  }
137