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.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025: import java.util.LinkedList;
026: import java.util.List;
027:
028: import javax.swing.ButtonGroup;
029: import javax.swing.JComponent;
030: import javax.swing.JLabel;
031: import javax.swing.JPanel;
032: import javax.swing.JRadioButton;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035:
036: /**
037: * @author pete
038: *
039: * JLabeledRadio will create a set of Radio buttons with a label.
040: */
041: public class JLabeledRadio extends JPanel implements JLabeledField,
042: ActionListener {
043:
044: private JLabel mLabel = new JLabel();
045:
046: private ButtonGroup bGroup = new ButtonGroup();
047:
048: private ArrayList mChangeListeners = new ArrayList(3);
049:
050: /**
051: *
052: */
053: public JLabeledRadio() {
054: super ();
055: this .add(mLabel);
056: }
057:
058: public JLabeledRadio(String label, String[] items) {
059: mLabel.setText(label);
060: init(items, null);
061: }
062:
063: public JLabeledRadio(String label, String[] items,
064: String selectedItem) {
065: mLabel.setText(label);
066: init(items, selectedItem);
067: }
068:
069: /**
070: * Method is responsible for creating the JRadioButtons and adding them to
071: * the ButtonGroup.
072: *
073: * @param items
074: */
075: private void init(String[] items, String selected) {
076: this .add(mLabel);
077: for (int idx = 0; idx < items.length; idx++) {
078: JRadioButton btn = new JRadioButton(items[idx]);
079: btn.setActionCommand(items[idx]);
080: btn.addActionListener(this );
081: // add the button to the button group
082: this .bGroup.add(btn);
083: // add the button
084: this .add(btn);
085: if (selected != null && selected.equals(items[idx])) {
086: btn.setSelected(true);
087: }
088: }
089: }
090:
091: /**
092: * setItems will set the radio button items. The implementation first
093: * removes the old JRadioButton, then it creates new ones.
094: *
095: * @param items
096: */
097: public void setItems(String[] items) {
098: Enumeration en = this .bGroup.getElements();
099: while (en.hasMoreElements()) {
100: JComponent comp = (JComponent) en.nextElement();
101: this .bGroup.remove((JRadioButton) comp);
102: this .remove(comp);
103: }
104: init(items, null);
105: }
106:
107: /**
108: * The implementation will get the Text value from the selected radio button
109: * in the JButtonGroup.
110: */
111: public String getText() {
112: return this .bGroup.getSelection().getActionCommand();
113: }
114:
115: /**
116: * The implementation will iterate through the radio buttons and find the
117: * match. It then sets it to selected and sets all other radion buttons as
118: * not selected.
119: */
120: public void setText(String text) {
121: Enumeration en = this .bGroup.getElements();
122: while (en.hasMoreElements()) {
123: JRadioButton jrb = (JRadioButton) en.nextElement();
124: if (jrb.getText().equals(text)) {
125: this .bGroup.setSelected(jrb.getModel(), true);
126: } else {
127: this .bGroup.setSelected(jrb.getModel(), false);
128: }
129: }
130: }
131:
132: /*
133: * (non-Javadoc)
134: *
135: * @see org.apache.jorphan.gui.JLabeledField#setLabel(java.lang.String)
136: */
137: public void setLabel(String pLabel) {
138: this .mLabel.setText(pLabel);
139: }
140:
141: /*
142: * (non-Javadoc)
143: *
144: * @see org.apache.jorphan.gui.JLabeledField#addChangeListener(javax.swing.event.ChangeListener)
145: */
146: public void addChangeListener(ChangeListener pChangeListener) {
147: this .mChangeListeners.add(pChangeListener);
148: }
149:
150: /**
151: * Notify all registered change listeners that the text in the text field
152: * has changed.
153: */
154: private void notifyChangeListeners() {
155: ChangeEvent ce = new ChangeEvent(this );
156: for (int index = 0; index < mChangeListeners.size(); index++) {
157: ((ChangeListener) mChangeListeners.get(index))
158: .stateChanged(ce);
159: }
160: }
161:
162: /**
163: * Method will return all the label and JRadioButtons. ButtonGroup is
164: * excluded from the list.
165: */
166: public List getComponentList() {
167: List comps = new LinkedList();
168: comps.add(mLabel);
169: Enumeration en = this .bGroup.getElements();
170: while (en.hasMoreElements()) {
171: comps.add(en.nextElement());
172: }
173: return comps;
174: }
175:
176: /**
177: * When a radio button is clicked, an ActionEvent is triggered.
178: */
179: public void actionPerformed(ActionEvent e) {
180: this.notifyChangeListeners();
181: }
182:
183: }
|