KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SRadioButton


1 /*
2  * $Id: SRadioButton.java,v 1.9 2005/02/03 12:44:31 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16
17 import org.wings.plaf.RadioButtonCG;
18
19 /**
20  * <form>
21  * <b>Radiobuttons:</b>
22  * <p><input type="radio" name="1" value="1" checked>One</p>
23  * <p><input type="radio" name="1" value="2">Two</p>
24  * <p><input type="radio" name="1" value="3">Three</p>
25  * </form>
26  *
27  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
28  * @version $Revision: 1.9 $
29  */

30 public class SRadioButton
31         extends SAbstractButton {
32     public SRadioButton() {
33         setType(RADIOBUTTON);
34     }
35
36     public SRadioButton(String JavaDoc label) {
37         super(label, RADIOBUTTON);
38     }
39
40     public SRadioButton(boolean selected) {
41         this();
42         setSelected(selected);
43     }
44
45     public String JavaDoc getLowLevelEventId() {
46         if (getGroup() != null && getShowAsFormComponent()) {
47             return getGroup().getComponentId();
48         } else {
49             return super.getLowLevelEventId();
50         } // end of if ()
51
}
52
53     public void setType(String JavaDoc t) {
54         if (!RADIOBUTTON.equals(t))
55             throw new IllegalArgumentException JavaDoc("type change not supported, type is fix: radiobutton");
56
57         super.setType(t);
58     }
59
60     public void setCG(RadioButtonCG cg) {
61         super.setCG(cg);
62     }
63
64     public void processLowLevelEvent(String JavaDoc action, String JavaDoc[] values) {
65         processKeyEvents(values);
66
67         boolean origSelected = isSelected();
68
69         if (getShowAsFormComponent()) {
70             if (getGroup() == null) {
71                 // one hidden and one checked event from the form says select
72
// it, else deselect it (typically only the hidden event)
73
setSelected(values.length == 2);
74             } else {
75                 int eventCount = 0;
76                 for (int i = 0; i < values.length; i++) {
77                     // check the count of events, which are for me - with a
78
// button group, the value is my component id, if a event is
79
// for me
80
if (getName().equals(values[i])) {
81                         eventCount++;
82                     } // end of if ()
83
} // end of for (int i=0; i<; i++)
84
// one hidden and one checked event from the form says select
85
// it, else deselect it (typically only the hidden event)
86
setSelected(eventCount == 2);
87             } // end of if ()
88
} else {
89             if (getGroup() != null) {
90                 getGroup().setDelayEvents(true);
91                 setSelected(parseSelectionToggle(values[0]));
92                 getGroup().setDelayEvents(false);
93             } else {
94                 setSelected(parseSelectionToggle(values[0]));
95             } // end of else
96
}
97
98         if (isSelected() != origSelected) {
99             // got an event, that is a select...
100
SForm.addArmedComponent(this);
101         } // end of if ()
102
}
103
104     /**
105      * in form components the parameter value of an button is the button
106      * text. So just toggle selection, in process request, if it is a request
107      * for me.
108      */

109     protected boolean parseSelectionToggle(String JavaDoc toggleParameter) {
110         // a button/image in a form has no value, so just toggle selection...
111
if (getShowAsFormComponent()) {
112             return !isSelected();
113         } // end of if ()
114

115         if ("1".equals(toggleParameter))
116             return true;
117         else if ("0".equals(toggleParameter))
118             return false;
119
120
121         // don't change...
122
return isSelected();
123     }
124
125     public String JavaDoc getSelectionParameter() {
126         if (getGroup() != null && getShowAsFormComponent()) {
127             return getName();
128         } else {
129             return "1";
130         }
131     }
132
133     public String JavaDoc getDeselectionParameter() {
134         if (getGroup() != null && getShowAsFormComponent()) {
135             return getName();
136         } else {
137             return "0";
138         }
139     }
140 }
141
Popular Tags