package cn.net.connor.std.processedit.ldy;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;

import com.teamcenter.rac.aif.AIFDesktop;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCAccessControlService;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

//创建常规文件夹结构
public class CutterWordTimeHandler extends AbstractHandler implements IHandler{
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("CutterWordTimeHandler");
//		new Thread() {
//			public void run() {
//			}
//		}.start();
		AbstractAIFUIApplication app = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) app.getSession();
		InterfaceAIFComponent target = app.getTargetComponent();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			if(!(target instanceof TCComponentBOMLine)) {
				MessageBox.post(desktop, "请选择工序BOM进行操作", "ERROR", MessageBox.ERROR);
				return null;
			}
			TCComponentItemRevision rev = ((TCComponentBOMLine)target).getItemRevision();
			if(!rev.isTypeOf("LD6_MEOPRevision")) {
				MessageBox.post(desktop, "请选择工序BOM进行操作", "ERROR", MessageBox.ERROR);
				return null;
			}
			TCAccessControlService accessControlService = session.getTCAccessControlService();
			if(!accessControlService.checkUsersPrivilege(session.getUser(), rev, TCAccessControlService.WRITE)) {
				MessageBox.post(desktop, "当前用户对"+rev+"没有写权限，无法执行命令", "ERROR", MessageBox.ERROR);
				return null;
			}
			
			CutterWordTimeOperation operation = new CutterWordTimeOperation(session, (TCComponentBOMLine)target);
			session.queueOperation(operation);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
}
