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

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 cn.com.origin.autocode.common.operations.ClassificationOperation;
import cn.com.origin.autocode.views.groupview.CreateGoupView;
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.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeViewRightKeyAction;

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

public class ClassificationViewRightKeyAction extends AbstractTreeViewRightKeyAction {

	/**
	 * @param treeViewer
	 */
	public ClassificationViewRightKeyAction(AbstractCreateTreeView createClassificationView) {
		super(createClassificationView);
	}

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

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

		public void run() {
			String type = selectedTreeData.getPackCodeNodeInfo().getNode_type();
			if ((type.equals(AutoCodeManagerPropertyName.ItemName)) || (type.equals(AutoCodeManagerPropertyName.ComponentName))) {
				MessageBox.post("此节点不可移除!", "INFO", MessageBox.WARNING);
				return;
			}
			ClassificationOperation classificationOperation = new ClassificationOperation();
			createView.treeViewer.remove(selectedTreeData);
			treeViewOperation.removeNodeFromParent(selectedTreeData.getParentTreeData(), selectedTreeData);

			try {
				if ((type.equals(AutoCodeManagerPropertyName.ListName)) || ((type.equals(AutoCodeManagerPropertyName.TreeName))) || (type.equals(AutoCodeManagerPropertyName.CodeGroupName))) {
					String classificationName = selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNode_name();
					String segmentName = selectedTreeData.getPackCodeNodeInfo().getNodeComponent().getUid();
					HashMap<Object, Object> hashMap1 = new HashMap<Object, Object>();
					hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_node_name, classificationName);
					hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_segment, segmentName);
					TCComponent[] components = classificationOperation.searchClassificationNodes(hashMap1);
					if ((components != null) && (components.length > 0)) {
						components[0].delete();
					}
				} else {
					CreateGoupView groupView = new CreateGoupView();
					TCComponent updateComponent = null;
					groupView.getAllUser(selectedTreeData.getParentTreeData());
					String classificationName = selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNode_name();
					String segmentName = "";
					if (selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent() != null) {
						segmentName = selectedTreeData.getParentTreeData().getPackCodeNodeInfo().getNodeComponent().getUid();
						classificationName = selectedTreeData.getParentTreeData().getParentTreeData().getPackCodeNodeInfo().getNode_name();
					}
					HashMap<Object, Object> hashMap1 = new HashMap<Object, Object>();
					hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_node_name, classificationName);
//					if (segmentName.trim().length() > 0) {
						hashMap1.put(AutoCodeManagerPropertyName.ClassificationCodeNode_segment, segmentName);
//					}
					TCComponent[] components = classificationOperation.searchClassificationNodes(hashMap1);
					if ((components != null) && (components.length > 0)) {
						updateComponent = components[0];
					} else {
						return;
					}
					HashMap<Object, Object> hashMap = new HashMap<Object, Object>();
					hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_groups, groupView.groups);
					hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_roles, groupView.roles);
					hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_grant_users, groupView.users);
					hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_last_revise_date, "");
					hashMap.put(AutoCodeManagerPropertyName.ClassificationCodeNode_last_revise_user, "");
					classificationOperation.updateClassificationNode(updateComponent, hashMap);
				}
				createView.treeViewer.refresh();
			} catch (ServiceException e) {
				e.printStackTrace();
			} catch (TCException e) {
				e.printStackTrace();
			}

		}
	}

}
