/* UnhideSheetCtrl.java

	Purpose:
		
	Description:
		
	History:
		Fri, Nov 14, 2014 12:12:41 PM, Created by JerryChen

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

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

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

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.zssex.ui.dialog.impl.DialogCallbackEvent;
import org.zkoss.zssex.ui.dialog.impl.DialogCtrlBase;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Window;

/**
 * 
 * @author JerryChen
 */
public class UnhideSheetCtrl extends DialogCtrlBase {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3360528271114203033L;

	private final static String URI = "~./zssex/dlg/unhideSheet.zul";
	
	public final static String ARG_SHEET_NAMES = "sheetNames";
	
	@Wire
	private Button okBtn;
	
	@Wire
	private Listbox nameListbox;
	
	@Wire
	private Checkbox checkbox;
	
	public static void show(EventListener<DialogCallbackEvent> callback, List<String> names) {
		Map<String, List<String>> arg = newArg(callback);
		arg.put(ARG_SHEET_NAMES, names);
		
		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();
		
		for(String name: (List<String>) arg.get(ARG_SHEET_NAMES))
			nameListbox.appendChild(new Listitem(name));
		
		if(nameListbox.getChildren().size() > 0)
			nameListbox.setSelectedIndex(0);
	}
	
	@Listen("onCheck = #checkbox")
	public void onCheck() {
		if(checkbox.isChecked())
			nameListbox.selectAll();
		else
			nameListbox.clearSelection();
	}
	
	@Listen("onOK=window; onClick=#okBtn;")
	public void onOK() {
		Set<Listitem> itemSet = nameListbox.getSelectedItems();
		List<String> nameList = new ArrayList<String>(itemSet.size());
		for(Listitem item: itemSet) {
			nameList.add(item.getLabel());
		}

		if(nameList.size() > 0)
			postCallback(ON_OK, newMap(newEntry(ARG_SHEET_NAMES, nameList)));

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