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

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.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.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeViewRightKeyAction;

public class CodeSegmentViewRightKeyAction extends AbstractTreeViewRightKeyAction {
	/**
	 * @param treeViewer
	 */
	public CodeSegmentViewRightKeyAction(AbstractCreateTreeView createCodeSegmentView) {
		super(createCodeSegmentView);
	}

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

	private class NewCodeNodeAction extends Action {
		String actionType = "";
		String title = "";

		public NewCodeNodeAction(String actionType) {
			this.actionType = actionType;
			if (actionType.equals(AutoCodeManagerPropertyName.AddRuleRootNodeName)) {
				title = "新建规则";
				ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODE_IMAGE);
				this.setImageDescriptor(imageDesc);
			} else {
				title = "新建组";
				ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.CODEGROUP_IMAGE);
				this.setImageDescriptor(imageDesc);
			}
			this.setText(title);
		
		}

		public void run() {
			AbstractTreeData selectedTreeData=	treeViewOperation.getSelectedTreeNode(createView.treeViewer);
			if((treeViewOperation.isRuleRootNode(selectedTreeData))&&(actionType.equals(AutoCodeManagerPropertyName.AddRuleGroupNodeName)))
			{
				MessageBox.post("此节点下不能建组!", "INFO", MessageBox.WARNING);
				return;
			}
			NewCodeNodeDialog codeNodeDialog = new NewCodeNodeDialog(null, createView, actionType,title, false);
			codeNodeDialog.open();
		}
	}

}
