package com.sensia.tools.client.swetools.editors.sensorml.panels.base;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.sensia.relaxNG.RNGData;
import com.sensia.relaxNG.RNGTag;
import com.sensia.relaxNG.RNGValue;
import com.sensia.tools.client.swetools.editors.sensorml.panels.AbstractPanel;
import com.sensia.tools.client.swetools.editors.sensorml.panels.IPanel;
import com.sensia.tools.client.swetools.editors.sensorml.utils.Utils;

public class DataValuePanel extends AbstractPanel<RNGData<?>>{
	protected static final int DEFAULT_TEXBOX_VALUE_SIZE = 20;
	
	protected TextBox textBox;

	protected HTML htmlTextBox;

	private String niceLabel;
	
	private boolean isNiceLabel;
	
	private MODE currentMode;
	
	public DataValuePanel(final RNGData<?> data) {
		super(data);
		currentMode = MODE.VIEW;
		isNiceLabel = true;
		
		htmlTextBox = new HTML();
		textBox = new TextBox();
		
		// put saved value in text box
		if (data.getStringValue() != null) {
			if(isNiceLabel) {
				niceLabel = Utils.toNiceLabel(data.getStringValue().trim());
			} else {
				niceLabel = data.getStringValue().trim();
			}
			
			textBox.setText(data.getStringValue().trim());
			htmlTextBox.setText(niceLabel);
			
			int valueLength = data.getStringValue().trim().length();
			if(valueLength > 0) {
				textBox.setVisibleLength(data.getStringValue().trim().length());
			} else {
				textBox.setVisibleLength(DEFAULT_TEXBOX_VALUE_SIZE);
			}
			
			textBox.addChangeHandler(new ChangeHandler() {
				
				@Override
				public void onChange(ChangeEvent event) {
					data.setStringValue(textBox.getText());
				}
			});
		}

		textBox.addKeyDownHandler(new KeyDownHandler() {
			
			@Override
			public void onKeyDown(KeyDownEvent event) {
				data.setStringValue(textBox.getText());
			}
		});
		// add into the main container
		container.add(htmlTextBox);
	}

	@Override
	public void addInnerElement(IPanel<? extends RNGTag> element) {
		// Do nothing
	}

	@Override
	protected AbstractPanel<RNGData<?>> newInstance() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected void activeMode(MODE mode) {
		currentMode = mode;
		refreshTextFields();
	}

	@Override
	public String getName() {
		return "value";
	}
	
	public void setNiceLabel(boolean isNiceLabel) {
		this.isNiceLabel = isNiceLabel;
		refreshTextFields();
	}
	
	private void refreshTextFields() {
		container.clear();
		
		// update html value with edited value
		if(isNiceLabel) {
			niceLabel = Utils.toNiceLabel(getTag().getStringValue());
		} else {
			niceLabel = getTag().getStringValue();
		}
		
		htmlTextBox.setText(niceLabel);
		if(currentMode == MODE.EDIT) {
			container.add(textBox);
		} else if (currentMode == MODE.VIEW) {
			container.add(htmlTextBox);
		}
	}
}

@@ -65,16 +65,55 @@
 .base;%0A%0A
+import com.google.gwt.core.shared.GWT;%0A
 import c
@@ -310,32 +310,246 @@
 KeyDownHandler;%0A
+import com.google.gwt.event.dom.client.KeyPressEvent;%0Aimport com.google.gwt.event.dom.client.KeyPressHandler;%0Aimport com.google.gwt.event.dom.client.KeyUpEvent;%0Aimport com.google.gwt.event.dom.client.KeyUpHandler;%0A
 import com.googl
@@ -1936,32 +1936,34 @@
 E);%0A%09%09%09%7D%0A%09%09%09%0A%09%09%09
+/*
 textBox.addChang
@@ -2115,16 +2115,18 @@
 %7D%0A%09%09%09%7D);
+*/
 %0A%09%09%7D%0A%0A%09%09
@@ -2139,20 +2139,18 @@
 x.addKey
-Down
+Up
 Handler(
@@ -2156,20 +2156,18 @@
 (new Key
-Down
+Up
 Handler(
@@ -2170,19 +2170,16 @@
 ler() %7B%0A
-%09%09%09
 %0A%09%09%09@Ove
@@ -2208,20 +2208,16 @@
 nKey
-Down(KeyDown
+Up(KeyUp
 Even
@@ -2223,24 +2223,56 @@
 nt event) %7B%0A
+%09%09%09%09GWT.log(textBox.getText());%0A
 %09%09%09%09data.set

Save data to RNG model after typing