package com.sksamuel.jqm4gwt.form.elements;

import com.google.gwt.dom.client.InputElement;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.sksamuel.jqm4gwt.HasMini;
import com.sksamuel.jqm4gwt.html.FormLabel;

/**
 * @author Stephen K Samuel samspade79@gmail.com 12 Jul 2011 15:42:39
 * 
 * 
 */
public class JQMCheckbox implements HasText, IsChecked, HasValue<Boolean>, HasMini {

	private final InputElement	input;

	private final FormLabel		label;

	private final String		id;

	JQMCheckbox(InputElement input, FormLabel label, String id) {
		this.input = input;
		this.label = label;
		this.id = id;
	}

	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Boolean> handler) {
		return null;
	}

	@Override
	public void fireEvent(GwtEvent<?> event) {
	}

	public String getId() {
		return id;
	}

	public InputElement getInput() {
		return input;
	}

	@Override
	public String getText() {
		return label.getText();
	}

	@Override
	public Boolean getValue() {
		return isSelected();
	}

	@Override
	public boolean isMini() {
		return "true".equals(input.getAttribute("data-mini"));
	}

	@Override
	public boolean isSelected() {
		String style = label.getStyleName();
		if (style == null)
			return false;
		if (style.contains("ui-btn-down")) {
			return !style.contains("ui-checkbox-on");
		} else {
			return style.contains("ui-checkbox-on");
		}

	}

	/**
	 * If set to true then renders a smaller version of the standard-sized element.
	 */
	@Override
	public void setMini(boolean mini) {
		input.setAttribute("data-mini", String.valueOf(mini));
	}

	@Override
	public void setText(String text) {
		label.setText(text);
	}

	@Override
	public void setValue(Boolean value) {
		setValue(value, false);
	}

	@Override
	public void setValue(Boolean value, boolean ignored) {
		input.setChecked(value);
		input.setDefaultChecked(value);
	}
}

@@ -84,24 +84,130 @@
 utElement;%0D%0A
+import com.google.gwt.event.dom.client.ClickEvent;%0D%0Aimport com.google.gwt.event.dom.client.ClickHandler;%0D%0A
 import com.g
@@ -250,32 +250,32 @@
 ChangeHandler;%0D%0A
-
 import com.googl
@@ -824,16 +824,49 @@
 %09id;%0D%0A%0D%0A
+%09private boolean isSelected;%0D%0A%09%0D%0A
 %09JQMChec
@@ -981,25 +981,206 @@
 .id = id;%0D%0A%09
-%7D
+%09%0D%0A%09%09label.addDomHandler(new ClickHandler()%0D%0A%09%09%7B%0D%0A%09%09%09@Override%0D%0A%09%09%09public void onClick(ClickEvent arg0)%0D%0A%09%09%09%7B%0D%0A%09%09%09%09isSelected = !isSelected;%0D%0A%09%09%09%7D%0D%0A%09%09%7D, ClickEvent.getType());%09%09%0D%0A%09%7D;
 %0D%0A%0D%0A%09@Overri
@@ -1761,234 +1761,26 @@
 %0D%0A%09%09
-String style = label.getStyleName();%0D%0A%09%09if (style == null)%0D%0A%09%09%09return false;%0D%0A%09%09if (style.contains(%22ui-btn-down%22)) %7B%0D%0A%09%09%09return !style.contains(%22ui-checkbox-on%22);%0D%0A%09%09%7D else %7B%0D%0A%09%09%09return style.contains(%22ui-checkbox-on%22);%0D%0A%09%09%7D%0D%0A
+return isSelected;
 %0D%0A%09%7D
@@ -2252,16 +2252,16 @@
 alue);%0D%0A
-
 %09%09input.
@@ -2283,19 +2283,42 @@
 ed(value);%0D%0A
+%09%09isSelected = value;%0D%0A
 %09%7D%0D%0A%7D%0D%0A

use a flag for selection status, instead of relying on the UI of the control, in this way, the selection status is updated at the right moment, when the control is clicked