1 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 36 public class CheckBox extends ButtonBase implements HasName { 37 private static int uniqueId; 38 private Element inputElem, labelElem; 39 40 43 public CheckBox() { 44 this(DOM.createInputCheck()); 45 setStyleName("gwt-CheckBox"); 46 } 47 48 53 public CheckBox(String label) { 54 this(); 55 setText(label); 56 } 57 58 64 public CheckBox(String 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 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 uid = "check" + (++uniqueId); 85 DOM.setElementProperty(inputElem, "id", uid); 86 DOM.setElementProperty(labelElem, "htmlFor", uid); 87 } 88 89 public String getHTML() { 90 return DOM.getInnerHTML(labelElem); 91 } 92 93 public String getName() { 94 return DOM.getElementProperty(inputElem, "name"); 95 } 96 97 public int getTabIndex() { 98 return getFocusImpl().getTabIndex(inputElem); 99 } 100 101 public String getText() { 102 return DOM.getInnerText(labelElem); 103 } 104 105 110 public boolean isChecked() { 111 String 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 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 html) { 146 DOM.setInnerHTML(labelElem, html); 147 } 148 149 public void setName(String 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 text) { 158 DOM.setInnerText(labelElem, text); 159 } 160 161 166 protected void onAttach() { 167 DOM.setEventListener(inputElem, this); 170 super.onAttach(); 171 } 172 173 178 protected void onDetach() { 179 DOM.setEventListener(inputElem, null); 182 setChecked(isChecked()); 183 super.onDetach(); 184 } 185 } 186 | Popular Tags |