/* AbstractWidget.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 java.io.IOException;

import org.zkoss.zk.au.AuResponse;
import org.zkoss.zk.ui.AbstractComponent;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.UiException;
import org.zkoss.zk.ui.sys.ContentRenderer;
import org.zkoss.zss.ui.Spreadsheet;

/**
 * @author Dennis.Chen
 *
 */
public class WidgetCtrl extends AbstractComponent{
	
	private static final long serialVersionUID = 1L;
	private BaseWidget _widget;
	
	public WidgetCtrl(BaseWidget widget){
		_widget = widget;
	}

	protected void renderProperties(ContentRenderer renderer) throws IOException {
		super.renderProperties(renderer);
		renderer.render("id", _widget.getId());
		renderer.render("type", _widget.getWidgetType());
		renderer.render("panel", _widget.getPanel());
		renderer.render("row", _widget.getRow());
		renderer.render("col", _widget.getColumn());
		renderer.render("left", _widget.getLeft());
		renderer.render("top", _widget.getTop());
		renderer.render("zIndex", _widget.getZindex());
		renderer.render("style", getStyle());
		renderer.render("sizable", _widget.isSizable());
		renderer.render("movable", _widget.isMovable());
		renderer.render("ctrlKeys", _widget.getCtrlKeys());
	}
	
	protected String getStyle(){
		final StringBuffer sb = new StringBuffer(64);
		appendStyle(sb, "z-index", Integer.toString(_widget.getZindex()));
		return sb.toString();
	}
	
	public Spreadsheet getSpreadsheet() {
		return (Spreadsheet)getParent();
	}

	public void setSpreadsheet(Spreadsheet ss) {
		this.setParent(ss);
	}

	public void setParent(Component parent){
		if (parent != null && !(parent instanceof Ghost)) {
			throw new UiException("parent must be a " + Ghost.class);
		}
		super.setParent(parent);
	}

	public BaseWidget getWidget(){
		return _widget;
	}
	
	public String toString(){
		StringBuffer sb = new StringBuffer(super.toString());
		sb.append("[").append(_widget.toString()).append("]");
		return sb.toString();
	}
	
	public void smartUpdate(String attr, Object value) {
		super.smartUpdate(attr, value);
	}
	
	public void response(AuResponse response) {
		super.response(response);
	}
	
	private static final
	void appendStyle(StringBuffer sb, String name, String val) {
		if (val != null && val.length() != 0)
			sb.append(name).append(':').append(val).append(';');
	}
}
