View Javadoc

1   /*
2   * The contents of this file are subject to the BT "ZEUS" Open Source 
3   * Licence (L77741), Version 1.0 (the "Licence"); you may not use this file 
4   * except in compliance with the Licence. You may obtain a copy of the Licence
5   * from $ZEUS_INSTALL/licence.html or alternatively from
6   * http://www.labs.bt.com/projects/agents/zeus/licence.htm
7   * 
8   * Except as stated in Clause 7 of the Licence, software distributed under the
9   * Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or 
10  * implied. See the Licence for the specific language governing rights and 
11  * limitations under the Licence.
12  * 
13  * The Original Code is within the package zeus.*.
14  * The Initial Developer of the Original Code is British Telecommunications
15  * public limited company, whose registered office is at 81 Newgate Street, 
16  * London, EC1A 7AJ, England. Portions created by British Telecommunications 
17  * public limited company are Copyright 1996-9. All Rights Reserved.
18  * 
19  * THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
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.event.*;
31  
32  import zeus.util.*;
33  
34  
35  public class DoubleSelectionDialog extends JDialog
36                                     implements ActionListener,
37                                                ListSelectionListener {
38  
39    protected JButton cancelButton;
40    protected String OK = "OK";
41    protected String CANCEL = "Cancel";
42    protected JList lhsList, rhsList;
43    protected DefaultListModel rhsListModel, lhsListModel;
44    protected JButton okButton;
45    protected Hashtable input = new Hashtable();
46    protected Object[]  selection = null;
47  
48    public DoubleSelectionDialog(Frame parent, String title,
49                                 String leftLabel, String rightLabel,
50                                 Hashtable data) {
51      this(parent,title,leftLabel,rightLabel);
52      setListData(input);
53    }
54  
55    public DoubleSelectionDialog(Frame parent, String title,
56                                 String leftLabel, String rightLabel,
57  			       Hashtable input, Object leftItem,
58                                 Object rightItem) {
59      this(parent,title,leftLabel,rightLabel,input);
60      lhsList.setSelectedValue(leftItem,true);
61      rhsList.setSelectedValue(rightItem,true);
62    }
63  
64    public DoubleSelectionDialog(Frame parent, String title,
65                                 String leftLabel, String rightLabel) {
66  
67      super(parent,title,true);
68  
69      JPanel pane = (JPanel)getContentPane();
70      pane.setLayout( new BorderLayout() );
71  
72      JPanel p1 = new JPanel();
73      p1.setLayout(new GridLayout(1,2,10,10));
74      okButton = new JButton(OK);
75      cancelButton = new JButton(CANCEL);
76      p1.add(okButton);
77      p1.add(cancelButton);
78  
79      GridBagLayout gb = new GridBagLayout();
80      GridBagConstraints gbc = new GridBagConstraints();
81      gbc.insets = new Insets(10,0,10,0);
82      gbc.anchor = GridBagConstraints.NORTHWEST;
83      gbc.fill = GridBagConstraints.HORIZONTAL;
84      gbc.gridwidth = GridBagConstraints.REMAINDER;
85      gbc.weightx = 1;
86  
87      JPanel p0 = new JPanel();
88      JSeparator s1 = new JSeparator();
89      p0.setLayout(gb);
90      gb.setConstraints(s1,gbc);
91      p0.add(s1);
92  
93      gbc.anchor = GridBagConstraints.CENTER;
94      gbc.fill = GridBagConstraints.NONE;
95      gbc.insets = new Insets(0,0,10,0);
96      gb.setConstraints(p1,gbc);
97      p0.add(p1);
98  
99      lhsListModel = new DefaultListModel();
100     lhsList = new JList(lhsListModel);
101     lhsList.setSelectionModel(new DefaultListSelectionModel());
102     lhsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
103     lhsList.addListSelectionListener(this);
104     lhsList.setPreferredSize(new Dimension(60,100));
105     JScrollPane lhsSP = new JScrollPane();
106     lhsSP.getViewport().setView(lhsList);
107 
108     rhsListModel = new DefaultListModel();
109     rhsList = new JList(rhsListModel);
110     rhsList.setSelectionModel(new DefaultListSelectionModel());
111     rhsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
112     rhsList.setPreferredSize(new Dimension(60,100));
113     JScrollPane rhsSP = new JScrollPane();
114     rhsSP.getViewport().setView(rhsList);
115 
116     JPanel p3 = new JPanel();
117     p3.setLayout(gb);
118 
119     gbc.insets = new Insets(10,10,10,0);
120     gbc.gridwidth = 1;
121     gbc.anchor = GridBagConstraints.NORTHWEST;
122     gbc.fill = GridBagConstraints.BOTH;
123     gbc.weightx = gbc.weighty = 1;
124 
125     gb.setConstraints(lhsSP,gbc);
126     p3.add(lhsSP);
127 
128     gbc.insets = new Insets(10,10,10,10);
129     gbc.gridwidth = GridBagConstraints.REMAINDER;
130     gbc.anchor = GridBagConstraints.NORTHWEST;
131     gbc.fill = GridBagConstraints.BOTH;
132     gbc.weightx = gbc.weighty = 1;
133 
134     gb.setConstraints(rhsSP,gbc);
135     p3.add(rhsSP);
136 
137     pane.add("South",p0);
138     pane.add("Center",p3);
139 
140     // Event handling
141     okButton.addActionListener(this);
142     cancelButton.addActionListener(this);
143     this.addWindowListener(
144        new WindowAdapter() {
145           public void windowClosing(WindowEvent evt) { setVisible(false); }
146        }
147     );
148     this.pack();
149   }
150 
151   public void valueChanged(ListSelectionEvent evt) {
152      if ( evt.getValueIsAdjusting() ) return;
153      JList list = (JList)evt.getSource();
154      String value = (String)list.getSelectedValue();
155      if ( value == null ) return;
156 
157      if ( list == lhsList ) {
158         rhsList.clearSelection();
159         HSet data = (HSet)input.get(value);
160         Enumeration enum = data.elements();
161         rhsListModel.removeAllElements();
162         while( enum.hasMoreElements() )
163            rhsListModel.addElement(enum.nextElement());
164      }
165   }
166 
167   public void actionPerformed(ActionEvent evt) {
168     Object source = evt.getSource();
169 
170     if ( source == okButton ) {
171        int result;
172        selection = new Object[2];
173        selection[0] = lhsList.getSelectedValue();
174        if ( selection[0] == null ) {
175           result = JOptionPane.showConfirmDialog(this,
176              "No value selected in left list\nContinue?", "Warning",
177 	     JOptionPane.YES_NO_OPTION);
178           switch(result) {
179 	     case JOptionPane.YES_OPTION:
180                   selection = null;
181                   setVisible(false);
182                   return;
183 
184              case JOptionPane.NO_OPTION:
185                   return;
186           }
187        }
188        selection[1] = rhsList.getSelectedValue();
189        if ( selection[1] == null ) {
190           result = JOptionPane.showConfirmDialog(this,
191              "No value selected in right list\nContinue?", "Warning",
192 	     JOptionPane.YES_NO_OPTION);
193           switch(result) {
194 	     case JOptionPane.YES_OPTION:
195                   selection = null;
196                   setVisible(false);
197                   return;
198 
199              case JOptionPane.NO_OPTION:
200                   return;
201           }
202        }
203        this.setVisible(false);
204     }
205     else if ( source == cancelButton ) {
206        this.setVisible(false);
207     }
208   }
209 
210   public Object[] getSelection() {
211      selection = null;
212      this.setVisible(true);
213      return selection;
214   }
215 
216   public Object[] getPriorSelection() {
217      Object[] prior = new Object[2];
218      prior[0] = lhsList.getSelectedValue();
219      prior[1] = rhsList.getSelectedValue();
220      return prior;
221   }
222 
223   public void setListData(Hashtable input) {
224      this.input.clear();
225      lhsList.clearSelection();
226      rhsList.clearSelection();
227      lhsListModel.removeAllElements();
228      rhsListModel.removeAllElements();
229      Enumeration enum = input.keys();
230      Object lvalue, rvalue;
231      while( enum.hasMoreElements() ) {
232         lvalue = enum.nextElement();
233         rvalue = input.get(lvalue);
234         lhsListModel.addElement(lvalue);
235         this.input.put(lvalue,rvalue);
236      }
237   }
238 
239   public Hashtable getListData() {
240      return input;
241   }
242 
243   public void setSelection(Object leftValue, Object rightValue) {
244      if ( leftValue != null  ) lhsList.setSelectedValue(leftValue,true);
245      if ( rightValue != null ) rhsList.setSelectedValue(rightValue,true);
246   }
247 }