001    /*
002     * Copyright 2006 Google Inc.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package com.google.gwt.user.client.ui;
017    
018    import com.google.gwt.user.client.DOM;
019    import com.google.gwt.user.client.Element;
020    
021    /**
022     * A standard check box widget (also serves as a base class for
023     * {@link com.google.gwt.user.client.ui.RadioButton}.
024     * 
025     * <p>
026     * <img class='gallery' src='CheckBox.png'/>
027     * </p>
028     * 
029     * <h3>CSS Style Rules</h3>
030     * <ul class='css'>
031     * <li>.gwt-CheckBox { }</li>
032     * </ul>
033     * 
034     * <h3>Example</h3>
035     * {@link com.google.gwt.examples.CheckBoxExample code}
036     */
037    public class CheckBox extends ButtonBase {
038    
039      private static int uniqueId;
040    
041      private Element inputElem, labelElem;
042    
043      /**
044       * Creates a check box with no label.
045       */
046      public CheckBox() {
047        this(DOM.createInputCheck());
048        setStyleName("gwt-CheckBox");
049      }
050    
051      /**
052       * Creates a check box with the specified text label.
053       * 
054       * @param label the check box's label
055       */
056      public CheckBox(String label) {
057        this();
058        setText(label);
059      }
060    
061      /**
062       * Creates a check box with the specified text label.
063       * 
064       * @param label the check box's label
065       * @param <code>true</code> to treat the specified label as html
066       */
067      public CheckBox(String label, boolean asHTML) {
068        this();
069        if (asHTML)
070          setHTML(label);
071        else
072          setText(label);
073      }
074    
075      protected CheckBox(Element elem) {
076        super(DOM.createSpan());
077        inputElem = elem;
078        labelElem = DOM.createLabel();
079    
080        DOM.appendChild(getElement(), inputElem);
081        DOM.appendChild(getElement(), labelElem);
082    
083        String uid = "check" + (++uniqueId);
084        DOM.setAttribute(inputElem, "id", uid);
085        DOM.setAttribute(labelElem, "htmlFor", uid);
086      }
087    
088      public String getHTML() {
089        return DOM.getInnerHTML(labelElem);
090      }
091    
092      public String getText() {
093        return DOM.getInnerText(labelElem);
094      }
095    
096      /**
097       * Determines whether this check box is currently checked.
098       * 
099       * @return <code>true</code> if the check box is checked
100       */
101      public boolean isChecked() {
102        String propName = isAttached() ? "checked" : "defaultChecked";
103    
104        // The 'checked' attribute can come back as 'true' or '-1'. Go figure.
105        String value = DOM.getAttribute(inputElem, propName);
106        return value.equals("true") || value.equals("-1");
107      }
108    
109      public boolean isEnabled() {
110        return !DOM.getAttribute(inputElem, "disabled").equals("true");
111      }
112    
113      /**
114       * Checks or unchecks this check box.
115       * 
116       * @param checked <code>true</code> to check the check box
117       */
118      public void setChecked(boolean checked) {
119        DOM.setAttribute(inputElem, "checked", checked ? "true" : "");
120        DOM.setAttribute(inputElem, "defaultChecked", checked ? "true" : "");
121      }
122    
123      public void setEnabled(boolean enabled) {
124        DOM.setAttribute(inputElem, "disabled", enabled ? "" : "true");
125      }
126    
127      public void setHTML(String html) {
128        DOM.setInnerHTML(labelElem, html);
129      }
130    
131      public void setText(String text) {
132        DOM.setInnerText(labelElem, text);
133      }
134    }