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.visualiser.basic;
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.*;
34  import zeus.gui.fields.*;
35  
36  public class MsgFilterEditor extends JDialog
37                               implements ActionListener, ItemListener {
38  
39    protected JButton okButton;
40    protected JButton cancelButton;
41    protected JLabel fromLabel  = new JLabel("From agent: ");
42    protected JLabel toLabel    = new JLabel("To agent: ");
43    protected JLabel aboutLabel = new JLabel("About topic: ");
44    protected AgentList fromList, toList;
45  
46    protected JCheckBox  checkbox   = null;
47    protected JTextField checkboxTf = null;
48    protected MsgFilter filter = null;
49  
50    public MsgFilterEditor(JFrame parent, String title) {
51      this(parent,title,null);
52    }
53  
54    public MsgFilterEditor(JFrame parent, String title, String[] agents) {
55      super(parent,title,true);
56      getContentPane().setLayout(new BorderLayout());
57  
58      JPanel p1 = new JPanel();
59      p1.setLayout(new GridLayout(1,2,10,10));
60      okButton = new JButton("OK");
61      cancelButton = new JButton("Cancel");
62      p1.add(okButton);
63      p1.add(cancelButton);
64  
65      getContentPane().add("South",p1);
66  
67      JPanel p2 = new JPanel();
68      GridBagLayout gb = new GridBagLayout();
69      GridBagConstraints gbc = new GridBagConstraints();
70  
71      p2.setLayout(gb);
72      gbc.insets = new Insets(10,0,10,0);
73      gbc.anchor = GridBagConstraints.NORTHWEST;
74      gbc.weightx = 1;
75      gbc.fill = GridBagConstraints.NONE;
76      gbc.weightx = gbc.weighty = 0;
77  
78      gbc.insets = new Insets(10,10,0,0);
79      gbc.gridwidth = GridBagConstraints.REMAINDER;
80      gb.setConstraints(fromLabel,gbc);
81      p2.add(fromLabel);
82  
83      fromList = new AgentList(agents);
84      gbc.insets = new Insets(0,0,0,10);
85      gbc.gridwidth = GridBagConstraints.REMAINDER;
86      gb.setConstraints(fromList,gbc);
87      p2.add(fromList);
88  
89      gbc.insets = new Insets(0,10,0,0);
90      gbc.gridwidth = GridBagConstraints.REMAINDER;
91      gb.setConstraints(toLabel,gbc);
92      p2.add(toLabel);
93  
94      toList = new AgentList(agents);
95      gbc.insets = new Insets(0,0,0,10);
96      gbc.gridwidth = GridBagConstraints.REMAINDER;
97      gb.setConstraints(toList,gbc);
98      p2.add(toList);
99  
100     gbc.insets = new Insets(0,10,0,0);
101     gbc.gridwidth = GridBagConstraints.REMAINDER;
102     gb.setConstraints(aboutLabel,gbc);
103     p2.add(aboutLabel);
104 
105     checkbox = new JCheckBox("About");
106     checkboxTf = new JTextField(20);
107 
108     gbc.gridwidth = 1;
109     gbc.insets = new Insets(10,10,0,0);
110     gbc.anchor = GridBagConstraints.WEST;
111     gbc.weightx = gbc.weighty = 0;
112     gbc.fill = GridBagConstraints.NONE;
113     gb.setConstraints(checkbox,gbc);
114     p2.add(checkbox);
115 
116     gbc.insets = new Insets(10,10,0,10);
117     gbc.fill = GridBagConstraints.HORIZONTAL;
118     gbc.gridwidth = GridBagConstraints.REMAINDER;
119     gb.setConstraints(checkboxTf,gbc);
120     p2.add(checkboxTf);
121 
122     JSeparator s1 = new JSeparator();
123     gbc.insets = new Insets(10,0,10,0);
124     gbc.fill = GridBagConstraints.HORIZONTAL;
125     gbc.gridwidth = GridBagConstraints.REMAINDER;
126     gb.setConstraints(s1,gbc);
127     p2.add(s1);
128 
129 
130     getContentPane().add("Center",p2);
131 
132     /* Event handling */
133     okButton.addActionListener(this);
134     cancelButton.addActionListener(this);
135     checkbox.addItemListener(this);
136     this.addWindowListener(
137        new WindowAdapter() {
138           public void windowClosing(WindowEvent evt) { setVisible(false); }
139        }
140     );
141 
142     /* Initialisation */
143     checkbox.setSelected(false);
144     checkboxTf.setText("");
145     checkboxTf.setEnabled(checkbox.isSelected());
146   }
147 
148   public void itemStateChanged(ItemEvent evt) {
149     Object source = evt.getSource();
150     if ( source == checkbox ) {
151        checkboxTf.setEnabled(checkbox.isSelected());
152     }
153   }
154 
155   public void actionPerformed(ActionEvent evt) {
156     Object source = evt.getSource();
157 
158     if ( source == okButton ) {
159        filter = new MsgFilter();
160        filter.from = Misc.stringArray(fromList.getSelection());
161        filter.to = Misc.stringArray(toList.getSelection());
162        if ( checkbox.isSelected() )
163           filter.about = checkboxTf.getText();
164        this.setVisible(false);
165     }
166     else if ( source == cancelButton ) {
167       this.setVisible(false);
168     }
169   }
170 
171   public void setListData(String[] agents) {
172     fromList.setListData(agents);
173     toList.setListData(agents);
174   }
175 
176   public void setFilter(MsgFilter filter) {
177     String[] from = new String[0], to = new String[0];
178     String about = null;
179 
180     if ( filter != null ) {
181        from = filter.from;
182        to = filter.to;
183        about = filter.about;
184     }
185 
186     fromList.setSelection(from);
187     toList.setSelection(to);
188 
189     checkbox.setSelected(false);
190     checkboxTf.setEditable(checkbox.isSelected());
191 
192     if ( about != null ) {
193        checkbox.setSelected(true);
194        checkboxTf.setEditable(checkbox.isSelected());
195        checkboxTf.setText(about);
196     }
197   }
198 
199   public synchronized MsgFilter getFilter() {
200     filter = null;
201     this.pack();
202     this.setVisible(true);
203     return filter;
204   }
205 
206   protected class AgentList extends JPanel
207                             implements ActionListener {
208 
209     protected JButton selectButton, clearButton, invertButton;
210     protected String ENTER = "Add item:";
211     protected String SELECT_ALL = "Select All";
212     protected String CLEAR_ALL = "Clear All";
213     protected String INVERT = "Invert Selection";
214     protected NameField textfield;
215     protected JList list;
216     protected Object[] selection = null;
217   
218     public AgentList(Object[] data) {
219       this();
220       list.setListData(data);
221     }
222   
223     public AgentList(Object[] data, Object[] selectedData) {
224       this();
225       list.setListData(data);
226       setSelection(selectedData);
227     }
228   
229     public AgentList() {
230       list = new JList();
231       list.setSelectionModel(new DefaultListSelectionModel());
232       list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
233       JScrollPane scrollpane = new JScrollPane();
234       scrollpane.getViewport().setView(list);
235       scrollpane.setPreferredSize(new Dimension(150,150));
236       selectButton = new JButton(SELECT_ALL);
237       clearButton = new JButton(CLEAR_ALL);
238       invertButton = new JButton(INVERT);
239   
240       JPanel p2  = new JPanel();
241       p2.setLayout(new GridLayout(3,1,2,2));
242       p2.add(selectButton);
243       p2.add(clearButton);
244       p2.add(invertButton);
245   
246       JPanel p4 = new JPanel();
247       GridBagLayout gb = new GridBagLayout();
248       GridBagConstraints gbc = new GridBagConstraints();
249   
250       p4.setLayout(gb);
251   
252       gbc.insets = new Insets(0,0,0,0);
253       gbc.gridwidth = 1;
254       gbc.anchor = GridBagConstraints.WEST;
255       gbc.fill = GridBagConstraints.NONE;
256       gbc.weightx = gbc.weighty = 0;
257   
258       JLabel label = new JLabel(ENTER);
259       label.setToolTipText("Enter new data into text field and press <return> to add to list");
260       gb.setConstraints(label,gbc);
261       p4.add(label);
262   
263       gbc.insets = new Insets(0,10,0,0);
264       gbc.gridwidth = GridBagConstraints.REMAINDER;
265       gbc.anchor = GridBagConstraints.WEST;
266       gbc.fill = GridBagConstraints.HORIZONTAL;
267       gbc.weightx = gbc.weighty = 0;
268   
269       textfield = new NameField(20);
270       textfield.addActionListener(this);
271       gb.setConstraints(textfield,gbc);
272       p4.add(textfield);
273   
274       setLayout(gb);
275   
276       gbc.insets = new Insets(10,10,0,10);
277       gbc.gridwidth = GridBagConstraints.REMAINDER;
278       gbc.anchor = GridBagConstraints.WEST;
279       gbc.fill = GridBagConstraints.NONE;
280       gbc.weightx = gbc.weighty = 0;
281   
282       gb.setConstraints(p4,gbc);
283       add(p4);
284   
285       gbc.insets = new Insets(10,10,10,0);
286       gbc.gridwidth = 1;
287       gbc.anchor = GridBagConstraints.NORTHWEST;
288       gbc.fill = GridBagConstraints.BOTH;
289       gbc.weightx = gbc.weighty = 1;
290   
291       gb.setConstraints(scrollpane,gbc);
292       add(scrollpane);
293   
294       gbc.gridwidth = GridBagConstraints.REMAINDER;
295       gbc.fill = GridBagConstraints.NONE;
296       gbc.insets = new Insets(10,40,0,10);
297       gbc.weightx = gbc.weighty = 0;
298       gb.setConstraints(p2,gbc);
299       add(p2);
300   
301       // Event handling
302       selectButton.addActionListener(this);
303       clearButton.addActionListener(this);
304       invertButton.addActionListener(this);
305     }
306   
307     public void actionPerformed(ActionEvent evt) {
308       Object source = evt.getSource();
309       int num = list.getModel().getSize();
310   
311       if ( source == selectButton ) {
312          list.setSelectionInterval(0,num-1);
313       }
314       else if ( source == clearButton )
315          list.clearSelection();
316       else if ( source == invertButton ) {
317          int[] indices = list.getSelectedIndices();
318          list.clearSelection();
319          for(int i = 0; i < num; i++ ) {
320             boolean status = false;
321             for(int j = 0; !status && j < indices.length; j++ )
322                status = (indices[j] == i);
323             if ( !status )
324                list.addSelectionInterval(i,i);
325          }
326       }
327       else if ( source == textfield ) {
328          String value = textfield.getText();
329          if ( value == null ) return;
330          value = value.trim();
331          if ( value.equals("") ) return;
332   
333          ListModel model = list.getModel();
334          Vector data = new Vector();
335          for(int i = 0; i < model.getSize(); i++ )
336             data.addElement(model.getElementAt(i));
337   
338          if ( !data.contains(value) ) {
339             data.addElement(value);
340             list.setListData(data);
341          }
342          textfield.setText("");
343       }
344     }
345   
346     public Object[] getPriorSelection() {
347        return list.getSelectedValues();
348     }
349   
350     public Object[] getSelection() {
351        selection = list.getSelectedValues();
352        if ( selection.length == 0 ) return null;
353        return selection;
354     }
355   
356     public void setListData(Object[] data) {
357        list.setListData(data);
358     }
359   
360     public Object[] getListData() {
361        ListModel model = list.getModel();
362        Vector data = new Vector();
363        for(int i = 0; i < model.getSize(); i++ )
364           data.addElement(model.getElementAt(i));
365        return data.toArray();
366     }
367   
368     public void setSelection(Object[] selectedData) {
369       ListModel model = list.getModel();
370       int num = model.getSize();
371       list.clearSelection();
372   
373       for(int i = 0; i < selectedData.length; i++ )
374          for(int j = 0; j < num; j++ )
375             if ( selectedData[i] == model.getElementAt(j) )
376                list.addSelectionInterval(j,j);
377     }
378   }
379 
380 
381   public static void main(String arg[]) {
382     JFrame f = new JFrame("Test");
383     f.setSize(200,200);
384     f.show();
385     String[] agents = {"Dave","John","Henry","Alice","Albert"};
386     MsgFilterEditor m = new MsgFilterEditor(f,"Edit Filter",agents);
387     MsgFilter data = m.getFilter();
388     System.out.println(data);
389     System.exit(0);
390   }
391 }