001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.plaf.RadioButtonCG;
016:
017: /**
018: * Can be selected or deselected, and displays that state to the user.
019: *
020: * <p/>
021: * Example:
022: * <form>
023: * <b>Radiobuttons:</b>
024: * <p><input type="radio" name="1" value="1" checked>One</p>
025: * <p><input type="radio" name="1" value="2">Two</p>
026: * <p><input type="radio" name="1" value="3">Three</p>
027: * </form>
028: *
029: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
030: */
031: public class SRadioButton extends SAbstractButton {
032:
033: /**
034: * Creates an unselected radio button.
035: *
036: */
037: public SRadioButton() {
038: setType(RADIOBUTTON);
039: }
040:
041: /**
042: * Creates a unselected radio button with an initial text.
043: *
044: * @param label Text to display
045: */
046: public SRadioButton(String label) {
047: super (label, RADIOBUTTON);
048: }
049:
050: /**
051: * Creates a radio button with a certain state.
052: *
053: * @param selected Whether the radio button is initially selected or not.
054: */
055: public SRadioButton(boolean selected) {
056: this ();
057: setSelected(selected);
058: }
059:
060: /**
061: * Creates a radio button with a text-label and a state.
062: *
063: * @param label Text to display
064: * @param selected Whether the radio button is initially selected or not.
065: */
066: public SRadioButton(String label, boolean selected) {
067: this (selected);
068: setText(label);
069: }
070:
071: public String getLowLevelEventId() {
072: if (getGroup() != null && getShowAsFormComponent()) {
073: return getGroup().getComponentId();
074: } else {
075: return super .getLowLevelEventId();
076: } // end of if ()
077: }
078:
079: /**
080: * You cannot change type of radio button. <p>
081: * Accepts only {@link SAbstractButton#RADIOBUTTON}
082: */
083: public void setType(String t) {
084: if (!RADIOBUTTON.equals(t))
085: throw new IllegalArgumentException(
086: "type change not supported, type is fix: radiobutton");
087:
088: super .setType(t);
089: }
090:
091: public void setCG(RadioButtonCG cg) {
092: super .setCG(cg);
093: }
094:
095: public void processLowLevelEvent(String action, String[] values) {
096: processKeyEvents(values);
097: if (action.endsWith("_keystroke"))
098: return;
099:
100: delayEvents(true);
101:
102: boolean origSelected = isSelected();
103:
104: if (getShowAsFormComponent()) {
105: if (values.length == 1) {
106: // this happens if we've got a form component but
107: // the CG uses icons since useIconsInForm is true
108: if (getName().equals(values[0]) && !isSelected()) {
109: setSelected(true);
110: }
111: } else if (getGroup() == null) {
112: // one hidden and one checked event from the form says select
113: // it, else deselect it (typically only the hidden event)
114: setSelected(values.length == 2);
115: } else {
116: int eventCount = 0;
117: for (int i = 0; i < values.length; i++) {
118: // check the count of events, which are for me - with a
119: // button group, the value is my component id, if a event is
120: // for me
121: if (getName().equals(values[i])) {
122: eventCount++;
123: } // end of if ()
124: } // end of for (int i=0; i<; i++)
125: // one hidden and one checked event from the form says select
126: // it, else deselect it (typically only the hidden event)
127: setSelected(eventCount == 2);
128: } // end of if ()
129: } else {
130: if (getGroup() != null) {
131: getGroup().setDelayEvents(true);
132: setSelected(parseSelectionToggle(values[0]));
133: getGroup().setDelayEvents(false);
134: } else {
135: setSelected(parseSelectionToggle(values[0]));
136: } // end of else
137: }
138:
139: if (isSelected() != origSelected) {
140: // got an event, that is a select...
141: SForm.addArmedComponent(this );
142: } // end of if ()
143:
144: delayEvents(false);
145: }
146:
147: protected boolean parseSelectionToggle(String toggleParameter) {
148: if ("1".equals(toggleParameter) && !isSelected())
149: return true;
150: else
151: return isSelected();
152: }
153:
154: public String getSelectionParameter() {
155: if (getGroup() != null && getShowAsFormComponent()) {
156: return getName();
157: } else {
158: return "1";
159: }
160: }
161:
162: public String getDeselectionParameter() {
163: if (getGroup() != null && getShowAsFormComponent()) {
164: return getName();
165: } else {
166: return "0";
167: }
168: }
169: }
|