/* CellCommentAction.java

{{IS_NOTE
	Purpose:
		
	Description:
		
	History:
		Wed, Dec 17, 2014 10:27:56 AM, Created by JerryChen
}}IS_NOTE

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

{{IS_RIGHT
	This program is distributed under GPL Version 2.0 in the hope that
	it will be useful, but WITHOUT ANY WARRANTY.
}}IS_RIGHT
*/
package org.zkoss.zssex.ui.impl.undo;

import org.zkoss.zss.api.Range;
import org.zkoss.zss.api.Ranges;
import org.zkoss.zss.api.model.Sheet;
import org.zkoss.zss.ui.impl.undo.AbstractUndoableAction;
/**
 * 
 * @author JerryChen
 * @since 3.7.0
 */
public class CellCommentAction extends AbstractUndoableAction {
	private static final long serialVersionUID = -4359865087115306982L;
	private final String _newRichText;
	private String _oldRichText;
	private boolean _doFlag;
	
	public CellCommentAction(String label,Sheet sheet,int row, int column, int lastRow,int lastColumn,String editText){
		super(label,sheet,row,column,lastRow,lastColumn);
		this._newRichText = editText;
	}

	@Override
	public void doAction() {
		if(!isSheetProtected()){
			Range r = Ranges.range(_sheet,_row,_column,_lastRow,_lastColumn);
			_oldRichText = r.getCommentRichText();
			r.setCommentRichText(_newRichText);
			_doFlag = true;
		}
	}

	@Override
	public boolean isUndoable() {
		return _doFlag && isSheetAvailable() && !isSheetProtected();
	}

	@Override
	public boolean isRedoable() {
		return !_doFlag && isSheetAvailable() && !isSheetProtected();
	}

	@Override
	public void undoAction() {
		Ranges.range(_sheet, _row, _column, _lastRow, _lastColumn).setCommentRichText(_oldRichText);
		_doFlag = false;
	}
	
	@Override
	protected boolean isSheetProtected(){
		final Range range = Ranges.range(_sheet);
		return super.isSheetProtected() && !range.getSheetProtection().isObjectsEditable();
	}
	
	
}
