/**
 * @author tyl
 * @Action  ini
 * 2011-3-22
 */
package cn.com.origin.autocodemanager.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.expressions.IEvaluationContext;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISources;
import cn.com.origin.autocodemanager.common.AbstractTreeViewPart;
import cn.com.origin.autocodemanager.common.tree.AbstractCreateTreeView;
import cn.com.origin.autocodemanager.common.tree.AbstractTreeData;
import cn.com.origin.autocodemanager.common.tree.TreeViewOperation;
import cn.com.origin.autocodemanager.export.ExportNodeOperation;

import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class ExportNodeHandler extends AbstractHandler {
	public Object execute(ExecutionEvent e) throws ExecutionException {
		AbstractAIFUIApplication abstractAIFUIApplication = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) abstractAIFUIApplication.getSession();
		Object obj = e.getApplicationContext();
		if (obj instanceof IEvaluationContext) {
			IEvaluationContext appContext = (IEvaluationContext) obj;
			Object view = appContext.getVariable(ISources.ACTIVE_PART_NAME);
			if (view instanceof AbstractTreeViewPart) {
				AbstractTreeViewPart treeViewPart = (AbstractTreeViewPart) view;
				AbstractCreateTreeView treeView = treeViewPart.getCreateView();
				AbstractTreeData selectedNode = new TreeViewOperation().getSelectedTreeNode(treeView.treeViewer);
				// TreeViewOperation treeViewOperation=new TreeViewOperation();
				DirectoryDialog directoryDialog = new DirectoryDialog(Display.getDefault().getActiveShell());
				directoryDialog.setText("导出");
				String filePath = directoryDialog.open();
				//String filePath = directoryDialog.getFilterPath();
				System.out.println("所选择的物理文件路径是:" + filePath);
				if (filePath != null && !"".equals(filePath)) {
					filePath = filePath + "\\" + selectedNode.getPackCodeNodeInfo().getNode_name() + ".xls";
				} else {
					MessageBox.post("请选择路径！", "INFO", MessageBox.WARNING);
					return null;
				}
				session.setStatus("正在导出......");
				new ExportNodeOperation(treeViewPart, selectedNode, filePath).exportNode();
				session.setReadyStatus();
			}
		}
		return null;
	}

}
