001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.jorphan.gui;
020:
021: import java.awt.Insets;
022: import java.awt.event.ActionEvent;
023: import java.awt.event.ActionListener;
024: import java.awt.event.ItemEvent;
025: import java.awt.event.ItemListener;
026: import java.util.ArrayList;
027: import java.util.LinkedList;
028: import java.util.List;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.JButton;
032: import javax.swing.JComboBox;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.event.ChangeEvent;
036: import javax.swing.event.ChangeListener;
037:
038: public class JLabeledChoice extends JPanel implements JLabeledField {
039: private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
040:
041: private final JLabel mLabel = new JLabel();
042:
043: private final JComboBox choiceList;
044:
045: // Maybe move to vector if MT problems occur
046: private final ArrayList mChangeListeners = new ArrayList(3);
047:
048: private JButton delete, add;
049:
050: /**
051: * Default constructor, The label and the Text field are left empty.
052: */
053: public JLabeledChoice() {
054: super ();
055: choiceList = new JComboBox();
056: init();
057: }
058:
059: public JLabeledChoice(String pLabel, boolean editable) {
060: super ();
061: choiceList = new JComboBox();
062: mLabel.setText(pLabel);
063: choiceList.setEditable(editable);
064: init();
065: }
066:
067: /**
068: * Constructs a non-edittable combo-box with the label displaying the passed text.
069: *
070: * @param pLabel - the text to display in the label.
071: * @param items - the items to display in the Combo box
072: */
073: public JLabeledChoice(String pLabel, String[] items) {
074: this (pLabel, items, false);
075: }
076:
077: /**
078: * Constructs a combo-box with the label displaying the passed text.
079: *
080: * @param pLabel - the text to display in the label.
081: * @param items - the items to display in the Combo box
082: * @param editable - if true, then Add and Delete buttons are created.
083: *
084: */
085: public JLabeledChoice(String pLabel, String[] items,
086: boolean editable) {
087: super ();
088: mLabel.setText(pLabel);
089: choiceList = new JComboBox(items);
090: choiceList.setEditable(editable);
091: init();
092: }
093:
094: public List getComponentList() {
095: List comps = new LinkedList();
096: comps.add(mLabel);
097: comps.add(choiceList);
098: return comps;
099: }
100:
101: public void setEditable(boolean editable) {
102: choiceList.setEditable(editable);
103: }
104:
105: public void addValue(String item) {
106: choiceList.addItem(item);
107: }
108:
109: public void setValues(String[] items) {
110: choiceList.removeAllItems();
111: for (int i = 0; i < items.length; i++) {
112: choiceList.addItem(items[i]);
113: }
114: }
115:
116: /**
117: * Initialises all of the components on this panel.
118: */
119: private void init() {
120: /*
121: * if(choiceList.isEditable()) { choiceList.addActionListener(new
122: * ComboListener()); }
123: */
124: choiceList.setBorder(BorderFactory.createLoweredBevelBorder());
125: // Register the handler for focus listening. This handler will
126: // only notify the registered when the text changes from when
127: // the focus is gained to when it is lost.
128: choiceList.addItemListener(new ItemListener() {
129: /**
130: * Callback method when the focus to the Text Field component is
131: * lost.
132: *
133: * @param e
134: * The focus event that occured.
135: */
136: public void itemStateChanged(ItemEvent e) {
137: if (e.getStateChange() == ItemEvent.SELECTED) {
138: notifyChangeListeners();
139: }
140: }
141: });
142:
143: // Add the sub components
144: this .add(mLabel);
145: this .add(choiceList);
146: if (choiceList.isEditable()) {
147: add = new JButton("Add");
148: add.setMargin(new Insets(1, 1, 1, 1));
149: add.addActionListener(new AddListener());
150: this .add(add);
151: delete = new JButton("Del");
152: delete.setMargin(new Insets(1, 1, 1, 1));
153: delete.addActionListener(new DeleteListener());
154: this .add(delete);
155: }
156:
157: }
158:
159: /**
160: * Set the text displayed in the label.
161: *
162: * @param pLabel
163: * The new label text.
164: */
165: public void setLabel(String pLabel) {
166: mLabel.setText(pLabel);
167: }
168:
169: /**
170: * Set the text displayed in the Text Field.
171: *
172: * @param pText
173: * The new text to display in the text field.
174: */
175: public void setText(String pText) {
176: choiceList.setSelectedItem(pText);
177: }
178:
179: public void setSelectedIndex(int index) {
180: choiceList.setSelectedIndex(index);
181: }
182:
183: /**
184: * Returns the text in the Text Field.
185: *
186: * @return The text in the Text Field. Never returns null.
187: */
188: public String getText() {
189: Object item = choiceList.getSelectedItem();
190: if (item == null) {
191: return "";
192: } else {
193: return (String) item;
194: }
195: }
196:
197: public int getSelectedIndex() {
198: return choiceList.getSelectedIndex();
199: }
200:
201: public Object[] getSelectedItems() {
202: Object list[] = choiceList.getSelectedObjects();
203: if (list == null) {
204: return EMPTY_OBJECT_ARRAY;
205: }
206: return list;
207: }
208:
209: public String[] getItems() {
210: String[] items = new String[choiceList.getItemCount()];
211: for (int i = 0; i < items.length; i++) {
212: items[i] = (String) choiceList.getItemAt(i);
213: }
214: return items;
215: }
216:
217: /**
218: * Returns the text of the label.
219: *
220: * @return The text of the label.
221: */
222: public String getLabel() {
223: return mLabel.getText();
224: }
225:
226: /**
227: * Adds a change listener, that will be notified when the text in the text
228: * field is changed. The ChangeEvent that will be passed to registered
229: * listeners will contain this object as the source, allowing the new text
230: * to be extracted using the {@link #getText() getText} method.
231: *
232: * @param pChangeListener
233: * The listener to add
234: */
235: public void addChangeListener(ChangeListener pChangeListener) {
236: mChangeListeners.add(pChangeListener);
237: }
238:
239: /**
240: * Removes a change listener.
241: *
242: * @param pChangeListener
243: * The change listener to remove.
244: */
245: public void removeChangeListener(ChangeListener pChangeListener) {
246: mChangeListeners.remove(pChangeListener);
247: }
248:
249: /**
250: * Notify all registered change listeners that the text in the text field
251: * has changed.
252: */
253: private void notifyChangeListeners() {
254: ChangeEvent ce = new ChangeEvent(this );
255: for (int index = 0; index < mChangeListeners.size(); index++) {
256: ((ChangeListener) mChangeListeners.get(index))
257: .stateChanged(ce);
258: }
259: }
260:
261: private class AddListener implements ActionListener {
262:
263: public void actionPerformed(ActionEvent e) {
264: Object item = choiceList.getSelectedItem();
265: int index = choiceList.getSelectedIndex();
266: if (!item.equals(choiceList.getItemAt(index))) {
267: choiceList.addItem(item);
268: }
269: choiceList.setSelectedItem(item);
270: notifyChangeListeners();
271: }
272: }
273:
274: private class DeleteListener implements ActionListener {
275:
276: public void actionPerformed(ActionEvent e) {
277: if (choiceList.getItemCount() > 1) {
278: choiceList.removeItemAt(choiceList.getSelectedIndex());
279: notifyChangeListeners();
280: }
281: }
282: }
283: }
|