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.border.*;
31  
32  import zeus.util.*;
33  import zeus.gui.fields.*;
34  
35  
36  public class EditableSingleSelectionDialog extends JDialog
37                                     implements ActionListener {
38  
39    protected JButton cancelButton;
40    protected String OK = "OK";
41    protected String CANCEL = "Cancel";
42    protected JList list;
43    protected JButton okButton;
44    protected Object selection = null;
45    protected String ENTER = "Enter new item:";
46    protected NameField textfield;
47  
48    public EditableSingleSelectionDialog(Frame parent, String title, 
49                                         Object[] data) {
50      this(parent,title);
51      list.setListData(data);
52    }
53  
54    public EditableSingleSelectionDialog(Frame parent, String title,
55                                         Object[] data, Object selectedData) {
56      this(parent,title);
57      list.setListData(data);
58      list.setSelectedValue(selectedData,true);
59    }
60  
61    public EditableSingleSelectionDialog(Frame parent, String title) {
62      super(parent,title,true);
63  
64      JPanel pane = (JPanel)getContentPane();
65      pane.setLayout( new BorderLayout() );
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.SINGLE_SELECTION);
97      list.setPreferredSize(new Dimension(100,100));
98      JScrollPane scrollpane = new JScrollPane();
99      scrollpane.getViewport().setView(list);
100 
101     JPanel p4 = new JPanel();
102     p4.setLayout(gb);
103 
104     gbc.insets = new Insets(0,0,0,0);
105     gbc.gridwidth = 1;
106     gbc.anchor = GridBagConstraints.WEST;
107     gbc.fill = GridBagConstraints.NONE;
108     gbc.weightx = gbc.weighty = 0;
109 
110     JLabel label = new JLabel(ENTER);
111     label.setToolTipText("Enter new data into text field and press <return> to add to list");
112     gb.setConstraints(label,gbc);
113     p4.add(label);
114 
115     gbc.insets = new Insets(0,10,0,0);
116     gbc.gridwidth = GridBagConstraints.REMAINDER;
117     gbc.anchor = GridBagConstraints.WEST;
118     gbc.fill = GridBagConstraints.HORIZONTAL;
119     gbc.weightx = gbc.weighty = 1;
120 
121     textfield = new NameField(20);
122     textfield.addActionListener(this);
123     gb.setConstraints(textfield,gbc);
124     p4.add(textfield);
125 
126     JPanel p3 = new JPanel();
127     p3.setLayout(gb);
128 
129     gbc.insets = new Insets(10,10,0,10);
130     gbc.gridwidth = GridBagConstraints.REMAINDER;
131     gbc.anchor = GridBagConstraints.WEST;
132     gbc.fill = GridBagConstraints.NONE;
133     gbc.weightx = gbc.weighty = 0;
134 
135     gb.setConstraints(p4,gbc);
136     p3.add(p4);
137 
138     gbc.insets = new Insets(10,10,10,10);
139     gbc.gridwidth = 1;
140     gbc.anchor = GridBagConstraints.NORTHWEST;
141     gbc.fill = GridBagConstraints.BOTH;
142     gbc.weightx = gbc.weighty = 1;
143 
144     gb.setConstraints(scrollpane,gbc);
145     p3.add(scrollpane);
146 
147     pane.add("South",p0);
148     pane.add("Center",p3);
149 
150     gb.setConstraints(scrollpane,gbc);
151     p3.add(scrollpane);
152 
153     pane.add("South",p0);
154     pane.add("Center",p3);
155 
156     // Event handling
157     okButton.addActionListener(this);
158     cancelButton.addActionListener(this);
159     this.addWindowListener(
160        new WindowAdapter() {
161           public void windowClosing(WindowEvent evt) { setVisible(false); }
162        }
163     );
164     this.pack();
165   }
166 
167   public void actionPerformed(ActionEvent evt) {
168     Object source = evt.getSource();
169 
170     if ( source == okButton ) {
171        selection = list.getSelectedValue();
172        if ( selection == null ) {
173           int result = JOptionPane.showConfirmDialog(this,
174              "No value selected\nContinue?", "Warning",
175 	     JOptionPane.YES_NO_OPTION);
176           switch(result) {
177 	     case JOptionPane.YES_OPTION:
178                   break;
179 
180              case JOptionPane.NO_OPTION:
181                   return;
182           }
183        }
184        this.setVisible(false);
185     }
186     else if ( source == cancelButton ) {
187        this.setVisible(false);
188     }
189     else if ( source == textfield ) {
190        String value = textfield.getText();
191        if ( value == null ) return;
192        value = value.trim();
193        if ( value.equals("") ) return;
194 
195        ListModel model = list.getModel();
196        Vector data = new Vector();
197        for(int i = 0; i < model.getSize(); i++ )
198           data.addElement(model.getElementAt(i));
199 
200        if ( !data.contains(value) ) {
201           data.addElement(value);
202           list.setListData(data);
203        }
204        textfield.setText("");
205     }
206   }
207 
208   public Object getSelection() {
209      selection = null;
210      this.setVisible(true);
211      return selection;
212   }
213 
214   public void setListData(Object[] data) {
215      list.setListData(data);
216   }
217 
218   public Object[] getListData() {
219      ListModel model = list.getModel();
220      Vector data = new Vector();
221      for(int i = 0; i < model.getSize(); i++ )
222         data.addElement(model.getElementAt(i));
223      return data.toArray();
224   }
225   
226   public void setSelection(Object value) {
227      list.setSelectedValue(value,true);
228   }
229 
230   public static void main(String arg[]) {
231     JFrame f = new JFrame("Test");
232     f.setSize(200,200);
233     f.show();
234     String[] agents = {"Dave","John","Henry","Alice","Albert"};
235     EditableSingleSelectionDialog m = new EditableSingleSelectionDialog(f,"Select Servers");
236     m.setListData(agents);
237     Object data = m.getSelection();
238     System.out.println(data);
239   }
240 }