KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SCheckBox


1 /*
2  * $Id: SCheckBox.java,v 1.9 2005/05/26 13:18:08 neurolabs 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 import org.wings.plaf.CheckBoxCG;
17
18 import java.util.Arrays JavaDoc;
19
20 /*
21  * Checkboxen sind etwas besondere {@link SFormComponent}, denn
22  * ihre eigentliche Identitaet ({@link #getUID}) steckt nicht im Name,
23  * sondern im Value. Das ist deswegen so, weil in HTML Gruppen
24  * durch den selben Namen generiert werden. Das ist natuerlich
25  * problematisch. Eine anderes Problem, welches hier auftaucht ist,
26  * das HTML immer nur rueckmeldet, wenn eine Checkbox markiert ist.
27  * Deshalb wird hintenan immer ein Hidden Form Element gehaengt,
28  * welches rueckmeldet, dass die Checkbox bearbeitet wurde.
29  */

30
31 /**
32  * @author <a HREF="mailto:armin.haaf@mercatis.de">Armin Haaf</a>
33  * @version $Revision: 1.9 $
34  */

35 public class SCheckBox extends SAbstractButton {
36     public SCheckBox() {
37         this(false);
38     }
39
40     /**
41      * create a checkbox with a text-label.
42      */

43     public SCheckBox(String JavaDoc text) {
44         this(false);
45         setText(text);
46     }
47
48     public SCheckBox(boolean selected) {
49         setSelected(selected);
50
51         setHorizontalTextPosition(SConstants.NO_ALIGN);
52         setVerticalTextPosition(SConstants.NO_ALIGN);
53
54         super.setType(CHECKBOX);
55     }
56
57     protected void setGroup(SButtonGroup g) {
58         if (g != null) {
59             throw new IllegalArgumentException JavaDoc("SCheckBox don`t support button groups, use SRadioButton");
60         } // end of if ()
61
}
62
63     public final void setType(String JavaDoc t) {
64         if (!CHECKBOX.equals(t))
65             throw new IllegalArgumentException JavaDoc("type change not supported, type is fix: checkbox");
66
67         super.setType(t);
68     }
69
70     public void setCG(CheckBoxCG cg) {
71         super.setCG(cg);
72     }
73
74     public void processLowLevelEvent(String JavaDoc action, String JavaDoc[] values) {
75         processKeyEvents(values);
76
77         boolean requestSelection;
78         if (Arrays.asList(values).contains("hidden_reset")) {
79             // one hidden and one checked event from the form says select
80
// it, else deselect it (typically only the hidden event)
81
requestSelection = values.length == 2;
82         } else {
83             requestSelection = parseSelectionToggle(values[0]);
84         }
85
86         if (requestSelection != isSelected()) {
87             delayEvents(true);
88             setSelected(requestSelection);
89             // got an event, that is a select...
90
SForm.addArmedComponent(this);
91         } // end of if ()
92
}
93
94     /**
95      * in form components the parameter value of an button is the button
96      * text. So just toggle selection, in process request, if it is a request
97      * for me.
98      */

99     protected boolean parseSelectionToggle(String JavaDoc toggleParameter) {
100         // a button/image in a form has no value, so just toggle selection...
101
if (getShowAsFormComponent()) {
102             return !isSelected();
103         } // end of if ()
104

105         if ("1".equals(toggleParameter))
106             return true;
107         else if ("0".equals(toggleParameter))
108             return false;
109
110
111         // don't change...
112
return isSelected();
113     }
114
115 }
116
117
118
Popular Tags