/* HeaderSizeCtrl.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Dec 20, 2010 3:39:33 PM , Created by Sam
}}IS_NOTE

Copyright (C) 2009 Potix Corporation. All Rights Reserved.

*/
package org.zkoss.zssex.ui.dialog;

import java.util.Map;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zssex.ui.dialog.impl.DialogCtrlBase;
import org.zkoss.zssex.ui.dialog.impl.RefocusDialogCtrlBase;
import org.zkoss.zul.Intbox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Window;

/**
 * @author Sam
 * @author dennis
 */
public class HeaderSizeCtrl extends RefocusDialogCtrlBase {
	private static final long serialVersionUID = 8339635352125083705L;

	public enum HeaderType{
		ROW,COLUMN
	}

	private final static String URI = "~./zssex/dlg/headerSize.zul";
	
	/* header type to set, specify column or row */
	public final static String ARG_HEADER_TYPE = "headerType";
	private HeaderType headerType;
	
	/* header original value */
	public final static String ARG_HEADER_SIZE = "headerSize";
	private Integer size;
	
	/* header title specify Column width or Row height */
	@Wire
	private Label title;
	
	/* header size of the target */
	@Wire
	private Intbox headerSize;
	
	
	public static void show(EventListener<DialogCallbackEvent> callback,HeaderType headrType, Integer headerSize, Spreadsheet ss) {
		Map arg = newArg(callback);
		arg.put(ARG_HEADER_TYPE, headrType);
		arg.put(ARG_HEADER_SIZE, headerSize);
		arg.put(ARG_SPREADSHEET, ss);
		
		Window comp = (Window)Executions.createComponents(URI, null, arg);
		comp.doModal();
		return;
	}
	
	public void doAfterCompose(Window comp) throws Exception{
		super.doAfterCompose(comp);
		
		Map arg = Executions.getCurrent().getArg();
		
		headerType = (HeaderType)arg.get(ARG_HEADER_TYPE);
		initTitle(headerType);
		size = (Integer)arg.get(ARG_HEADER_SIZE);
		headerSize.setValue(size);
		
//		((Button)getFellow("okBtn")).setLabel(Messages.get(MZul.OK));
//		((Button)getFellow("cancelBtn")).setLabel(Messages.get(MZul.CANCEL));
	}
	
	private void initTitle(HeaderType target) {
		String val = null;
		if (target == HeaderType.ROW) {
			val = Labels.getLabel("zssex.dlg.header.rowHeight");
		} else {
			val = Labels.getLabel("zssex.dlg.header.columnWidth");
		}
		getSelf().setTitle(val);
		title.setValue(val + ": ");	
	}

	@Listen("onOK=window; onClick=#okBtn")
	public void onOK() {
		int size = headerSize.getValue();//get before detach(for constraint)
		detach();
		postCallback(ON_OK, newMap(
			newEntry(ARG_HEADER_TYPE, headerType),
			newEntry(ARG_HEADER_SIZE, size)));
	}

	
	@Listen("onCancel=window; onClick=#cancelBtn")
	public void onCancel(){
		detach();
		postCallback(ON_CANCEL, null);
	}
}
