/* InsertChartHandler.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.*;
import org.zkoss.zss.api.model.Book.BookType;
import org.zkoss.zss.api.model.*;
import org.zkoss.zss.api.model.Chart.Grouping;
import org.zkoss.zss.api.model.Chart.LegendPosition;
import org.zkoss.zss.api.model.Chart.Type;
import org.zkoss.zss.api.model.impl.SheetImpl;
import org.zkoss.zss.model.ViewAnchor;
import org.zkoss.zss.ui.UserActionContext;
import org.zkoss.zssex.api.ChartDataUtil;

/**
 * @author dennis
 * @since 3.0.0
 */
public class InsertChartHandler extends AbstractObjectHandler {
	private static final long serialVersionUID = 7005418038394891974L;
	private final Chart.Type _type;
	private final Chart.Grouping _grouping;
	private final Chart.LegendPosition _pos;

	public InsertChartHandler(Type type, Grouping grouping,
			LegendPosition pos) {
		super();
		this._type = type;
		this._grouping = grouping;
		this._pos = pos;
	}

	@Override
	protected boolean processAction(final UserActionContext ctx) {
		Sheet sheet = ctx.getSheet();

		AreaRef selection = ctx.getSelection();
		Range range = Ranges.range(sheet, selection.getRow(),
				selection.getColumn(), selection.getLastRow(),
				selection.getLastColumn());
		if(range.isProtected() && !range.getSheetProtection().isObjectsEditable()){
			showProtectMessage();
			return true;
		}

		SheetAnchor anchor = SheetOperationUtil.toChartAnchor(range);
		SheetOperationUtil.addChart(range,anchor,_type,_grouping,_pos);
		return true;
	}

}
