/* RichTextEditHandler.java

	Purpose:
		
	Description:
		
	History:
		Fri, Aug 08, 2014 10:27:56 AM, Created by RaymondChao

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

*/
package org.zkoss.zssex.ui.impl.ua;

import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.SerializableEventListener;
import org.zkoss.zss.api.AreaRef;
import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.model.SSheet;
import org.zkoss.zss.model.util.RichTextHelper;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zss.ui.impl.CellFormatHelper;
import org.zkoss.zss.ui.impl.ua.AbstractCellHandler;
import org.zkoss.zss.ui.impl.undo.CellEditTextAction;
import org.zkoss.zss.ui.impl.undo.CellRichTextAction;
import org.zkoss.zss.ui.sys.UndoableActionManager;
import org.zkoss.zssex.ui.dialog.RichTextEditCtrl;
import org.zkoss.zssex.ui.dialog.impl.DialogCallbackEvent;

/**
 * 
 * @author RaymondChao
 * @since 3.6.0
 */
public class RichTextEditHandler extends AbstractCellHandler {
	private static final long serialVersionUID = 4621843841374312965L;

	/* (non-Javadoc)
	 * @see org.zkoss.zss.ui.sys.ua.impl.AbstractHandler#processAction(org.zkoss.zss.ui.UserActionContext)
	 */
	@Override
	protected boolean processAction(final UserActionContext ctx) {
		final Sheet sheet = ctx.getSheet();
		final SSheet internalSheet = ctx.getSheet().getInternalSheet();
		final AreaRef selection = ctx.getSelection();
		final int row = selection.getRow();
		final int column = selection.getColumn();
		final Range range = Ranges.range(sheet, row, column);
		if (range.isProtected()) {
			showProtectMessage();
			return true;
		}
		RichTextEditCtrl.show(new SerializableEventListener<DialogCallbackEvent>() {
			private static final long serialVersionUID = -213431772127471055L;

			@Override
			public void onEvent(DialogCallbackEvent event) throws Exception {
				if (RichTextEditCtrl.ON_OK.equals(event.getName())) {
					final String content = (String) event.getData(RichTextEditCtrl.ARG_CONTENT);
					
					//ZSS-753
					//range.setCellRichText(content);
					UndoableActionManager uam = ctx.getSpreadsheet().getUndoableActionManager();
					uam.doAction(new CellRichTextAction(Labels.getLabel("zss.undo.editText"),sheet,row,column,row,column,content));
				}
			}}, "Rich Text Edit", CellFormatHelper.getRichTextEditCellHtml(internalSheet, row, column), ctx.getSpreadsheet());
		return true;
	}

}