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 import javax.swing.event.*;
32
33 import zeus.util.*;
34 import zeus.gui.fields.*;
35
36
37 public class EditableDoubleSelectionDialog extends JDialog
38 implements ActionListener,
39 ListSelectionListener {
40
41 protected JButton cancelButton;
42 protected String OK = "OK";
43 protected String CANCEL = "Cancel";
44 protected JList lhsList, rhsList;
45 protected DefaultListModel rhsListModel, lhsListModel;
46 protected JButton okButton;
47 protected NameField lhsTextfield;
48 protected NameField rhsTextfield;
49 protected Hashtable input = new Hashtable();
50 protected Object[] selection = null;
51
52 public EditableDoubleSelectionDialog(Frame parent, String title,
53 String leftLabel, String rightLabel,
54 Hashtable data) {
55 this(parent,title,leftLabel,rightLabel);
56 setListData(input);
57 }
58
59 public EditableDoubleSelectionDialog(Frame parent, String title,
60 String leftLabel, String rightLabel,
61 Hashtable input, Object leftItem,
62 Object rightItem) {
63 this(parent,title,leftLabel,rightLabel,input);
64 lhsList.setSelectedValue(leftItem,true);
65 rhsList.setSelectedValue(rightItem,true);
66 }
67
68 public EditableDoubleSelectionDialog(Frame parent, String title,
69 String leftLabel, String rightLabel) {
70
71 super(parent,title,true);
72
73 JPanel pane = (JPanel)getContentPane();
74 pane.setLayout( new BorderLayout() );
75
76 JPanel p1 = new JPanel();
77 p1.setLayout(new GridLayout(1,2,10,10));
78 okButton = new JButton(OK);
79 cancelButton = new JButton(CANCEL);
80 p1.add(okButton);
81 p1.add(cancelButton);
82
83 GridBagLayout gb = new GridBagLayout();
84 GridBagConstraints gbc = new GridBagConstraints();
85 gbc.insets = new Insets(10,0,10,0);
86 gbc.anchor = GridBagConstraints.NORTHWEST;
87 gbc.fill = GridBagConstraints.HORIZONTAL;
88 gbc.gridwidth = GridBagConstraints.REMAINDER;
89 gbc.weightx = 1;
90
91 JPanel p0 = new JPanel();
92 JSeparator s1 = new JSeparator();
93 p0.setLayout(gb);
94 gb.setConstraints(s1,gbc);
95 p0.add(s1);
96
97 gbc.anchor = GridBagConstraints.CENTER;
98 gbc.fill = GridBagConstraints.NONE;
99 gbc.insets = new Insets(0,0,10,0);
100 gb.setConstraints(p1,gbc);
101 p0.add(p1);
102
103 lhsListModel = new DefaultListModel();
104 lhsList = new JList(lhsListModel);
105 lhsList.setSelectionModel(new DefaultListSelectionModel());
106 lhsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
107 lhsList.addListSelectionListener(this);
108 lhsList.setPreferredSize(new Dimension(60,100));
109 JScrollPane lhsSP = new JScrollPane();
110 lhsSP.getViewport().setView(lhsList);
111
112 rhsListModel = new DefaultListModel();
113 rhsList = new JList(rhsListModel);
114 rhsList.setSelectionModel(new DefaultListSelectionModel());
115 rhsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
116 rhsList.setPreferredSize(new Dimension(60,100));
117 JScrollPane rhsSP = new JScrollPane();
118 rhsSP.getViewport().setView(rhsList);
119
120 JPanel p4 = new JPanel();
121 p4.setLayout(gb);
122
123 gbc.insets = new Insets(0,0,0,0);
124 gbc.gridwidth = 1;
125 gbc.anchor = GridBagConstraints.WEST;
126 gbc.fill = GridBagConstraints.NONE;
127 gbc.weightx = gbc.weighty = 0;
128
129 JLabel label = new JLabel(leftLabel);
130 label.setToolTipText("Enter new data into text field and press <return> to add to list");
131 gb.setConstraints(label,gbc);
132 p4.add(label);
133
134 gbc.insets = new Insets(0,10,0,0);
135 gbc.gridwidth = GridBagConstraints.REMAINDER;
136 gbc.anchor = GridBagConstraints.WEST;
137 gbc.fill = GridBagConstraints.HORIZONTAL;
138 gbc.weightx = gbc.weighty = 1;
139
140 lhsTextfield = new NameField(10);
141 lhsTextfield.addActionListener(this);
142 gb.setConstraints(lhsTextfield,gbc);
143 p4.add(lhsTextfield);
144
145 JPanel p5 = new JPanel();
146 p5.setLayout(gb);
147
148 gbc = new GridBagConstraints();
149 gbc.insets = new Insets(0,0,0,0);
150 gbc.gridwidth = 1;
151 gbc.anchor = GridBagConstraints.WEST;
152 gbc.fill = GridBagConstraints.NONE;
153 gbc.weightx = gbc.weighty = 0;
154
155 label = new JLabel(rightLabel);
156 label.setToolTipText("Enter new data into text field and press <return> to add to list");
157 gb.setConstraints(label,gbc);
158 p5.add(label);
159
160 gbc.insets = new Insets(0,10,0,0);
161 gbc.gridwidth = GridBagConstraints.REMAINDER;
162 gbc.anchor = GridBagConstraints.WEST;
163 gbc.fill = GridBagConstraints.HORIZONTAL;
164 gbc.weightx = gbc.weighty = 1;
165
166 rhsTextfield = new NameField(10);
167 rhsTextfield.addActionListener(this);
168 gb.setConstraints(rhsTextfield,gbc);
169 p5.add(rhsTextfield);
170
171 JPanel p3 = new JPanel();
172 p3.setLayout(gb);
173
174 gbc.insets = new Insets(10,10,0,0);
175 gbc.gridwidth = 1;
176 gbc.anchor = GridBagConstraints.WEST;
177 gbc.fill = GridBagConstraints.HORIZONTAL;
178 gbc.weightx = gbc.weighty = 0;
179
180 gb.setConstraints(p4,gbc);
181 p3.add(p4);
182
183 gbc.insets = new Insets(10,10,0,10);
184 gbc.gridwidth = GridBagConstraints.REMAINDER;
185 gbc.anchor = GridBagConstraints.WEST;
186 gbc.fill = GridBagConstraints.HORIZONTAL;
187 gbc.weightx = gbc.weighty = 0;
188
189 gb.setConstraints(p5,gbc);
190 p3.add(p5);
191
192 gbc.insets = new Insets(10,10,10,0);
193 gbc.gridwidth = 1;
194 gbc.anchor = GridBagConstraints.NORTHWEST;
195 gbc.fill = GridBagConstraints.BOTH;
196 gbc.weightx = gbc.weighty = 1;
197
198 gb.setConstraints(lhsSP,gbc);
199 p3.add(lhsSP);
200
201 gbc.insets = new Insets(10,10,10,10);
202 gbc.gridwidth = GridBagConstraints.REMAINDER;
203 gbc.anchor = GridBagConstraints.NORTHWEST;
204 gbc.fill = GridBagConstraints.BOTH;
205 gbc.weightx = gbc.weighty = 1;
206
207 gb.setConstraints(rhsSP,gbc);
208 p3.add(rhsSP);
209
210 pane.add("South",p0);
211 pane.add("Center",p3);
212
213
214 okButton.addActionListener(this);
215 cancelButton.addActionListener(this);
216 this.addWindowListener(
217 new WindowAdapter() {
218 public void windowClosing(WindowEvent evt) { setVisible(false); }
219 }
220 );
221 this.pack();
222 }
223
224 public void valueChanged(ListSelectionEvent evt) {
225 if ( evt.getValueIsAdjusting() ) return;
226 JList list = (JList)evt.getSource();
227 String value = (String)list.getSelectedValue();
228 if ( value == null ) return;
229
230 if ( list == lhsList ) {
231 rhsList.clearSelection();
232 HSet data = (HSet)input.get(value);
233 Enumeration enum = data.elements();
234 rhsListModel.removeAllElements();
235 while( enum.hasMoreElements() )
236 rhsListModel.addElement(enum.nextElement());
237 }
238 }
239
240 public void actionPerformed(ActionEvent evt) {
241 Object source = evt.getSource();
242
243 if ( source == okButton ) {
244 int result;
245 selection = new Object[2];
246 selection[0] = lhsList.getSelectedValue();
247 if ( selection[0] == null ) {
248 result = JOptionPane.showConfirmDialog(this,
249 "No value selected in left list\nContinue?", "Warning",
250 JOptionPane.YES_NO_OPTION);
251 switch(result) {
252 case JOptionPane.YES_OPTION:
253 selection = null;
254 setVisible(false);
255 return;
256
257 case JOptionPane.NO_OPTION:
258 return;
259 }
260 }
261 selection[1] = rhsList.getSelectedValue();
262 if ( selection[1] == null ) {
263 result = JOptionPane.showConfirmDialog(this,
264 "No value selected in right list\nContinue?", "Warning",
265 JOptionPane.YES_NO_OPTION);
266 switch(result) {
267 case JOptionPane.YES_OPTION:
268 selection = null;
269 setVisible(false);
270 return;
271
272 case JOptionPane.NO_OPTION:
273 return;
274 }
275 }
276 this.setVisible(false);
277 }
278 else if ( source == cancelButton ) {
279 this.setVisible(false);
280 }
281 else if ( source == lhsTextfield ) {
282 String value = lhsTextfield.getText();
283 if ( value == null ) return;
284 value = value.trim();
285 if ( value.equals("") ) return;
286
287 if ( !lhsListModel.contains(value) ) {
288 lhsListModel.addElement(value);
289 input.put(value, new HSet());
290 }
291 lhsTextfield.setText("");
292 }
293 else if ( source == rhsTextfield ) {
294 String value = rhsTextfield.getText();
295 if ( value == null ) return;
296 value = value.trim();
297 if ( value.equals("") ) return;
298
299
300 if ( !rhsListModel.contains(value) ) {
301 String agent = (String)lhsList.getSelectedValue();
302 if ( agent == null ) {
303 JOptionPane.showMessageDialog(this,
304 "Selection an item on the left list\nbefore typing <return>",
305 "Error", JOptionPane.ERROR_MESSAGE);
306 return;
307 }
308 rhsListModel.addElement(value);
309 HSet inner = (HSet)input.get(agent);
310 inner.add(value);
311 }
312 rhsTextfield.setText("");
313 }
314 }
315
316 public Object[] getSelection() {
317 selection = null;
318 this.setVisible(true);
319 return selection;
320 }
321
322 public Object getPriorSelection() {
323 Object[] prior = new Object[2];
324 prior[0] = lhsList.getSelectedValue();
325 prior[1] = rhsList.getSelectedValue();
326 return prior;
327 }
328
329 public void setListData(Hashtable input) {
330 this.input.clear();
331 lhsList.clearSelection();
332 rhsList.clearSelection();
333 lhsListModel.removeAllElements();
334 rhsListModel.removeAllElements();
335 Enumeration enum = input.keys();
336 Object lvalue, rvalue;
337 while( enum.hasMoreElements() ) {
338 lvalue = enum.nextElement();
339 rvalue = input.get(lvalue);
340 lhsListModel.addElement(lvalue);
341 this.input.put(lvalue,rvalue);
342 }
343 }
344
345 public Hashtable getListData() {
346 return input;
347 }
348
349 public void setSelection(Object leftValue, Object rightValue) {
350 if ( leftValue != null ) lhsList.setSelectedValue(leftValue,true);
351 if ( rightValue != null ) rhsList.setSelectedValue(rightValue,true);
352 }
353 }