/**
 * @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.Display;
import org.eclipse.swt.widgets.FileDialog;
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.imports.ImportNodeOperation;
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 ImportNodeHandler 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);
				FileDialog fileDialog = new FileDialog(Display.getDefault().getActiveShell());
				fileDialog.setText("导入");
				fileDialog.setFilterNames(new String[] { "*.xls" });
				fileDialog.setFilterExtensions(new String[] { "*.xls" });
				fileDialog.open();
				String filePath = fileDialog.getFilterPath();
				String fileName = fileDialog.getFileName();
				// System.out.println("所选择的物理文件路径是:" + filePath);
				if (filePath != null && !"".equals(filePath) && fileName != null && !"".equals(fileName)) {
					filePath = filePath + "/" + fileName;
				} else {
					MessageBox.post("请选择导入文件！", "INFO", MessageBox.WARNING);
					return null;
				}
				session.setStatus("正在导入......");
				new ImportNodeOperation(treeViewPart, selectedNode, filePath).importNode();
				session.setReadyStatus();
				MessageBox.post("导入成功!", "INFO", MessageBox.WARNING);
			}
		}
		return null;
	}
}
