/**
 * @author tyl
 * @Action  ini
 * 2011-3-16
 */
package cn.com.origin.autocodemanager.common.tree;

import java.util.HashMap;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.actions.ActionGroup;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerImageKey;
import cn.com.origin.autocodemanager.common.AutoCodeManagerPropertyName;
import cn.com.origin.autocodemanager.common.AutoCodeManangerConstants;
import cn.com.origin.autocodemanager.common.operations.PackCodeNodeInfo;
import cn.com.origin.autocodemanager.common.operations.TreeAndListCodeOperation;
import cn.com.origin.autocodemanager.views.AutoCodeListCodeView;
import cn.com.origin.autocodemanager.views.AutoCodeSegmentView;
import cn.com.origin.autocodemanager.views.AutoCodeTreeCodeView;
import cn.com.origin.autocodemanager.views.codeview.CodeSegmentDialog;
import cn.com.origin.autocodemanager.views.codeview.CopyCodeNodeDialog;
import cn.com.origin.autocodemanager.views.codeview.NewCodeNodeDialog;
import cn.com.origin.autocodemanager.views.listcodeview.CreateListCodeView;
import cn.com.origin.autocodemanager.views.listcodeview.NewListPropertyNodeDialog;
import cn.com.origin.autocodemanager.views.listcodeview.NewListPropertyValueNodeDialog;
import cn.com.origin.autocodemanager.views.treecodeview.CreateTreeCodeView;
import cn.com.origin.autocodemanager.views.treecodeview.NewTreeCodeNodeDialog;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.schemas.soa._2006_03.exceptions.ServiceException;

public class AbstractTreeViewRightKeyAction extends ActionGroup {
	public TreeViewer treeViewer;
	public MenuManager menuManager;
	public AbstractCreateTreeView createView;
	public AbstractTreeData selectedTreeData;
	public TCSession session;
	public AutoCodeListCodeView codeListView;
	public AutoCodeSegmentView codeSegmentView;
	public AutoCodeTreeCodeView codeTreeView;
	public TreeAndListCodeOperation treeAndListCodeOperation;
	public TreeViewOperation treeViewOperation;

	public AbstractTreeViewRightKeyAction(AbstractCreateTreeView createTreeView) {
		this.createView = createTreeView;
		this.treeViewer = createTreeView.treeViewer;
		initParams();
		this.selectedTreeData = treeViewOperation.getSelectedTreeNode(createTreeView.treeViewer);
	}

	public void initParams() {
		this.treeAndListCodeOperation = new TreeAndListCodeOperation();
		this.treeViewOperation = new TreeViewOperation();
		session = (TCSession) AIFUtility.getCurrentApplication().getSession();
		if (createView == null) {
			return;
		}
		if (createView.viewPart == null) {
			return;
		}
		codeListView = (AutoCodeListCodeView) createView.viewPart.getSite().getPage().findView(AutoCodeManangerConstants.AutoCodeManagerListCodeView_ID);
		codeSegmentView = (AutoCodeSegmentView) createView.viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeSegmentView_ID);
		codeTreeView = (AutoCodeTreeCodeView) createView.viewPart.getSite().getPage().findView(AutoCodeManangerConstants.AutoCodeManagerTreeCodeView_ID);
	}

	public void fillContextMenu() {
		if (menuManager == null) {
			// 第一次调用该方法时
			menuManager = new MenuManager();
			menuManager.add(new LockCodeNodeAction());
			menuManager.add(new UnLockCodeNodeAction());
			menuManager.add(new RemoveCodeNodeAction());
		}
		Tree tree = treeViewer.getTree();
		Menu menu = menuManager.createContextMenu(tree);
		tree.setMenu(menu);
	}

	public class EidtCodeNodeAction extends Action {
		public EidtCodeNodeAction() {
			setText("编辑");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.EDIT_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			try {
				if ((selectedTreeData == null) || (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
					MessageBox.post("请选择要修改节点!", "INFO", MessageBox.WARNING);
					return;
				}
				AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
				TCComponent segmentComponent = segmentTreeData.getPackCodeNodeInfo().getNodeComponent();
				TCComponent selectedComponent = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
				if (selectedComponent != null) {
					TCComponent[] components = treeAndListCodeOperation.getNodeReferenceComponents(selectedComponent);
					String segmentNames = "";
					if (components != null) {
						for (int i = 0; i < components.length; i++) {
							if (!components[i].equals(segmentComponent)) {
								components[i].refresh();
								segmentNames = segmentNames + components[i].getProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name);
								segmentNames = segmentNames + "\n";
							}
						}
						if (segmentNames.trim().length() > 0) {
							Boolean value = false;
							value = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "INFO", "下面规则引用到此节点：\n" + segmentNames + "确定修改？");
							if (!value) {
								return;
							}
						}
					}
				}
				Image nodeImage = selectedTreeData.getImage();
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				if (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() != null) {
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "true");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, session.getUser().getUid());
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, session.getUser().getUid());
					try {
						TCComponent updateComponent = new TreeAndListCodeOperation().updateTreeORListNode(selectedComponent, hashMap);
						PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(updateComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
						selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
					} catch (ServiceException e) {
						e.printStackTrace();
					}
				}
				selectedTreeData.setImage(new AbstractTreeData().getImageByType(AutoCodeManagerPropertyName.LockName));
				treeViewer.refresh(selectedTreeData);

				if (selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyNodeName)) {
					new NewListPropertyNodeDialog(treeViewer.getControl().getShell(), (CreateListCodeView) createView, "编辑列表", true).open();
				} else {
					if (selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyValueNodeName)) {
						new NewListPropertyValueNodeDialog(treeViewer.getControl().getShell(), (CreateListCodeView) createView, "编辑列表", true).open();
					} else if (selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.TreeNodeName)) {
						new NewTreeCodeNodeDialog(treeViewer.getControl().getShell(), (CreateTreeCodeView) createView, "编辑节点", true, true).open();
					} else {
						if (selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.CodeGroupName)) {
							NewCodeNodeDialog codeNodeDialog = new NewCodeNodeDialog(null, createView, AutoCodeManagerPropertyName.AddRuleGroupNodeName, "编辑组", true);
							codeNodeDialog.open();
						} else {
							NewCodeNodeDialog codeNodeDialog = new NewCodeNodeDialog(null, createView, AutoCodeManagerPropertyName.AddRuleRootNodeName, "编辑规则", true);
							codeNodeDialog.open();
						}
					}
				}
				HashMap<Object, Object> hashMap2 = new HashMap<Object, Object>();
				if (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() != null) {
					hashMap2.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
					hashMap2.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
					hashMap2.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
					hashMap2.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, session.getUser().getUid());
					hashMap2.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, session.getUser().getUid());
					TCComponent updateComponent = new TreeAndListCodeOperation().updateTreeORListNode(selectedComponent, hashMap2);
					PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(updateComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
					selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
				}
				selectedTreeData.setImage(nodeImage);
				treeViewer.refresh(selectedTreeData);
			} catch (TCException e) {
				e.printStackTrace();
			} catch (ServiceException e) {
				e.printStackTrace();
			}
		}
	}

	public class AddChildrenNodeAction extends Action {
		String text = "";
		String type = "";

		public AddChildrenNodeAction(String text, String type) {
			setText(text);
			this.text = text;
			this.type = type;
			ImageDescriptor imageDesc = new AbstractTreeData().getImageDescriptorByType(type);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			try {
				AbstractTreeData rootNode = treeViewOperation.getSelectedTreeNode(codeSegmentView.getCreateView().treeViewer);
				if (!treeViewOperation.isRuleRootNode(rootNode)) {
					MessageBox.post("请选择要修改规则!", "INFO", MessageBox.WARNING);
					return;
				}
				new CodeSegmentDialog(treeViewer.getControl().getShell(), createView, text, type).open();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public class LockCodeNodeAction extends Action {
		public LockCodeNodeAction() {
			setText("锁定");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LOCK_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			try {
				if ((selectedTreeData == null) || (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
					MessageBox.post("请选择要锁定节点!", "INFO", MessageBox.WARNING);
					return;
				}

				AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
				TCComponent segmentComponent = segmentTreeData.getPackCodeNodeInfo().getNodeComponent();
				TCComponent selectedComponent = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
				if (selectedComponent != null) {
					TCComponent[] components = treeAndListCodeOperation.getNodeReferenceComponents(selectedComponent);
					String segmentNames = "";
					if (components != null) {
						for (int i = 0; i < components.length; i++) {
							if (!components[i].equals(segmentComponent)) {
								components[i].refresh();
								segmentNames = segmentNames + components[i].getProperty(AutoCodeManagerPropertyName.TreeAndListCodeNode_node_name);
								segmentNames = segmentNames + "\n";
							}
						}
						if (segmentNames.trim().length() > 0) {
							Boolean value = false;
							value = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "INFO", "下面规则引用到此节点：\n" + segmentNames + "确定锁定？");
							if (!value) {
								return;
							}
						}
					}
				}
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				if (selectedComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "true");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_date, "");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_create_user, session.getUser().getUid());
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, session.getUser().getUid());
					TCComponent updateComponent = new TreeAndListCodeOperation().updateTreeORListNode(selectedComponent, hashMap);
					PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(updateComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
					selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
					createView.treeViewer.refresh(selectedTreeData);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public class UnLockCodeNodeAction extends Action {
		public UnLockCodeNodeAction() {
			setText("解锁");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.UNLOCK_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			try {
				if ((selectedTreeData == null) || (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
					MessageBox.post("请选择要解锁节点!", "INFO", MessageBox.WARNING);
					return;
				}
				TCComponent selectedComponent = selectedTreeData.getPackCodeNodeInfo().getNodeComponent();
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				if (selectedComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_is_lock, "false");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_date, "");
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_last_revise_user, session.getUser().getUid());
					TCComponent updateComponent = new TreeAndListCodeOperation().updateTreeORListNode(selectedComponent, hashMap);
					PackCodeNodeInfo packCodeNodeInfo = treeAndListCodeOperation.getTreeAndListStringValue(updateComponent, AutoCodeManagerPropertyName.searchTreeAndListCodeNodePropNames);
					selectedTreeData.setPackCodeNodeInfo(packCodeNodeInfo);
					createView.treeViewer.refresh(selectedTreeData);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public class CopyCodeNodeAction extends Action {
		public CopyCodeNodeAction() {
			setText("复制");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.EDIT_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			if ((selectedTreeData == null) || (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
				MessageBox.post("请选择要复制节点!", "INFO", MessageBox.WARNING);
				return;
			}

			try {
				if (treeViewOperation.isRuleRootNode(selectedTreeData)) {
					CopyCodeNodeDialog codeNodeDialog = new CopyCodeNodeDialog(null, createView, "复制规则", true, AutoCodeManagerPropertyName.AddRuleRootNodeName);
					codeNodeDialog.open();
				} else {
					CopyCodeNodeDialog codeNodeDialog = new CopyCodeNodeDialog(null, createView, "复制组", true, AutoCodeManagerPropertyName.AddRuleGroupNodeName);
					codeNodeDialog.open();
				}

			} catch (Exception e) {
				e.printStackTrace();
			}

		}
	}

	public class RemoveCodeNodeAction extends Action {
		public RemoveCodeNodeAction() {
			setText("移除");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.REMOVE_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			if ((selectedTreeData == null) || (selectedTreeData.getPackCodeNodeInfo().getNodeComponent() == null)) {
				MessageBox.post("请选择要移除节点!", "INFO", MessageBox.WARNING);
				return;
			}
			try {
				boolean value = MessageDialog.openConfirm(Display.getCurrent().getActiveShell(), "INFO", "确定移除？");
				AbstractTreeData segmentTreeData = treeViewOperation.getSelectedTreeNode(codeSegmentView.createView.treeViewer);
				if (value) {
					treeViewOperation.removeTreeNode(createView.treeViewer, selectedTreeData);
					treeAndListCodeOperation.removeTreeORListNode(segmentTreeData, selectedTreeData);
				}
			} catch (ServiceException e) {
				e.printStackTrace();
			} catch (TCException e) {
				e.printStackTrace();
			}
		}
	}
}
