/* BaseWidget.java

	Purpose:
		
	Description:
		
	History:
		Apr 16, 2008 5:35:09 PM     2008, Created by Dennis.Chen

Copyright (C) 2008 Potix Corporation. All Rights Reserved.
 */
package org.zkoss.zssex.ui.widget;

import org.zkoss.lang.Objects;
import org.zkoss.poi.ss.util.AreaReference;
import org.zkoss.zk.au.AuResponse;
import org.zkoss.zk.au.out.AuInvoke;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zss.ui.Widget;
import org.zkoss.zss.ui.sys.WidgetHandler;

/**
 * @author Dennis.Chen
 * 
 */
public abstract class BaseWidget implements Widget {
	private static final long serialVersionUID = 8862367074307148694L;
	private String _id;
	private int _row;
	private int _column;
	
	private int _row2;
	private int _column2;

	private int _left;
	private int _top;

	private int _zindex = 0;

	private boolean _visible = true;
	
	private boolean _movable = false;
	
	private boolean _sizable = false;
	
	private String _ctrlKeys;

	private WidgetCtrl _ctrl;

	private WidgetHandler _handler;

	private boolean _inClient = false;
	
	/**
	 * the panel location, could be default,left,top,corner
	 */
	private String _panel;
	
	protected BaseWidget(String panel){
		this._panel = panel;
	}

	protected WidgetCtrl newCtrl() {
		WidgetCtrl ctrl = new WidgetCtrl(this);
		return ctrl;
	}

	boolean isInClient() {
		return _inClient;
	}

	void setInClient(boolean inClient) {
		this._inClient = inClient;
	}

	public Component getComponent() {
		return getCtrl();
	}

	protected Component getInnerComponent() {
		return null;
	}

	synchronized protected WidgetCtrl getCtrl() {
		if (_ctrl == null) {
			_ctrl = newCtrl();
		}
		return _ctrl;
	}

	public WidgetHandler getHandler() {
		return _handler;
	}

	public void setHandler(WidgetHandler handler) {
		if (!Objects.equals(_handler, handler)) {
			if (_handler != null)
				_handler.removeWidget(this);
			_handler = handler;
			if (_handler != null)
				_handler.addWidget(this);
		}
	}

	/* package */void setHandler0(WidgetHandler handler) {
		if (!Objects.equals(_handler, handler)) {
			_handler = handler;
		}
	}

	public int getRow() {
		return _row;
	}

	public void setRow(int row) {
		if (this._row != row) {
			this._row = row;
			getCtrl().smartUpdate("row", _row);
		}
	}

	public int getColumn() {
		return _column;
	}

	public void setColumn(int column) {
		if (this._column != column) {
			this._column = column;
			getCtrl().smartUpdate("col", _column);
		}
	}

	public int getRow2() {
		return _row2;
	}

	public void setRow2(int row) {
		if (this._row2 != row) {
			this._row2 = row;
			getCtrl().smartUpdate("row2", _row2);
		}
	}

	public int getColumn2() {
		return _column2;
	}

	public void setColumn2(int column) {
		if (this._column2 != column) {
			this._column2 = column;
			getCtrl().smartUpdate("col2", _column2);
		}
	}

	public int getLeft() {
		return _left;
	}

	public void setLeft(int left) {
		if (this._left != left) {
			this._left = left;
			getCtrl().smartUpdate("left", _left);
		}

	}

	public int getTop() {
		return _top;
	}

	public void setTop(int top) {
		if (this._top != top) {
			this._top = top;
			getCtrl().smartUpdate("top", _top);
		}
	}

	public void setZindex(int zindex) {
		if (this._zindex != zindex) {
			this._zindex = zindex;
			getCtrl().smartUpdate("zindex", _zindex);
		}
	}

	public int getZindex() {
		return this._zindex;
	}

	public boolean isVisible() {
		return _visible;
	}

	public void setVisible(boolean visible) {
		if (this._visible != visible) {
			this._visible = visible;
			getCtrl().smartUpdate("visible", _visible);
		}
	}
	
	public boolean isMovable() {
		return this._movable;
	}
	
	public void setMovable(boolean movable) {
		if (this._movable != movable) {
			this._movable = movable;
			getCtrl().smartUpdate("movable", movable);
		}
	}
	
	public boolean isSizable() {
		return this._sizable;
	}
	
	public void setSizable(boolean sizable) {
		if (this._sizable != sizable) {
			this._sizable = sizable;
			getCtrl().smartUpdate("sizable", sizable);
		}
	}
	
	public void setCtrlKeys(String ctrlKeys) {
		if (ctrlKeys != null && ctrlKeys.length() == 0)
			ctrlKeys = null;
		if (this._ctrlKeys != ctrlKeys) {
			this._ctrlKeys = ctrlKeys;
			getCtrl().smartUpdate("ctrlKeys", ctrlKeys);
		}
	}
	
	public String getCtrlKeys() {
		return this._ctrlKeys;
	}

	public void setId(String id) {
		if (this._id != id) {
			this._id = id;
			getCtrl().smartUpdate("id", id);
		}
	}
	
	public String getId() {
		return this._id;
	}
	
	public void addEventListener(String evtnm, EventListener listener) {
		Component comp = getInnerComponent();
		if (comp != null) {
			comp.addEventListener(evtnm, listener);
		}
	}

	public boolean removeEventListener(String evtnm, EventListener listener) {
		Component comp = getInnerComponent();
		if (comp != null) {
			return comp.removeEventListener(evtnm, listener);
		}
		return false;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer(this.getClass().getSimpleName());
		sb.append(",row:").append(_row).append(",col:").append(_column).append(",panel:").append(_panel);
		sb.append(",client:").append(_inClient);
		sb.append(",zindex:").append(_zindex);
		return sb.toString();
	}
	
	public String getPanel(){
		return _panel;
	}
	

	public void adjustLocation() {
		getCtrl().response(new AuInvoke(getCtrl(),"adjustLocation"));
	}
	
	public abstract void invalidate();
	
//	public abstract AreaReference getUpdateAreaReference();

	/**
	 * Returns the widget type.
	 * @return the widget type.
	 */
	public abstract String getWidgetType(); 
}
