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 MultipleSelectionDialog extends JDialog
36 implements ActionListener {
37
38 protected JButton cancelButton, selectButton, clearButton, invertButton;
39 protected String SELECT_ALL = "Select All";
40 protected String CLEAR_ALL = "Clear All";
41 protected String INVERT = "Invert Selection";
42 protected String OK = "OK";
43 protected String CANCEL = "Cancel";
44 protected JList list;
45 protected JButton okButton;
46 protected Object[] selection = null;
47
48 public MultipleSelectionDialog(Frame parent, String title, Object[] data) {
49 this(parent,title);
50 list.setListData(data);
51 }
52
53 public MultipleSelectionDialog(Frame parent, String title,
54 Object[] data, Object[] selectedData) {
55 this(parent,title);
56 list.setListData(data);
57 setSelection(selectedData);
58 }
59
60 public MultipleSelectionDialog(Frame parent, String title) {
61 super(parent,title,true);
62
63 JPanel pane = (JPanel)getContentPane();
64 pane.setLayout( new BorderLayout() );
65
66
67 JPanel p1 = new JPanel();
68 p1.setLayout(new GridLayout(1,2,10,10));
69 okButton = new JButton(OK);
70 cancelButton = new JButton(CANCEL);
71 p1.add(okButton);
72 p1.add(cancelButton);
73
74 GridBagLayout gb = new GridBagLayout();
75 GridBagConstraints gbc = new GridBagConstraints();
76 gbc.insets = new Insets(10,0,10,0);
77 gbc.anchor = GridBagConstraints.NORTHWEST;
78 gbc.fill = GridBagConstraints.HORIZONTAL;
79 gbc.gridwidth = GridBagConstraints.REMAINDER;
80 gbc.weightx = 1;
81
82 JPanel p0 = new JPanel();
83 JSeparator s1 = new JSeparator();
84 p0.setLayout(gb);
85 gb.setConstraints(s1,gbc);
86 p0.add(s1);
87
88 gbc.anchor = GridBagConstraints.CENTER;
89 gbc.fill = GridBagConstraints.NONE;
90 gbc.insets = new Insets(0,0,10,0);
91 gb.setConstraints(p1,gbc);
92 p0.add(p1);
93
94 list = new JList();
95 list.setSelectionModel(new DefaultListSelectionModel());
96 list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
97 JScrollPane scrollpane = new JScrollPane();
98 scrollpane.getViewport().setView(list);
99 scrollpane.setPreferredSize(new Dimension(150,150));
100 selectButton = new JButton(SELECT_ALL);
101 clearButton = new JButton(CLEAR_ALL);
102 invertButton = new JButton(INVERT);
103
104 JPanel p2 = new JPanel();
105 p2.setLayout(new GridLayout(3,1,2,2));
106 p2.add(selectButton);
107 p2.add(clearButton);
108 p2.add(invertButton);
109
110 JPanel p3 = new JPanel();
111 p3.setLayout(gb);
112 gbc.insets = new Insets(10,10,10,0);
113 gbc.gridwidth = 1;
114 gbc.anchor = GridBagConstraints.NORTHWEST;
115 gbc.fill = GridBagConstraints.BOTH;
116 gbc.weightx = gbc.weighty = 1;
117
118 gb.setConstraints(scrollpane,gbc);
119 p3.add(scrollpane);
120
121 gbc.gridwidth = GridBagConstraints.REMAINDER;
122 gbc.fill = GridBagConstraints.NONE;
123 gbc.insets = new Insets(10,40,0,10);
124 gbc.weightx = gbc.weighty = 0;
125 gb.setConstraints(p2,gbc);
126 p3.add(p2);
127
128 pane.add("South",p0);
129 pane.add("Center",p3);
130
131
132 okButton.addActionListener(this);
133 cancelButton.addActionListener(this);
134 selectButton.addActionListener(this);
135 clearButton.addActionListener(this);
136 invertButton.addActionListener(this);
137 this.addWindowListener(
138 new WindowAdapter() {
139 public void windowClosing(WindowEvent evt) { setVisible(false); }
140 }
141 );
142 this.pack();
143 }
144
145 public void actionPerformed(ActionEvent evt) {
146 Object source = evt.getSource();
147 int num = list.getModel().getSize();
148
149 if ( source == selectButton ) {
150 list.setSelectionInterval(0,num-1);
151 }
152 else if ( source == clearButton )
153 list.clearSelection();
154 else if ( source == invertButton ) {
155 int[] indices = list.getSelectedIndices();
156 list.clearSelection();
157 for(int i = 0; i < num; i++ ) {
158 boolean status = false;
159 for(int j = 0; !status && j < indices.length; j++ )
160 status = (indices[j] == i);
161 if ( !status )
162 list.addSelectionInterval(i,i);
163 }
164 }
165 else if ( source == okButton ) {
166 if ( !list.isSelectionEmpty() )
167 selection = list.getSelectedValues();
168 this.setVisible(false);
169 }
170 else if ( source == cancelButton ) {
171 this.setVisible(false);
172 }
173 }
174
175 public Object[] getPriorSelection() {
176 return list.getSelectedValues();
177 }
178
179 public Object[] getSelection() {
180 selection = null;
181 this.setVisible(true);
182 return selection;
183 }
184
185 public void setListData(Object[] data) {
186 list.setListData(data);
187 }
188
189 public void setSelection(Object[] selectedData) {
190 ListModel model = list.getModel();
191 int num = model.getSize();
192 list.clearSelection();
193
194 for(int i = 0; i < selectedData.length; i++ )
195 for(int j = 0; j < num; j++ )
196 if ( selectedData[i] == model.getElementAt(j) )
197 list.addSelectionInterval(j,j);
198 }
199
200 public Object[] getListData() {
201 ListModel model = list.getModel();
202 Vector data = new Vector();
203 for(int i = 0; i < model.getSize(); i++ )
204 data.addElement(model.getElementAt(i));
205 return data.toArray();
206 }
207
208 public static void main(String arg[]) {
209 JFrame f = new JFrame("Test");
210 f.setSize(200,200);
211 f.show();
212 String[] agents = {"Dave","John","Henry","Alice","Albert"};
213 MultipleSelectionDialog m = new MultipleSelectionDialog(f,"Select Servers");
214 m.setListData(agents);
215 Object[] data = m.getSelection();
216 for(int i = 0; data != null && i < data.length; i++ )
217 System.out.println(data[i]);
218 System.out.println("DONE...");
219 }
220 }