001: /*
002: * This file is part of the Echo Web Application Framework (hereinafter "Echo").
003: * Copyright (C) 2002-2005 NextApp, Inc.
004: *
005: * Version: MPL 1.1/GPL 2.0/LGPL 2.1
006: *
007: * The contents of this file are subject to the Mozilla Public License Version
008: * 1.1 (the "License"); you may not use this file except in compliance with
009: * the License. You may obtain a copy of the License at
010: * http://www.mozilla.org/MPL/
011: *
012: * Software distributed under the License is distributed on an "AS IS" basis,
013: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
014: * for the specific language governing rights and limitations under the
015: * License.
016: *
017: * Alternatively, the contents of this file may be used under the terms of
018: * either the GNU General Public License Version 2 or later (the "GPL"), or
019: * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
020: * in which case the provisions of the GPL or the LGPL are applicable instead
021: * of those above. If you wish to allow use of your version of this file only
022: * under the terms of either the GPL or the LGPL, and not to allow others to
023: * use your version of this file under the terms of the MPL, indicate your
024: * decision by deleting the provisions above and replace them with the notice
025: * and other provisions required by the GPL or the LGPL. If you do not delete
026: * the provisions above, a recipient may use your version of this file under
027: * the terms of any one of the MPL, the GPL or the LGPL.
028: */
029:
030: package nextapp.echo2.app;
031:
032: import nextapp.echo2.app.button.ButtonGroup;
033: import nextapp.echo2.app.button.ButtonModel;
034: import nextapp.echo2.app.button.DefaultToggleButtonModel;
035: import nextapp.echo2.app.button.ToggleButton;
036: import nextapp.echo2.app.button.ToggleButtonModel;
037: import nextapp.echo2.app.event.ChangeEvent;
038: import nextapp.echo2.app.event.ChangeListener;
039:
040: /**
041: * A radio button implementation.
042: */
043: public class RadioButton extends ToggleButton {
044:
045: public static final String BUTTON_GROUP_CHANGED_PROPERTY = "buttonGroup";
046:
047: /**
048: * Monitors state changes to enforce <code>ButtonGroup</code> behavior.
049: */
050: private ChangeListener changeMonitor = new ChangeListener() {
051:
052: /**
053: * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
054: */
055: public void stateChanged(ChangeEvent e) {
056: if (buttonGroup != null) {
057: buttonGroup.updateSelection(RadioButton.this );
058: }
059: }
060: };
061:
062: private ButtonGroup buttonGroup;
063:
064: /**
065: * Creates a radio button with no text or icon.
066: */
067: public RadioButton() {
068: this (null, null);
069: }
070:
071: /**
072: * Creates a radio button with text.
073: *
074: * @param text the text to be displayed in the radio button
075: */
076: public RadioButton(String text) {
077: this (text, null);
078: }
079:
080: /**
081: * Creates a radio button with an icon.
082: *
083: * @param icon the icon to be displayed in the radio button
084: */
085: public RadioButton(ImageReference icon) {
086: this (null, icon);
087: }
088:
089: /**
090: * Creates a radio button with text and an icon.
091: *
092: * @param text the text to be displayed in the radio button
093: * @param icon the icon to be displayed in the radio button
094: */
095: public RadioButton(String text, ImageReference icon) {
096: super ();
097:
098: setModel(new DefaultToggleButtonModel());
099:
100: setIcon(icon);
101: setText(text);
102: }
103:
104: /**
105: * Retrieves the <code>ButtonGroup</code> to which this
106: * <code>RadioButton</code> belongs.
107: * Only one radio button in a group may be selected at a time.
108: *
109: * @return the button group
110: */
111: public ButtonGroup getGroup() {
112: return buttonGroup;
113: }
114:
115: /**
116: * Sets the <code>ButtonGroup</code> to which this
117: * <code>RadioButton</code> belongs.
118: * Only one radio button in a group may be selected at a time.
119: *
120: * @param newValue the new button group
121: */
122: public void setGroup(ButtonGroup newValue) {
123: ButtonGroup oldValue = buttonGroup;
124: buttonGroup = newValue;
125:
126: if (oldValue != null) {
127: oldValue.removeButton(this );
128: }
129: if (newValue != null) {
130: newValue.addButton(this );
131: }
132:
133: firePropertyChange(BUTTON_GROUP_CHANGED_PROPERTY, oldValue,
134: newValue);
135: }
136:
137: /**
138: * @see nextapp.echo2.app.button.AbstractButton#setModel(nextapp.echo2.app.button.ButtonModel)
139: */
140: public void setModel(ButtonModel newValue) {
141: ButtonModel oldValue = getModel();
142: super .setModel(newValue);
143: if (oldValue != null) {
144: ((ToggleButtonModel) oldValue)
145: .removeChangeListener(changeMonitor);
146: }
147: ((ToggleButtonModel) newValue).addChangeListener(changeMonitor);
148: if (buttonGroup != null) {
149: buttonGroup.updateSelection(this);
150: }
151: }
152: }
|