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 import zeus.gui.fields.*;
34
35
36 public class EditableMultipleSelectionDialog extends JDialog
37 implements ActionListener {
38
39 protected JButton cancelButton, selectButton, clearButton, invertButton;
40 protected String ENTER = "Enter new item:";
41 protected String SELECT_ALL = "Select All";
42 protected String CLEAR_ALL = "Clear All";
43 protected String INVERT = "Invert Selection";
44 protected String OK = "OK";
45 protected String CANCEL = "Cancel";
46 protected NameField textfield;
47 protected JList list;
48 protected JButton okButton;
49 protected Object[] selection = null;
50
51 public EditableMultipleSelectionDialog(
52 Frame parent, String title, Object[] data) {
53 this(parent,title);
54 list.setListData(data);
55 }
56
57 public EditableMultipleSelectionDialog(
58 Frame parent, String title, Object[] data, Object[] selectedData) {
59 this(parent,title);
60 list.setListData(data);
61 setSelection(selectedData);
62 }
63
64 public EditableMultipleSelectionDialog(Frame parent, String title) {
65 super(parent,title,true);
66
67 JPanel pane = (JPanel)getContentPane();
68 pane.setLayout( new BorderLayout() );
69
70
71 JPanel p1 = new JPanel();
72 p1.setLayout(new GridLayout(1,2,10,10));
73 okButton = new JButton(OK);
74 cancelButton = new JButton(CANCEL);
75 p1.add(okButton);
76 p1.add(cancelButton);
77
78 GridBagLayout gb = new GridBagLayout();
79 GridBagConstraints gbc = new GridBagConstraints();
80 gbc.insets = new Insets(10,0,10,0);
81 gbc.anchor = GridBagConstraints.NORTHWEST;
82 gbc.fill = GridBagConstraints.HORIZONTAL;
83 gbc.gridwidth = GridBagConstraints.REMAINDER;
84 gbc.weightx = 1;
85
86 JPanel p0 = new JPanel();
87 JSeparator s1 = new JSeparator();
88 p0.setLayout(gb);
89 gb.setConstraints(s1,gbc);
90 p0.add(s1);
91
92 gbc.anchor = GridBagConstraints.CENTER;
93 gbc.fill = GridBagConstraints.NONE;
94 gbc.insets = new Insets(0,0,10,0);
95 gb.setConstraints(p1,gbc);
96 p0.add(p1);
97
98 list = new JList();
99 list.setSelectionModel(new DefaultListSelectionModel());
100 list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
101 JScrollPane scrollpane = new JScrollPane();
102 scrollpane.getViewport().setView(list);
103 scrollpane.setPreferredSize(new Dimension(150,150));
104 selectButton = new JButton(SELECT_ALL);
105 clearButton = new JButton(CLEAR_ALL);
106 invertButton = new JButton(INVERT);
107
108 JPanel p2 = new JPanel();
109 p2.setLayout(new GridLayout(3,1,2,2));
110 p2.add(selectButton);
111 p2.add(clearButton);
112 p2.add(invertButton);
113
114 JPanel p4 = new JPanel();
115 p4.setLayout(gb);
116
117 gbc.insets = new Insets(0,0,0,0);
118 gbc.gridwidth = 1;
119 gbc.anchor = GridBagConstraints.WEST;
120 gbc.fill = GridBagConstraints.NONE;
121 gbc.weightx = gbc.weighty = 0;
122
123 JLabel label = new JLabel(ENTER);
124 label.setToolTipText("Enter new data into text field and press <return> to add to list");
125 gb.setConstraints(label,gbc);
126 p4.add(label);
127
128 gbc.insets = new Insets(0,10,0,0);
129 gbc.gridwidth = GridBagConstraints.REMAINDER;
130 gbc.anchor = GridBagConstraints.WEST;
131 gbc.fill = GridBagConstraints.HORIZONTAL;
132 gbc.weightx = gbc.weighty = 0;
133
134 textfield = new NameField(20);
135 textfield.addActionListener(this);
136 gb.setConstraints(textfield,gbc);
137 p4.add(textfield);
138
139 JPanel p3 = new JPanel();
140 p3.setLayout(gb);
141
142 gbc.insets = new Insets(10,10,0,10);
143 gbc.gridwidth = GridBagConstraints.REMAINDER;
144 gbc.anchor = GridBagConstraints.WEST;
145 gbc.fill = GridBagConstraints.NONE;
146 gbc.weightx = gbc.weighty = 0;
147
148 gb.setConstraints(p4,gbc);
149 p3.add(p4);
150
151 gbc.insets = new Insets(10,10,10,0);
152 gbc.gridwidth = 1;
153 gbc.anchor = GridBagConstraints.NORTHWEST;
154 gbc.fill = GridBagConstraints.BOTH;
155 gbc.weightx = gbc.weighty = 1;
156
157 gb.setConstraints(scrollpane,gbc);
158 p3.add(scrollpane);
159
160 gbc.gridwidth = GridBagConstraints.REMAINDER;
161 gbc.fill = GridBagConstraints.NONE;
162 gbc.insets = new Insets(10,40,0,10);
163 gbc.weightx = gbc.weighty = 0;
164 gb.setConstraints(p2,gbc);
165 p3.add(p2);
166
167 pane.add("South",p0);
168 pane.add("Center",p3);
169
170
171 okButton.addActionListener(this);
172 cancelButton.addActionListener(this);
173 selectButton.addActionListener(this);
174 clearButton.addActionListener(this);
175 invertButton.addActionListener(this);
176 this.addWindowListener(
177 new WindowAdapter() {
178 public void windowClosing(WindowEvent evt) { setVisible(false); }
179 }
180 );
181 this.pack();
182 }
183
184 public void actionPerformed(ActionEvent evt) {
185 Object source = evt.getSource();
186 int num = list.getModel().getSize();
187
188 if ( source == selectButton ) {
189 list.setSelectionInterval(0,num-1);
190 }
191 else if ( source == clearButton )
192 list.clearSelection();
193 else if ( source == invertButton ) {
194 int[] indices = list.getSelectedIndices();
195 list.clearSelection();
196 for(int i = 0; i < num; i++ ) {
197 boolean status = false;
198 for(int j = 0; !status && j < indices.length; j++ )
199 status = (indices[j] == i);
200 if ( !status )
201 list.addSelectionInterval(i,i);
202 }
203 }
204 else if ( source == okButton ) {
205 if ( !list.isSelectionEmpty() )
206 selection = list.getSelectedValues();
207 this.setVisible(false);
208 }
209 else if ( source == cancelButton ) {
210 this.setVisible(false);
211 }
212 else if ( source == textfield ) {
213 String value = textfield.getText();
214 if ( value == null ) return;
215 value = value.trim();
216 if ( value.equals("") ) return;
217
218 ListModel model = list.getModel();
219 Vector data = new Vector();
220 for(int i = 0; i < model.getSize(); i++ )
221 data.addElement(model.getElementAt(i));
222
223 if ( !data.contains(value) ) {
224 data.addElement(value);
225 list.setListData(data);
226 }
227 textfield.setText("");
228 }
229 }
230
231 public Object[] getPriorSelection() {
232 return list.getSelectedValues();
233 }
234
235 public Object[] getSelection() {
236 selection = null;
237 this.setVisible(true);
238 return selection;
239 }
240
241 public void setListData(Object[] data) {
242 list.setListData(data);
243 }
244
245 public Object[] getListData() {
246 ListModel model = list.getModel();
247 Vector data = new Vector();
248 for(int i = 0; i < model.getSize(); i++ )
249 data.addElement(model.getElementAt(i));
250 return data.toArray();
251 }
252
253 public void setSelection(Object[] selectedData) {
254 ListModel model = list.getModel();
255 int num = model.getSize();
256 list.clearSelection();
257
258 for(int i = 0; i < selectedData.length; i++ )
259 for(int j = 0; j < num; j++ )
260 if ( selectedData[i] == model.getElementAt(j) )
261 list.addSelectionInterval(j,j);
262 }
263
264 public static void main(String arg[]) {
265 JFrame f = new JFrame("Test");
266 f.setSize(200,200);
267 f.show();
268 String[] agents = {"Dave","John","Henry","Alice","Albert"};
269 EditableMultipleSelectionDialog m = new EditableMultipleSelectionDialog(f,"Select Servers");
270 m.setListData(agents);
271 Object[] data = m.getSelection();
272 for(int i = 0; data != null && i < data.length; i++ )
273 System.out.println(data[i]);
274 System.out.println("DONE...");
275 }
276 }