KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > gui > JLabeledChoice


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/gui/JLabeledChoice.java,v 1.5 2004/02/11 23:48:32 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.gui;
20
21 import java.awt.Insets JavaDoc;
22 import java.awt.event.ActionEvent JavaDoc;
23 import java.awt.event.ActionListener JavaDoc;
24 import java.awt.event.ItemEvent JavaDoc;
25 import java.awt.event.ItemListener JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import javax.swing.BorderFactory JavaDoc;
31 import javax.swing.JButton JavaDoc;
32 import javax.swing.JComboBox JavaDoc;
33 import javax.swing.JLabel JavaDoc;
34 import javax.swing.JPanel JavaDoc;
35 import javax.swing.event.ChangeEvent JavaDoc;
36 import javax.swing.event.ChangeListener JavaDoc;
37
38 public class JLabeledChoice extends JPanel JavaDoc implements JLabeledField
39 {
40     private JLabel JavaDoc mLabel = new JLabel JavaDoc();
41     private JComboBox JavaDoc choiceList;
42     
43     // Maybe move to vector if MT problems occur
44
private ArrayList JavaDoc mChangeListeners = new ArrayList JavaDoc(3);
45     
46     private JButton JavaDoc delete, add;
47
48     /**
49      * Default constructor, The label and the Text field are left empty.
50      */

51     public JLabeledChoice()
52     {
53         super();
54         choiceList = new JComboBox JavaDoc();
55         init();
56     }
57
58     public List JavaDoc getComponentList()
59     {
60         List JavaDoc comps = new LinkedList JavaDoc();
61         comps.add(mLabel);
62         comps.add(choiceList);
63         return comps;
64     }
65
66     public JLabeledChoice(String JavaDoc pLabel, boolean editable)
67     {
68         super();
69         choiceList = new JComboBox JavaDoc();
70         mLabel.setText(pLabel);
71         choiceList.setEditable(editable);
72         init();
73     }
74
75     public void setEditable(boolean editable)
76     {
77         choiceList.setEditable(false);//TODO - is this correct?
78
}
79
80     public void addValue(String JavaDoc item)
81     {
82         choiceList.addItem(item);
83     }
84
85     public void setValues(String JavaDoc[] items)
86     {
87         choiceList.removeAllItems();
88         for (int i = 0; i < items.length; i++)
89         {
90             choiceList.addItem(items[i]);
91         }
92     }
93
94     /**
95      * Constructs a new component with the label displaying the
96      * passed text.
97      *
98      * @param pLabel The text to in the label.
99      */

100     public JLabeledChoice(String JavaDoc pLabel, String JavaDoc[] items)
101     {
102         super();
103         mLabel.setText(pLabel);
104         choiceList = new JComboBox JavaDoc(items);
105         choiceList.setEditable(false);
106         init();
107     }
108
109     public JLabeledChoice(String JavaDoc pLabel, String JavaDoc[] items, boolean editable)
110     {
111         super();
112         mLabel.setText(pLabel);
113         choiceList = new JComboBox JavaDoc(items);
114         choiceList.setEditable(editable);
115         init();
116     }
117
118     /**
119      * Initialises all of the components on this panel.
120      */

121     private void init()
122     {
123         /*if(choiceList.isEditable())
124         {
125             choiceList.addActionListener(new ComboListener());
126         }*/

127         choiceList.setBorder(BorderFactory.createLoweredBevelBorder());
128         // Register the handler for focus listening. This handler will
129
// only notify the registered when the text changes from when
130
// the focus is gained to when it is lost.
131
choiceList.addItemListener(new ItemListener JavaDoc()
132         {
133             /**
134              * Callback method when the focus to the Text Field component
135              * is lost.
136              *
137              * @param pFocusEvent The focus event that occured.
138              */

139             public void itemStateChanged(ItemEvent JavaDoc e)
140             {
141                 if (e.getStateChange() == ItemEvent.SELECTED)
142                 {
143                     notifyChangeListeners();
144                 }
145             }
146         });
147
148         // Add the sub components
149
this.add(mLabel);
150         this.add(choiceList);
151         if (choiceList.isEditable())
152         {
153             add = new JButton JavaDoc("Add");
154             add.setMargin(new Insets JavaDoc(1, 1, 1, 1));
155             add.addActionListener(new AddListener());
156             this.add(add);
157             delete = new JButton JavaDoc("Del");
158             delete.setMargin(new Insets JavaDoc(1, 1, 1, 1));
159             delete.addActionListener(new DeleteListener());
160             this.add(delete);
161         }
162
163     }
164
165     /**
166      * Set the text displayed in the label.
167      *
168      * @param pLabel The new label text.
169      */

170     public void setLabel(String JavaDoc pLabel)
171     {
172         mLabel.setText(pLabel);
173     }
174
175     /**
176      * Set the text displayed in the Text Field.
177      *
178      * @param pText The new text to display in the text field.
179      */

180     public void setText(String JavaDoc pText)
181     {
182         choiceList.setSelectedItem(pText);
183     }
184
185     /**
186      * Returns the text in the Text Field.
187      *
188      * @return The text in the Text Field.
189      */

190     public String JavaDoc getText()
191     {
192         return (String JavaDoc) choiceList.getSelectedItem();
193     }
194
195     public Object JavaDoc[] getSelectedItems()
196     {
197         return choiceList.getSelectedObjects();
198     }
199
200     public String JavaDoc[] getItems()
201     {
202         String JavaDoc[] items = new String JavaDoc[choiceList.getItemCount()];
203         for (int i = 0; i < items.length; i++)
204         {
205             items[i] = (String JavaDoc) choiceList.getItemAt(i);
206         }
207         return items;
208     }
209
210     /**
211      * Returns the text of the label.
212      *
213      * @return The text of the label.
214      */

215     public String JavaDoc getLabel()
216     {
217         return mLabel.getText();
218     }
219
220     /**
221      * Adds a change listener, that will be notified when the text in the
222      * text field is changed. The ChangeEvent that will be passed
223      * to registered listeners will contain this object as the source, allowing
224      * the new text to be extracted using the {@link #getText() getText} method.
225      *
226      * @param pChangeListener The listener to add
227      */

228     public void addChangeListener(ChangeListener JavaDoc pChangeListener)
229     {
230         mChangeListeners.add(pChangeListener);
231     }
232
233     /**
234      * Removes a change listener.
235      *
236      * @param pChangeListener The change listener to remove.
237      */

238     public void removeChangeListener(ChangeListener JavaDoc pChangeListener)
239     {
240         mChangeListeners.remove(pChangeListener);
241     }
242
243     /**
244      * Notify all registered change listeners that the
245      * text in the text field has changed.
246      */

247     private void notifyChangeListeners()
248     {
249         ChangeEvent JavaDoc ce = new ChangeEvent JavaDoc(this);
250         for (int index = 0; index < mChangeListeners.size(); index++)
251         {
252             ((ChangeListener JavaDoc) mChangeListeners.get(index)).stateChanged(ce);
253         }
254     }
255
256     private class AddListener implements ActionListener JavaDoc
257     {
258
259         public void actionPerformed(ActionEvent JavaDoc e)
260         {
261             Object JavaDoc item = choiceList.getSelectedItem();
262             int index = choiceList.getSelectedIndex();
263             if (!item.equals(choiceList.getItemAt(index)))
264             {
265                 choiceList.addItem(item);
266             }
267             choiceList.setSelectedItem(item);
268             notifyChangeListeners();
269         }
270     }
271
272     private class DeleteListener implements ActionListener JavaDoc
273     {
274
275         public void actionPerformed(ActionEvent JavaDoc e)
276         {
277             if (choiceList.getItemCount() > 1)
278             {
279                 choiceList.removeItemAt(choiceList.getSelectedIndex());
280                 notifyChangeListeners();
281             }
282         }
283     }
284 }
Popular Tags