//
//  StringPropertyPart.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 20 Jul 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.client.part.property;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.willshex.blogwt.client.helper.UiHelper;

/**
 * @author William Shakour (billy1380)
 *
 */
public class StringPropertyPart extends AbstractPropertyPart {

	@UiField Element elDescription;
	@UiField Element elName;
	@UiField TextBox txtValue;
	@UiField HTMLPanel pnlValueNote;

	private static StringPropertyPartUiBinder uiBinder = GWT
			.create(StringPropertyPartUiBinder.class);

	interface StringPropertyPartUiBinder extends
			UiBinder<Widget, StringPropertyPart> {}

	public StringPropertyPart () {
		initWidget(uiBinder.createAndBindUi(this));
	}

	/**
	 * @param description
	 */
	public void setDescription (String description) {
		elDescription.setInnerText(description);
		UiHelper.addPlaceholder(txtValue, description);
	}

	/**
	 */
	public void setAutofocus () {
		UiHelper.autoFocus(txtValue);
	}

	@UiHandler("txtValue")
	void onTextValueChanged (ValueChangeEvent<String> vce) {
		setValue(vce.getValue(), true);
	}

	/* (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object,
	 * boolean) */
	@Override
	public void setValue (String value, boolean fireEvents) {
		if (value == null) {
			value = "";
		}

		String oldValue = getValue();

		txtValue.setValue(value);
		this.value = value;

		if (value.equals(oldValue)) { return; }
		if (fireEvents) {
			ValueChangeEvent.fire(this, value);
		}
	}

	/* (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.HasName#getName() */
	@Override
	public String getName () {
		return elName.getInnerText();
	}

	/* (non-Javadoc)
	 * 
	 * @see com.google.gwt.user.client.ui.HasName#setName(java.lang.String) */
	@Override
	public void setName (String name) {
		elName.setInnerText(name);
	}

}

@@ -493,24 +493,72 @@
 .UiHandler;%0A
+import com.google.gwt.user.client.ui.Focusable;%0A
 import com.g
@@ -847,16 +847,39 @@
 rtyPart 
+implements%0A%09%09Focusable 
 %7B%0A%0A%09@UiF
@@ -2408,10 +2408,711 @@
 e);%0A%09%7D%0A%0A
+%09/* (non-Javadoc)%0A%09 * %0A%09 * @see com.google.gwt.user.client.ui.Focusable#getTabIndex() */%0A%09@Override%0A%09public int getTabIndex () %7B%0A%09%09return txtValue.getTabIndex();%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * %0A%09 * @see com.google.gwt.user.client.ui.Focusable#setAccessKey(char) */%0A%09@Override%0A%09public void setAccessKey (char key) %7B%0A%09%09txtValue.setAccessKey(key);%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * %0A%09 * @see com.google.gwt.user.client.ui.Focusable#setFocus(boolean) */%0A%09@Override%0A%09public void setFocus (boolean focused) %7B%0A%09%09txtValue.setFocus(focused);%0A%09%7D%0A%0A%09/* (non-Javadoc)%0A%09 * %0A%09 * @see com.google.gwt.user.client.ui.Focusable#setTabIndex(int) */%0A%09@Override%0A%09public void setTabIndex (int index) %7B%0A%09%09txtValue.setTabIndex(index);%0A%09%7D%0A%0A
 %7D%0A

make string property focusable