/**
 * @author tyl
 * @Action  ini
 * 2011-3-17
 */
package cn.com.origin.autocodemanager.views.listcodeview;

import java.util.HashMap;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.util.MessageBox;
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.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeViewRightKeyAction;

public class CodeListViewRightKeyAction extends AbstractTreeViewRightKeyAction {
	public CodeListViewRightKeyAction(CreateListCodeView createListCodeView) {
		super(createListCodeView);
	}

	public void fillContextMenu() {
		if (menuManager == null) {
			// 第一次调用该方法时
			menuManager = new MenuManager();
			menuManager.add(new NewCodeNodeAction());
			menuManager.add(new NewCodeNodeValueAction());
			menuManager.add(new AddChildrenNodeAction("添加属性", AutoCodeManagerPropertyName.ListPropertyNodeName));
			menuManager.add(new SetDefaultSelectedAction());
			menuManager.add(new CancelDefaultSelectedAction());
			menuManager.add(new EidtCodeNodeAction());
			menuManager.add(new RemoveCodeNodeAction());
			menuManager.add(new LockCodeNodeAction());
			menuManager.add(new UnLockCodeNodeAction());
		}
		Tree tree = treeViewer.getTree();
		Menu menu = menuManager.createContextMenu(tree);
		tree.setMenu(menu);
	}

	public class SetDefaultSelectedAction extends Action {
		public SetDefaultSelectedAction() {
			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();
				if (!selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyValueNodeName)) {
					MessageBox.post("请选择列表值!", "INFO", MessageBox.WARNING);
					return;
				}
				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				if (selectedComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "true");
					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 CancelDefaultSelectedAction extends Action {
		public CancelDefaultSelectedAction() {
			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();
				if (!selectedTreeData.getPackCodeNodeInfo().getNode_type().equals(AutoCodeManagerPropertyName.ListPropertyValueNodeName)) {
					MessageBox.post("请选择列表值!", "INFO", MessageBox.WARNING);
					return;
				}

				HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
				if (selectedComponent != null) {
					hashMap.put(AutoCodeManagerPropertyName.TreeAndListCodeNode_isDefaultSelected, "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();
			}
		}
	}

	private class NewCodeNodeAction extends Action {
		public NewCodeNodeAction() {
			setText("新建属性");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTNODE_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			AbstractTreeData rootNode = treeViewOperation.getSelectedTreeNode(codeSegmentView.getCreateView().treeViewer);
			if (rootNode == null) {
				MessageBox.post("请选择要修改节点!", "INFO", MessageBox.WARNING);
				return;
			}
			String type = rootNode.getPackCodeNodeInfo().getNode_type();
			if (!type.equals(AutoCodeManagerPropertyName.ListName)) {
				MessageBox.post("请选择要修改列表节点!", "INFO", MessageBox.WARNING);
				return;
			}
			new NewListPropertyNodeDialog(treeViewer.getControl().getShell(), (CreateListCodeView) createView, "新建属性", false).open();
		}
	}

	private class NewCodeNodeValueAction extends Action {
		public NewCodeNodeValueAction() {
			setText("新建值");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.LISTVALUE_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			AbstractTreeData rootNode = treeViewOperation.getSelectedTreeNode(createView.treeViewer);
			if (rootNode == null) {
				MessageBox.post("请选择要修改节点!", "INFO", MessageBox.WARNING);
				return;
			}
			new NewListPropertyValueNodeDialog(treeViewer.getControl().getShell(), (CreateListCodeView) createView, "新建值", false).open();
		}
	}
}
