package com.scc.treecolumn.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.gwtext.client.widgets.Editor;
import com.gwtext.client.widgets.event.EditorListenerAdapter;
import com.gwtext.client.widgets.form.Field;
import com.gwtext.client.widgets.tree.ColumnTree;
import com.gwtext.client.widgets.tree.ColumnTreeEditor;

/**
 * The class SccColumnTreeEditor updates the tooltip of a cell after edit action
 * is completed.
 * 
 * @author Sorinel Cristescu
 */
public class SccColumnTreeEditor extends ColumnTreeEditor {

	/** Updates tooltip to the edited value, after the edit action is completed */
	public final static EditorListenerAdapter UPDATE_TOOLTIP_LISTENER = new EditorListenerAdapter() {
		@Override
		public void onComplete(final Editor source, final Object value,
				final Object startValue) {
			final Element elem = ((SccColumnTreeEditor) source).getCellElem();
			final String id = elem.getId();
			final Element el = DOM.getElementById(id);
			el.setAttribute("title", (String) value);
		}
	};

	/**
	 * Create an editable column tree using the specified Field as the editor.
	 * 
	 * @param columnTree
	 *            ColumnTree object
	 * @param dataIndex
	 *            String name of the column
	 * @param field
	 *            Field editor
	 * @param enableTooltip
	 *            boolean true to enable updating of the tooltip
	 */
	public SccColumnTreeEditor(final ColumnTree columnTree,
			final String dataIndex, final Field field,
			final boolean enableTooltip) {
		super(columnTree, dataIndex, field);

		if (enableTooltip) {
			addListener(UPDATE_TOOLTIP_LISTENER);
		}
	}

	/* gets the cell DOM element */
	private native Element getCellElem() /*-{
		var editor = this.@com.gwtext.client.widgets.Component::getOrCreateJsObj()();
		return editor.boundEl;
	}-*/;

}

