KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > user > client > ui > CheckBox


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.user.client.ui;
17
18 import com.google.gwt.user.client.DOM;
19 import com.google.gwt.user.client.Element;
20
21 /**
22  * A standard check box widget (also serves as a base class for
23  * {@link com.google.gwt.user.client.ui.RadioButton}.
24  * <p>
25  * <img class='gallery' SRC='CheckBox.png'/>
26  * </p>
27  * <h3>CSS Style Rules</h3>
28  * <ul class='css'>
29  * <li>.gwt-CheckBox { }</li>
30  * </ul>
31  * <p>
32  * <h3>Example</h3>
33  * {@example com.google.gwt.examples.CheckBoxExample}
34  * </p>
35  */

36 public class CheckBox extends ButtonBase implements HasName {
37   private static int uniqueId;
38   private Element inputElem, labelElem;
39
40   /**
41    * Creates a check box with no label.
42    */

43   public CheckBox() {
44     this(DOM.createInputCheck());
45     setStyleName("gwt-CheckBox");
46   }
47
48   /**
49    * Creates a check box with the specified text label.
50    *
51    * @param label the check box's label
52    */

53   public CheckBox(String JavaDoc label) {
54     this();
55     setText(label);
56   }
57
58   /**
59    * Creates a check box with the specified text label.
60    *
61    * @param label the check box's label
62    * @param asHTML <code>true</code> to treat the specified label as html
63    */

64   public CheckBox(String JavaDoc label, boolean asHTML) {
65     this();
66     if (asHTML) {
67       setHTML(label);
68     } else {
69       setText(label);
70     }
71   }
72
73   protected CheckBox(Element elem) {
74     super(DOM.createSpan());
75     inputElem = elem;
76     labelElem = DOM.createLabel();
77
78     // Hook events to input widget rather than the check box element.
79
DOM.sinkEvents(inputElem, DOM.getEventsSunk(this.getElement()));
80     DOM.sinkEvents(this.getElement(), 0);
81     DOM.appendChild(getElement(), inputElem);
82     DOM.appendChild(getElement(), labelElem);
83
84     String JavaDoc uid = "check" + (++uniqueId);
85     DOM.setElementProperty(inputElem, "id", uid);
86     DOM.setElementProperty(labelElem, "htmlFor", uid);
87   }
88
89   public String JavaDoc getHTML() {
90     return DOM.getInnerHTML(labelElem);
91   }
92
93   public String JavaDoc getName() {
94     return DOM.getElementProperty(inputElem, "name");
95   }
96
97   public int getTabIndex() {
98     return getFocusImpl().getTabIndex(inputElem);
99   }
100
101   public String JavaDoc getText() {
102     return DOM.getInnerText(labelElem);
103   }
104
105   /**
106    * Determines whether this check box is currently checked.
107    *
108    * @return <code>true</code> if the check box is checked
109    */

110   public boolean isChecked() {
111     String JavaDoc propName = isAttached() ? "checked" : "defaultChecked";
112     return DOM.getElementPropertyBoolean(inputElem, propName);
113   }
114
115   public boolean isEnabled() {
116     return !DOM.getElementPropertyBoolean(inputElem, "disabled");
117   }
118
119   public void setAccessKey(char key) {
120     DOM.setElementProperty(inputElem, "accessKey", "" + key);
121   }
122
123   /**
124    * Checks or unchecks this check box.
125    *
126    * @param checked <code>true</code> to check the check box
127    */

128   public void setChecked(boolean checked) {
129     DOM.setElementPropertyBoolean(inputElem, "checked", checked);
130     DOM.setElementPropertyBoolean(inputElem, "defaultChecked", checked);
131   }
132
133   public void setEnabled(boolean enabled) {
134     DOM.setElementPropertyBoolean(inputElem, "disabled", !enabled);
135   }
136
137   public void setFocus(boolean focused) {
138     if (focused) {
139       getFocusImpl().focus(inputElem);
140     } else {
141       getFocusImpl().blur(inputElem);
142     }
143   }
144
145   public void setHTML(String JavaDoc html) {
146     DOM.setInnerHTML(labelElem, html);
147   }
148
149   public void setName(String JavaDoc name) {
150     DOM.setElementProperty(inputElem, "name", name);
151   }
152
153   public void setTabIndex(int index) {
154     getFocusImpl().setTabIndex(inputElem, index);
155   }
156
157   public void setText(String JavaDoc text) {
158     DOM.setInnerText(labelElem, text);
159   }
160
161   /**
162    * This method is called when a widget is attached to the browser's document.
163    * onAttach needs special handling for the CheckBox case. Must still call
164    * {@link Widget#onAttach()} to preserve the <code>onAttach</code> contract.
165    */

166   protected void onAttach() {
167     // Sets the event listener on the inputElem, as in this case that's the
168
// element we want so input on.
169
DOM.setEventListener(inputElem, this);
170     super.onAttach();
171   }
172
173   /**
174    * This method is called when a widget is detached from the browser's
175    * document. Overridden because of IE bug that throws away checked state and
176    * in order to clear the event listener off of the <code>inputElem</code>.
177    */

178   protected void onDetach() {
179     // Clear out the inputElem's event listener (breaking the circular
180
// reference between it and the widget).
181
DOM.setEventListener(inputElem, null);
182     setChecked(isChecked());
183     super.onDetach();
184   }
185 }
186
Popular Tags