/* AbstractPasteSpecialHandler.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		2013/8/5 , Created by dennis
}}IS_NOTE

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

{{IS_RIGHT
}}IS_RIGHT
*/
package org.zkoss.zssex.ui.impl.ua;

import org.zkoss.util.resource.Labels;
import org.zkoss.zss.api.IllegalOpArgumentException;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zss.api.Range.PasteOperation;
import org.zkoss.zss.api.Range.PasteType;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.model.Book;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.UserActionContext.Clipboard;
import org.zkoss.zss.ui.impl.ua.AbstractHandler;
import org.zkoss.zss.ui.impl.undo.CutCellAction;
import org.zkoss.zss.ui.impl.undo.PasteSpecialCellAction;
import org.zkoss.zss.ui.sys.UndoableActionManager;

/**
 * @author dennis
 *
 */
public abstract class AbstractPasteSpecialHandler extends AbstractHandler {
	private static final long serialVersionUID = -1531153605054648412L;
	public AbstractPasteSpecialHandler(){
	}

	protected boolean doPaste(UserActionContext ctx,PasteType pasteType, PasteOperation pasteOperation, boolean skipBlank, boolean transpose) {
		try{
			return doPaste0(ctx,pasteType,pasteOperation,skipBlank,transpose);
		}catch(IllegalOpArgumentException x){
			showInfoMessage(Labels.getLabel("zss.actionhandler.msg.illegal_range_operation") +" : "+x.getMessage());
			return true;
		}
		
	}
	protected boolean doPaste0(UserActionContext ctx,PasteType pasteType, PasteOperation pasteOperation, boolean skipBlank, boolean transpose) {
		Clipboard cb = ctx.getClipboard();
		if(cb==null){
			//don't handle it , so give a chance to do client paste
			return false;
		}
		if(cb.isCutMode()){
			throw new IllegalOpArgumentException("Cannot support paste special for cut");
		}
		
		Book book = ctx.getBook();
		Sheet destSheet = ctx.getSheet();
		Sheet srcSheet = cb.getSheet();
		if(srcSheet==null){
			showInfoMessage(Labels.getLabel("zss.actionhandler.msg.cant_find_sheet_to_paste"));
			ctx.clearClipboard();
			return true;
		}
		AreaRef src = cb.getSelection();
		
		AreaRef selection = ctx.getSelection();
		
		Range srcRange = Ranges.range(srcSheet, src.getRow(),
				src.getColumn(), src.getLastRow(),src.getLastColumn());

		Range destRange = Ranges.range(destSheet, selection.getRow(),
				selection.getColumn(), selection.getLastRow(), selection.getLastColumn());
		
		if (destRange.isProtected()) {
			showProtectMessage();
			return true;
		} else if (cb.isCutMode() && srcRange.isProtected()) {
			showProtectMessage();
			return true;
		}
		
		UndoableActionManager uam = ctx.getSpreadsheet().getUndoableActionManager();
		uam.doAction(new PasteSpecialCellAction(Labels.getLabel("zss.undo.paste"),
				srcSheet, src.getRow(), src.getColumn(),src.getLastRow(), src.getLastColumn(), 
				destSheet, selection.getRow(), selection.getColumn(),selection.getLastRow(), selection.getLastColumn(),
				pasteType, pasteOperation, skipBlank, transpose));
		return true;
	}

}
