package org.waterforpeople.mapping.surveyentry.client.component;

import org.waterforpeople.mapping.app.gwt.client.survey.QuestionDto;
import org.waterforpeople.mapping.app.gwt.client.surveyinstance.QuestionAnswerStoreDto;

import com.gallatinsystems.framework.gwt.util.client.ViewUtil;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.TextBox;

/**
 * handles free text questions
 * 
 * @author Christopher Fagiani
 * 
 */
public class TextQuestionWidget extends QuestionWidget {
	private TextBox textBox;
	private boolean isNumeric;

	public TextQuestionWidget(QuestionDto q, QuestionAnswerStoreDto a,
			boolean numericOnly) {
		super(q, a);
		isNumeric = numericOnly;
		if (textBox != null && isNumeric) {
			// prevent non-numeric entry in the text box but allow navigational
			// keys
			textBox.addKeyPressHandler(new KeyPressHandler() {

				@Override
				public void onKeyPress(KeyPressEvent event) {
					char keyCode = event.getCharCode();
					if ((!Character.isDigit(keyCode))
							&& (keyCode != KeyCodes.KEY_TAB)
							&& (keyCode != KeyCodes.KEY_BACKSPACE)
							&& (keyCode != KeyCodes.KEY_DELETE)
							&& (keyCode != KeyCodes.KEY_ENTER)
							&& (keyCode != KeyCodes.KEY_HOME)
							&& (keyCode != KeyCodes.KEY_END)
							&& (keyCode != (KeyCodes.KEY_LEFT)
									&& (keyCode != KeyCodes.KEY_UP)
									&& (keyCode != KeyCodes.KEY_RIGHT) && (keyCode != KeyCodes.KEY_DOWN))) {
						((TextBox) event.getSource()).cancelKey();
					}
				}
			});
		}
	}

	@Override
	protected void constructResponseUi() {
		textBox = new TextBox();
		getPanel().add(textBox);
		if (getAnswer().getKeyId() != null) {
			textBox.setText(getAnswer().getValue());
		}
	}

	@Override
	protected void captureAnswer() {
		if (ViewUtil.isTextPopulated(textBox)) {
			getAnswer().setValue(textBox.getText().trim());
		}
	}

	@Override
	protected void resetUi() {
		textBox.setText("");
	}

}

@@ -694,16 +694,57 @@
 umeric;%0D
+%0A%09static final String MAX_WIDTH=%22300px%22;%0D
 %0A%0D%0A%09publ
@@ -1797,16 +1797,48 @@
 Box();%0D%0A
+%09%09textBox.setWidth(MAX_WIDTH);%0D%0A
 %09%09getPan
@@ -2052,29 +2052,33 @@
 %0A%09%09%09
-getAnswer().se
+String answerTex
 tValue
-(
+ = 
 text
@@ -2097,16 +2097,109 @@
 ).trim()
+;%0D%0A%09%09%09if(answerTextValue.contains(%22,%22))%7B%0D%0A%09%09%09%09%0D%0A%09%09%09%7D%0D%0A%09%09%09getAnswer().setValue(answerTextValue
 );%0D%0A%09%09%7D%0D

Set width of response text box to 300px