package org.zkoss.zss.essential.events;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.*;
import org.zkoss.zss.api.*;
import org.zkoss.zss.ui.Spreadsheet;
import org.zkoss.zss.ui.event.*;
import org.zkoss.zul.*;

/**
 * An example of CellSelectionEvent.
 * @author Hawk
 *
 */
public class RangeSelectionComposer extends SelectorComposer<Component>{

	private static final long serialVersionUID = 1L;
	
	@Wire
	private Spreadsheet ss;
	@Wire
	private Window dialog;
	
	private CellSelectionListener cellSelectionListener = new CellSelectionListener();
	private NoEditListener noEditListener = new NoEditListener();
	
	@Listen("onClick = #open")
	public void open(){
		dialog.setVisible(true);
		ss.addEventListener("onCellSelection", cellSelectionListener);
		ss.focus();
		Textbox rangeBox  = (Textbox)dialog.getFellow("rangeBox");
		rangeBox.setValue("");
		//forbid users doing anything
		ss.setShowFormulabar(false);
		ss.setShowToolbar(false);
		ss.setShowContextMenu(false);
		ss.setShowSheetbar(false);
		ss.addEventListener("onStartEditing", noEditListener);
	}
	
	@Listen("onCellSelection = #dialog")
	public void onCellSelection(CellSelectionEvent event){
		Textbox rangeBox  = (Textbox)dialog.getFellow("rangeBox");
		Range selection =Ranges.range(event.getSheet(), event.getArea()); 
		if (selection.isWholeRow()){
			rangeBox.setValue(Ranges.getRowRefString(event.getRow()));
		}else if (selection.isWholeColumn()){
			rangeBox.setValue(Ranges.getColumnRefString(event.getColumn()));
		}else{
			rangeBox.setValue(Ranges.getAreaRefString(event.getSheet(), event.getArea()));
		}
	}
	
	
	@Listen("onClick = #dialog #ok")
	public void hideDialog(Event event){
		//could validate user selection
		dialog.setVisible(false);
		event.stopPropagation();
		ss.removeEventListener("onCellSelection", cellSelectionListener); //reduce traffic to a server
		//recover editing actions back
		ss.setShowFormulabar(true);
		ss.setShowToolbar(true);
		ss.setShowContextMenu(true);
		ss.setShowSheetbar(true);
		ss.removeEventListener("onStartEditing", noEditListener); //back to normal
	}
	
	class CellSelectionListener implements EventListener<CellSelectionEvent>{
		@Override
		public void onEvent(CellSelectionEvent event) throws Exception {
			Events.postEvent(dialog, event);
		}
	}
	
	class NoEditListener implements EventListener<StartEditingEvent>{

		@Override
		public void onEvent(StartEditingEvent event) throws Exception {
			event.cancel();			
		}
	}
}





@@ -364,16 +364,16 @@
  * A
-n exampl
+ use cas
 e of
@@ -756,16 +756,150 @@
 er();%0A%09%0A
+%09/**%0A%09 * When opening the dialog to select a range, we can hide edit UI and cancel onStartEditing event to forbid users editing.%0A%09 */%0A
 %09@Listen
@@ -2556,24 +2556,93 @@
 );%0A%09%09%7D%0A%09%7D%0A%09%0A
+%09/**%0A%09 * Prevent users from editing cells.%0A%09 * @author hawk%0A%09 *%0A%09 */%0A
 %09class NoEdi

add some comments