001: /*
002: * GWT-Ext Widget Library
003: * Copyright(c) 2007-2008, GWT-Ext.
004: * licensing@gwt-ext.com
005: *
006: * http://www.gwt-ext.com/license
007: */
008:
009: package com.gwtext.client.widgets.form;
010:
011: import com.google.gwt.core.client.JavaScriptObject;
012: import com.gwtext.client.util.JavaScriptObjectHelper;
013: import com.gwtext.client.widgets.form.event.CheckboxListener;
014:
015: /**
016: * Single checkbox field.
017: */
018: public class Checkbox extends Field {
019:
020: private static JavaScriptObject configPrototype;
021:
022: static {
023: init();
024: }
025:
026: private static native void init()/*-{
027: var c = new $wnd.Ext.form.Checkbox();
028: var c = new $wnd.Ext.form.Checkbox();
029: @com.gwtext.client.widgets.form.Checkbox::configPrototype = c.initialConfig;
030: }-*/;
031:
032: protected JavaScriptObject getConfigPrototype() {
033: return configPrototype;
034: }
035:
036: public String getXType() {
037: return "checkbox";
038: }
039:
040: public Checkbox(JavaScriptObject jsObj) {
041: super (jsObj);
042: }
043:
044: /**
045: * Creates a new Checkbox field.
046: */
047: public Checkbox() {
048: }
049:
050: public Checkbox(String label) {
051: if (label != null)
052: setBoxLabel(label);
053: }
054:
055: public Checkbox(String fieldLabel, String name) {
056: setBoxLabel(fieldLabel);
057: setName(name);
058: }
059:
060: public Checkbox(String label, CheckboxListener listener) {
061: if (label != null)
062: setBoxLabel(label);
063: addListener(listener);
064: }
065:
066: protected native JavaScriptObject create(JavaScriptObject config)/*-{
067: return new $wnd.Ext.form.Checkbox(config);
068: }-*/;
069:
070: /**
071: * Returns the checked state of the checkbox.
072: *
073: * @return true if checked, else false
074: */
075: public native boolean getValue() /*-{
076: var cb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
077: return cb.getValue();
078: }-*/;
079:
080: /**
081: * Sets the checked state of the checkbox.
082: *
083: * @param checked true to chec the checkbox, false to uncheck it
084: */
085: public native void setValue(boolean checked) /*-{
086: var cb = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
087: cb.setValue(checked);
088: }-*/;
089:
090: /**
091: * Add a checkbox listener.
092: *
093: * @param listener the listener
094: */
095: public native void addListener(CheckboxListener listener) /*-{
096: this.@com.gwtext.client.widgets.form.Field::addListener(Lcom/gwtext/client/widgets/form/event/FieldListener;)(listener);
097: var fieldJ = this;
098:
099: this.@com.gwtext.client.widgets.Component::addListener(Ljava/lang/String;Lcom/google/gwt/core/client/JavaScriptObject;)('check',
100: function(fld, checked) {
101: listener.@com.gwtext.client.widgets.form.event.CheckboxListener::onCheck(Lcom/gwtext/client/widgets/form/Checkbox;Z)(fieldJ, checked);
102: }
103: );
104: }-*/;
105:
106: // --- config properties ---
107:
108: /**
109: * The text that appears beside the checkbox.
110: *
111: * @param boxLabel the box label
112: */
113: public void setBoxLabel(String boxLabel) {
114: setAttribute("boxLabel", boxLabel, true, true);
115: setLabelSeparator(" ");
116: if (isRendered()) {
117: setFieldLabelRendered(boxLabel, getId());
118: }
119: }
120:
121: private native boolean setFieldLabelRendered(String fieldLabel,
122: String fieldId) /*-{
123: var field = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
124: var label = $wnd.Ext.DomQuery.select($wnd.String.format('label[for="{0}"]', fieldId));
125: if (label){
126: //todo preserve user specified labelSeparator and only update the label text.
127: //var separator = typeof field.container.labelSeparator == 'undefined' ? field.labelSeparator : field.container.labelSeparator;
128: label[0].childNodes[0].nodeValue = fieldLabel;
129: }
130: }-*/;
131:
132: /**
133: * True if the the checkbox should render already checked (defaults to false).
134: *
135: * @param checked true to render checked
136: */
137: public void setChecked(boolean checked) {
138: if (!isRendered()) {
139: setAttribute("checked", checked, true);
140: } else {
141: setValue(checked);
142: }
143: }
144:
145: /**
146: * The value that should go into the generated input element's value attribute.
147: *
148: * @param inputValue fields input value
149: * @throws IllegalStateException this property cannot be changed after the Component has been rendered
150: */
151: public void setInputValue(String inputValue)
152: throws IllegalStateException {
153: setAttribute("inputValue", inputValue, true);
154: }
155: }
|