/* RichTextEditCtrl.java

	Purpose:
		
	Description:
		
	History:
		Fri, Aug 08, 2014 10:46:04 AM, Created by RaymondChao

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

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

import java.util.Map;

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.zssex.ui.widget.Richtextbox;
import org.zkoss.zul.Button;
import org.zkoss.zul.Window;

/**
 * 
 * @author RaymondChao
 */
public class RichTextEditCtrl extends RefocusDialogCtrlBase {
	
	private static final long serialVersionUID = 1L;

	private final static String URI = "~./zssex/dlg/richTextEditDialog.zul";
	
	public final static String ARG_TITLE = "title";
	public final static String ARG_CONTENT = "content";
	
	@Wire
	private Button okBtn;
	
	@Wire
	private Richtextbox richtextbox;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void show(EventListener<DialogCallbackEvent> callback, String title, String content, Spreadsheet ss) {
		Map arg = newArg(callback);
		arg.put(ARG_TITLE, title);
		arg.put(ARG_CONTENT, content);
		arg.put(ARG_SPREADSHEET, ss);
		
		Window comp = (Window) Executions.createComponents(URI, null, arg);
		comp.doModal();
		return;
	}
	
	@SuppressWarnings("rawtypes")
	public void doAfterCompose(Window comp) throws Exception {
		super.doAfterCompose(comp);
		
		final Map arg = Executions.getCurrent().getArg();
		final String title = (String) arg.get(ARG_TITLE);
		final String content = (String) arg.get(ARG_CONTENT);
		
		comp.setTitle(title);
		richtextbox.setContent(content);
	}

	@Listen("onOK = window; onClick = #okBtn")
	public void onOK() {
		detach();
		postCallback(ON_OK, newMap(
			newEntry(ARG_CONTENT, richtextbox.getContent())));
	}
	
	@Listen("onCancel = window; onClick = #cancelBtn")
	public void onCancel() {
		detach();
		postCallback(ON_CANCEL, null);
	}
}