package fr.lteconsulting.hexa.client.ui.widget;

import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.event.logical.shared.AttachEvent;
import com.google.gwt.user.client.ui.TextBox;

public abstract class TextEditor extends GenericEditor<TextBox>
{
	protected abstract void onValidate( String newValue );

	public TextEditor( String text, boolean fShowCancel )
	{
		this( text, fShowCancel, true );
	}
	
	public TextEditor( String text, boolean fShowCancel, boolean fShowValidator )
	{
		super( new TextBox(), fShowCancel, fShowValidator );
		
		getEditorWidget().addAttachHandler( new AttachEvent.Handler()
		{
			@Override
			public void onAttachOrDetach( AttachEvent event )
			{
				getEditorWidget().selectAll();
			}
		} );
		
		getEditorWidget().addKeyUpHandler( new KeyUpHandler()
		{
			@Override
			public void onKeyUp( KeyUpEvent event )
			{
				if( event.getNativeKeyCode() == KeyCodes.KEY_ENTER )
					onValidate( getEditorWidget().getText() );
			}
		} );

		getEditorWidget().setText( text );
	}

	@Override
	protected final void onValidate( TextBox widget )
	{
		onValidate( widget.getText() );
	}

	protected void displayMessage( String text )
	{
		getEditorWidget().setText( text );
		getEditorWidget().setEnabled( false );
	}
}

@@ -133,18 +133,21 @@
 ient.Key
-Up
+Press
 Event;%0Ai
@@ -187,18 +187,21 @@
 ient.Key
-Up
+Press
 Handler;
@@ -880,18 +880,21 @@
 ).addKey
-Up
+Press
 Handler(
@@ -901,18 +901,21 @@
  new Key
-Up
+Press
 Handler(
@@ -957,17 +957,23 @@
 nKey
-Up( KeyUp
+Press( KeyPress
 Even
@@ -1010,16 +1010,27 @@
 etNative
+Event().get
 KeyCode(
@@ -1055,16 +1055,22 @@
 ENTER )%0A
+%09%09%09%09%7B%0A
 %09%09%09%09%09onV
@@ -1109,16 +1109,192 @@
 xt() );%0A
+%09%09%09%09%09event.stopPropagation();%0A%09%09%09%09%7D%0A%09%09%09%09else if( event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ESCAPE )%0A%09%09%09%09%7B%0A%09%09%09%09%09finishedEdition();%0A%09%09%09%09%09event.stopPropagation();%0A%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Add ESCAPE key to TextEditor