/* PasteSpecialDialogHandler.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.zk.ui.event.SerializableEventListener;
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.Sheet;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.UserActionContext.Clipboard;
import org.zkoss.zssex.ui.dialog.PasteSpecialCtrl;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;

/**
 * @author dennis
 *
 */
public class PasteSpecialDialogHandler extends AbstractPasteSpecialHandler {
	private static final long serialVersionUID = -2036949560737545806L;

	@Override
	protected boolean processAction(final UserActionContext ctx) {
		final Sheet sheet = ctx.getSheet();
		final AreaRef selection = ctx.getSelection();
		final Range range = Ranges.range(sheet,selection);
		if(range.isProtected())
			return true;
		Clipboard cb = ctx.getClipboard();
		if(cb==null){
			showInfoMessage(Labels.getLabel("zss.actionhandler.msg.cant_find_thing_to_paste"));
			return true;
		}
		if(cb.isCutMode()){
			throw new IllegalOpArgumentException("Cannot support paste special for cut");
		}
		
		PasteSpecialCtrl.show(new SerializableEventListener<DialogCallbackEvent>(){
			private static final long serialVersionUID = 587632547836191154L;
			@Override
			public void onEvent(DialogCallbackEvent event) throws Exception {
				if(PasteSpecialCtrl.ON_OK.equals(event.getName())){
					PasteType type = (PasteType)event.getData(PasteSpecialCtrl.ARG_PASTE_TYPE);
					PasteOperation operation = (PasteOperation)event.getData(PasteSpecialCtrl.ARG_PASTE_OP);
					boolean skipBlank = (Boolean)event.getData(PasteSpecialCtrl.ARG_SKIP_BLANK);
					boolean transpose = (Boolean)event.getData(PasteSpecialCtrl.ARG_TRANSPOSE);
					doPaste(ctx,type, operation, skipBlank, transpose);
				}
			}}, sheet.getBook().getType(), ctx.getSpreadsheet());
		return true;
	}
}
