1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package zeus.gui;
25
26 import java.util.*;
27 import java.awt.*;
28 import java.awt.event.*;
29 import javax.swing.*;
30 import javax.swing.border.*;
31
32 import zeus.util.*;
33
34
35 public class SingleSelectionDialog extends JDialog
36 implements ActionListener {
37
38 protected JButton cancelButton;
39 protected String OK = "OK";
40 protected String CANCEL = "Cancel";
41 protected JList list;
42 protected JButton okButton;
43 protected Object selection = null;
44
45 public SingleSelectionDialog(Frame parent, String title, Object[] data) {
46 this(parent,title);
47 list.setListData(data);
48 }
49
50 public SingleSelectionDialog(Frame parent, String title,
51 Object[] data, Object selectedData) {
52 this(parent,title);
53 list.setListData(data);
54 list.setSelectedValue(selectedData,true);
55 }
56
57 public SingleSelectionDialog(Frame parent, String title) {
58 super(parent,title,true);
59
60 JPanel pane = (JPanel)getContentPane();
61 pane.setLayout( new BorderLayout() );
62
63 JPanel p1 = new JPanel();
64 p1.setLayout(new GridLayout(1,2,10,10));
65 okButton = new JButton(OK);
66 cancelButton = new JButton(CANCEL);
67 p1.add(okButton);
68 p1.add(cancelButton);
69
70 GridBagLayout gb = new GridBagLayout();
71 GridBagConstraints gbc = new GridBagConstraints();
72 gbc.insets = new Insets(10,0,10,0);
73 gbc.anchor = GridBagConstraints.NORTHWEST;
74 gbc.fill = GridBagConstraints.HORIZONTAL;
75 gbc.gridwidth = GridBagConstraints.REMAINDER;
76 gbc.weightx = 1;
77
78 JPanel p0 = new JPanel();
79 JSeparator s1 = new JSeparator();
80 p0.setLayout(gb);
81 gb.setConstraints(s1,gbc);
82 p0.add(s1);
83
84 gbc.anchor = GridBagConstraints.CENTER;
85 gbc.fill = GridBagConstraints.NONE;
86 gbc.insets = new Insets(0,0,10,0);
87 gb.setConstraints(p1,gbc);
88 p0.add(p1);
89
90 list = new JList();
91 list.setSelectionModel(new DefaultListSelectionModel());
92 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
93 list.setPreferredSize(new Dimension(100,100));
94 JScrollPane scrollpane = new JScrollPane();
95 scrollpane.getViewport().setView(list);
96
97 JPanel p3 = new JPanel();
98 p3.setLayout(gb);
99 gbc.insets = new Insets(10,10,10,10);
100 gbc.gridwidth = GridBagConstraints.REMAINDER;
101 gbc.anchor = GridBagConstraints.NORTHWEST;
102 gbc.fill = GridBagConstraints.BOTH;
103 gbc.weightx = gbc.weighty = 1;
104
105 gb.setConstraints(scrollpane,gbc);
106 p3.add(scrollpane);
107
108 pane.add("South",p0);
109 pane.add("Center",p3);
110
111
112 okButton.addActionListener(this);
113 cancelButton.addActionListener(this);
114 this.addWindowListener(
115 new WindowAdapter() {
116 public void windowClosing(WindowEvent evt) { setVisible(false); }
117 }
118 );
119 this.pack();
120 }
121
122 public void actionPerformed(ActionEvent evt) {
123 Object source = evt.getSource();
124
125 if ( source == okButton ) {
126 selection = list.getSelectedValue();
127 if ( selection == null ) {
128 int result = JOptionPane.showConfirmDialog(this,
129 "No value selected\nContinue?", "Warning",
130 JOptionPane.YES_NO_OPTION);
131 switch(result) {
132 case JOptionPane.YES_OPTION:
133 break;
134
135 case JOptionPane.NO_OPTION:
136 return;
137 }
138 }
139 this.setVisible(false);
140 }
141 else if ( source == cancelButton ) {
142 this.setVisible(false);
143 }
144 }
145
146 public Object getSelection() {
147 selection = null;
148 this.setVisible(true);
149 return selection;
150 }
151
152 public void setListData(Object[] data) {
153 list.setListData(data);
154 }
155
156 public void setSelection(Object value) {
157 list.setSelectedValue(value,true);
158 }
159
160 public Object[] getListData() {
161 ListModel model = list.getModel();
162 Vector data = new Vector();
163 for(int i = 0; i < model.getSize(); i++ )
164 data.addElement(model.getElementAt(i));
165 return data.toArray();
166 }
167
168 public static void main(String arg[]) {
169 JFrame f = new JFrame("Test");
170 f.setSize(200,200);
171 f.show();
172 String[] agents = {"Dave","John","Henry","Alice","Albert"};
173 SingleSelectionDialog m = new SingleSelectionDialog(f,"Select Servers");
174 m.setListData(agents);
175 Object data = m.getSelection();
176
177 }
178 }