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

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

public class CodeTreeViewRightKeyAction extends AbstractTreeViewRightKeyAction {

	/**
	 * @param treeViewer
	 */
	public CodeTreeViewRightKeyAction(CreateTreeCodeView createTreeCodeView) {
		super(createTreeCodeView);
	}

	public void fillContextMenu() {
		if (menuManager == null) {
			// 第一次调用该方法时
			menuManager = new MenuManager();	
			menuManager.add(new NewCodeNodeAction(AutoCodeManagerPropertyName.AddTreeNodeName));
			menuManager.add(new NewCodeNodeAction(AutoCodeManagerPropertyName.AddChildrenTreeNodeName));
			menuManager.add(new AddChildrenNodeAction("添加节点", AutoCodeManagerPropertyName.TreeNodeName));
			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 NewCodeNodeAction extends Action {
		private String actionType;
		private String title;

		public NewCodeNodeAction(String type) {
			this.actionType = type;
			if (type.equals(AutoCodeManagerPropertyName.AddChildrenTreeNodeName)) {
				title = "新建节点";
			} else {
				title = "新建平级节点";
			}
			setText(title);
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.TREENODE_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.TreeName)) {
				MessageBox.post("请选择要修改 的编码树规则!", "INFO", MessageBox.WARNING);
				return;
			}
			if (type.equals(AutoCodeManagerPropertyName.AddChildrenTreeNodeName)) {
				setText("新建节点");
			} else {
				setText("新建平级节点");
			}
			if (actionType.equals(AutoCodeManagerPropertyName.AddChildrenTreeNodeName)) {
				new NewTreeCodeNodeDialog(treeViewer.getControl().getShell(), (CreateTreeCodeView) createView, title, false, true).open();
			} else {
				new NewTreeCodeNodeDialog(treeViewer.getControl().getShell(), (CreateTreeCodeView) createView, title, false, false).open();
			}
		}
	}

}
