/**
 * @author tyl
 * @Action  ini
 * 2011-3-22
 */
package cn.com.origin.autocodemanager.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISources;

import cn.com.origin.autocodemanager.common.AbstractTreeViewPart;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.TreeViewOperation;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class DeleteNodeHandler extends AbstractHandler {

	/**
	 * 
	 */
	public DeleteNodeHandler() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
	 */
	public Object execute(ExecutionEvent e) throws ExecutionException {
		Object obj = e.getApplicationContext();
//		AutoCodeListCodeView codeListView;
//		AutoCodeTreeCodeView codeTreeView;
		AutoCodeSegmentView codeSegmentView;
		if (obj instanceof IEvaluationContext) {
			AbstractAIFUIApplication abstractAIFUIApplication = AIFUtility.getCurrentApplication();
			TCSession session = (TCSession) abstractAIFUIApplication.getSession();
			IEvaluationContext appContext = (IEvaluationContext) obj;
			Object view = appContext.getVariable(ISources.ACTIVE_PART_NAME);
			if (view instanceof AbstractTreeViewPart) {
				try {
					session.setStatus("正在删除......");
					Boolean value = false;
					AbstractTreeViewPart viewPart = (AbstractTreeViewPart) view;
//					codeListView = (AutoCodeListCodeView) viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeListCodeView_ID);
//					codeTreeView = (AutoCodeTreeCodeView) viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeTreeCodeView_ID);
					codeSegmentView = (AutoCodeSegmentView) viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);

					AbstractCreateTreeView autocodeView = viewPart.getCreateView();
					TreeViewOperation treeViewOperation = new TreeViewOperation();
					TreeAndListCodeOperation treeAndListCodeOperation = new TreeAndListCodeOperation();
					AbstractTreeData selectedTreeData = treeViewOperation.getSelectedTreeNode(autocodeView.treeViewer);
					AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);

					if ((selectedTreeData == null)) {
						MessageBox.post("请选择要删除节点!", "INFO", MessageBox.WARNING);
						return null;
					}
					if ((selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
						MessageBox.post("请选择要删除节点!", "INFO", MessageBox.WARNING);
						return null;
					}
					if ((segmentTreeData == null)) {
						MessageBox.post("请选择要删除规则!", "INFO", MessageBox.WARNING);
						return null;
					}
					if ((segmentTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.CodeGroupName))) {
						segmentTreeData = null;
					}
					if (!value) {
						value = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "INFO", "确定删除？");
					}
					if (value) {
						boolean isContinueDelete = treeAndListCodeOperation.deleteTreeORListNode(segmentTreeData, selectedTreeData);
						if (isContinueDelete) {
							treeViewOperation.removeTreeNode(autocodeView.treeViewer, selectedTreeData);
						}
					}
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				session.setReadyStatus();
			}
		}
		return null;
	}
}
