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.CheckBoxCG;
016:
017: import java.util.Arrays;
018:
019: /*
020: * Checkboxen sind etwas besondere {@link SFormComponent}, denn
021: * ihre eigentliche Identitaet ({@link #getUID}) steckt nicht im Name,
022: * sondern im Value. Das ist deswegen so, weil in HTML Gruppen
023: * durch den selben Namen generiert werden. Das ist natuerlich
024: * problematisch. Eine anderes Problem, welches hier auftaucht ist,
025: * das HTML immer nur rueckmeldet, wenn eine Checkbox markiert ist.
026: * Deshalb wird hintenan immer ein Hidden Form Element gehaengt,
027: * welches rueckmeldet, dass die Checkbox bearbeitet wurde.
028: */
029:
030: /**
031: * Checkbox widget.
032: * <p/>
033: * May be displayed as a HTML form element or as a clickable icon.
034: *
035: * @author <a href="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
036: */
037: public class SCheckBox extends SAbstractButton {
038:
039: /**
040: * Creates an initially unselected checkbox.
041: */
042: public SCheckBox() {
043: this (false);
044: }
045:
046: /**
047: * Creates an initially unselected checkbox with a text-label.
048: *
049: * @param label Text to display
050: */
051: public SCheckBox(String label) {
052: this (false);
053: setText(label);
054: }
055:
056: /**
057: * Creates a checkbox with a certain state.
058: *
059: * @param selected Whether the checkbox is initially selected or not.
060: */
061: public SCheckBox(boolean selected) {
062: setSelected(selected);
063:
064: setHorizontalTextPosition(SConstants.NO_ALIGN);
065: setVerticalTextPosition(SConstants.NO_ALIGN);
066:
067: super .setType(CHECKBOX);
068: }
069:
070: /**
071: * Creates a checkbox with a text-label and a state.
072: *
073: * @param label Text to display
074: * @param selected Whether the checkbox is initially selected or not.
075: */
076: public SCheckBox(String label, boolean selected) {
077: this (selected);
078: setText(label);
079: }
080:
081: protected void setGroup(SButtonGroup g) {
082: if (g != null) {
083: throw new IllegalArgumentException(
084: "SCheckBox don`t support button groups, use SRadioButton");
085: } // end of if ()
086: }
087:
088: /**
089: * You cannot change type of checkbox. <p>
090: * Accepts only {@link SAbstractButton#CHECKBOX}
091: */
092: public final void setType(String t) {
093: if (!CHECKBOX.equals(t))
094: throw new IllegalArgumentException(
095: "type change not supported, type is fix: checkbox");
096:
097: super .setType(t);
098: }
099:
100: public void setCG(CheckBoxCG cg) {
101: super .setCG(cg);
102: }
103:
104: public void processLowLevelEvent(String action, String[] values) {
105: processKeyEvents(values);
106: if (action.endsWith("_keystroke"))
107: return;
108:
109: delayEvents(true);
110:
111: boolean requestSelection;
112: if (Arrays.asList(values).contains("hidden_reset")) {
113: // one hidden and one checked event from the form says select
114: // it, else deselect it (typically only the hidden event)
115: requestSelection = values.length == 2;
116: } else {
117: requestSelection = parseSelectionToggle(values[0]);
118: }
119:
120: if (requestSelection != isSelected()) {
121: setSelected(requestSelection);
122: // got an event, that is a select...
123: SForm.addArmedComponent(this );
124: } // end of if ()
125:
126: delayEvents(false);
127: }
128:
129: /**
130: * in form components the parameter value of an button is the button
131: * text. So just toggle selection, in process request, if it is a request
132: * for me.
133: */
134: protected boolean parseSelectionToggle(String toggleParameter) {
135: // a button/image in a form has no value, so just toggle selection...
136: if (getShowAsFormComponent()) {
137: return !isSelected();
138: } // end of if ()
139:
140: if ("1".equals(toggleParameter))
141: return true;
142: else if ("0".equals(toggleParameter))
143: return false;
144:
145: // don't change...
146: return isSelected();
147: }
148:
149: }
|