KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > nextapp > echo2 > app > RadioButton


1 /*
2  * This file is part of the Echo Web Application Framework (hereinafter "Echo").
3  * Copyright (C) 2002-2005 NextApp, Inc.
4  *
5  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6  *
7  * The contents of this file are subject to the Mozilla Public License Version
8  * 1.1 (the "License"); you may not use this file except in compliance with
9  * the License. You may obtain a copy of the License at
10  * http://www.mozilla.org/MPL/
11  *
12  * Software distributed under the License is distributed on an "AS IS" basis,
13  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14  * for the specific language governing rights and limitations under the
15  * License.
16  *
17  * Alternatively, the contents of this file may be used under the terms of
18  * either the GNU General Public License Version 2 or later (the "GPL"), or
19  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
20  * in which case the provisions of the GPL or the LGPL are applicable instead
21  * of those above. If you wish to allow use of your version of this file only
22  * under the terms of either the GPL or the LGPL, and not to allow others to
23  * use your version of this file under the terms of the MPL, indicate your
24  * decision by deleting the provisions above and replace them with the notice
25  * and other provisions required by the GPL or the LGPL. If you do not delete
26  * the provisions above, a recipient may use your version of this file under
27  * the terms of any one of the MPL, the GPL or the LGPL.
28  */

29
30 package nextapp.echo2.app;
31
32 import nextapp.echo2.app.button.ButtonGroup;
33 import nextapp.echo2.app.button.ButtonModel;
34 import nextapp.echo2.app.button.DefaultToggleButtonModel;
35 import nextapp.echo2.app.button.ToggleButton;
36 import nextapp.echo2.app.button.ToggleButtonModel;
37 import nextapp.echo2.app.event.ChangeEvent;
38 import nextapp.echo2.app.event.ChangeListener;
39
40 /**
41  * A radio button implementation.
42  */

43 public class RadioButton extends ToggleButton {
44     
45     public static final String JavaDoc BUTTON_GROUP_CHANGED_PROPERTY = "buttonGroup";
46     
47     /**
48      * Monitors state changes to enforce <code>ButtonGroup</code> behavior.
49      */

50     private ChangeListener changeMonitor = new ChangeListener() {
51
52         /**
53          * @see nextapp.echo2.app.event.ChangeListener#stateChanged(nextapp.echo2.app.event.ChangeEvent)
54          */

55         public void stateChanged(ChangeEvent e) {
56             if (buttonGroup != null) {
57                 buttonGroup.updateSelection(RadioButton.this);
58             }
59         }
60     };
61
62     private ButtonGroup buttonGroup;
63
64     /**
65      * Creates a radio button with no text or icon.
66      */

67     public RadioButton() {
68         this(null, null);
69     }
70     
71     /**
72      * Creates a radio button with text.
73      *
74      * @param text the text to be displayed in the radio button
75      */

76     public RadioButton(String JavaDoc text) {
77         this(text, null);
78     }
79     
80     /**
81      * Creates a radio button with an icon.
82      *
83      * @param icon the icon to be displayed in the radio button
84      */

85     public RadioButton(ImageReference icon) {
86         this(null, icon);
87     }
88
89     /**
90      * Creates a radio button with text and an icon.
91      *
92      * @param text the text to be displayed in the radio button
93      * @param icon the icon to be displayed in the radio button
94      */

95     public RadioButton(String JavaDoc text, ImageReference icon) {
96         super();
97         
98         setModel(new DefaultToggleButtonModel());
99     
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, newValue);
134     }
135     
136     /**
137      * @see nextapp.echo2.app.button.AbstractButton#setModel(nextapp.echo2.app.button.ButtonModel)
138      */

139     public void setModel(ButtonModel newValue) {
140         ButtonModel oldValue = getModel();
141         super.setModel(newValue);
142         if (oldValue != null) {
143             ((ToggleButtonModel) oldValue).removeChangeListener(changeMonitor);
144         }
145         ((ToggleButtonModel) newValue).addChangeListener(changeMonitor);
146         if (buttonGroup != null) {
147             buttonGroup.updateSelection(this);
148         }
149     }
150 }
151
Popular Tags