package com.inepex.ineForm.client.form.widgets;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.user.client.ui.ListBox;
import com.inepex.ineForm.client.i18n.IneFormI18n;
import com.inepex.ineom.shared.descriptor.fdesc.FDesc;

public class ThreeWayBoolFw extends DenyingFormWidget {
	
	public static final String TRUE = "true";
	public static final String FALSE = "false";
	public static final String NULL = "null";
	private final ListBox listBox = new ListBox(false);

	public ThreeWayBoolFw(FDesc fieldDescriptor, String nullText, String trueText, String falseText) {
		super(fieldDescriptor);
		if (nullText == null) nullText = IneFormI18n.notSetText();
		if (trueText == null) trueText = IneFormI18n.trueText();
		if (falseText == null) falseText = IneFormI18n.falseText();
				
		initWidget(listBox);
		listBox.addItem(nullText);
		listBox.addItem(trueText);
		listBox.addItem(falseText);
	}

	@Override
	public void setEnabled(boolean enabled) {
		listBox.setEnabled(enabled);
	}

	@Override
	public boolean handlesBoolean() {
		return true;
	}

	@Override
	public void setBooleanValue(Boolean value) {
		if(value==null) {
			listBox.setSelectedIndex(0);
		} else {
			if(value)
				listBox.setSelectedIndex(1);
			else 
				listBox.setSelectedIndex(2);
		}
	}
	
	@Override
	protected void onAttach() {
		super.onAttach();
		
		registerHandler(listBox.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				fireFormWidgetChanged();
			}
		}));
	}
	
	@Override
	public Boolean getBooleanValue() {
		switch(listBox.getSelectedIndex()) {
		case 0:
			return null;
		case 1:
			return true;
		case 2: 
			return false;
		}
		
		throw new RuntimeException("The seleceted item of ThreeWayBoolFw "+listBox.getSelectedIndex());
	}
}

@@ -163,39 +163,46 @@
 com.
-google.gwt.user.client.ui.
+inepex.ineForm.client.general.Ine
 List
-B
+b
 ox;%0A
@@ -497,16 +497,18 @@
 %22null%22;%0A
+//
 %09private
@@ -551,16 +551,70 @@
 (false);
+%0A%09private final IneListbox listBox = new IneListbox();
 %0A%0A%09publi
@@ -940,16 +940,27 @@
 (listBox
+.asWidget()
 );%0A%09%09lis
@@ -956,32 +956,45 @@
 t());%0A%09%09listBox.
+getListBox().
 addItem(nullText
@@ -998,32 +998,45 @@
 ext);%0A%09%09listBox.
+getListBox().
 addItem(trueText
@@ -1048,16 +1048,29 @@
 listBox.
+getListBox().
 addItem(
@@ -1149,16 +1149,29 @@
 listBox.
+getListBox().
 setEnabl
@@ -1332,32 +1332,45 @@
 l) %7B%0A%09%09%09listBox.
+getListBox().
 setSelectedIndex
@@ -1402,32 +1402,45 @@
 ue)%0A%09%09%09%09listBox.
+getListBox().
 setSelectedIndex
@@ -1465,16 +1465,29 @@
 listBox.
+getListBox().
 setSelec
@@ -1597,16 +1597,29 @@
 listBox.
+getListBox().
 addChang
@@ -1813,32 +1813,45 @@
 %09switch(listBox.
+getListBox().
 getSelectedIndex
@@ -2020,16 +2020,29 @@
 listBox.
+getListBox().
 getSelec

use IneListBox in ThreeWayBoolFw