/**
 * @author tyl
 * @Action  ini
 * 2011-4-9
 */
package cn.com.origin.autocode.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 cn.com.origin.autocode.views.AutoCodeItemTypeView;
import cn.com.origin.autocodemanager.common.AutoCodeConstants;
import cn.com.origin.autocodemanager.common.AutoCodeManagerImageKey;
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 CodeViewRightKey extends AbstractTreeViewRightKeyAction {

	/**
	 * @param createTreeView
	 */
	public CodeViewRightKey(AbstractCreateTreeView createTreeView) {
		super(createTreeView);
		// TODO Auto-generated constructor stub
	}

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

	private class addCodeAction extends Action {
		public addCodeAction() {
			setText("添加");
			ImageDescriptor imageDesc = AbstractUIPlugin.imageDescriptorFromPlugin(AutoCodeManangerConstants.AutoCodeMananger_APPLICATION_ID, AutoCodeManagerImageKey.ADD_IMAGE);
			this.setImageDescriptor(imageDesc);
		}

		public void run() {
			AutoCodeItemTypeView codeItemTypeView = (AutoCodeItemTypeView) createView.viewPart.getSite().getPage().findView(AutoCodeConstants.AutoCodeItemTypeView_ID);
			if (codeItemTypeView != null) {
				AbstractTreeData segmentNode = treeViewOperation.getSelectedTreeNode(codeItemTypeView.createView.treeViewer);
				((CreateAutoCodeView) createView).addSegmentNode(codeItemTypeView.createView.treeViewer, segmentNode, selectedTreeData);
			}
		}
	}

}
