package com.connor.bhdz.pdm.drawingToCapp;

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

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentItemRevision;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;

public class SendDrawingToCappHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		
		AbstractAIFApplication app = AIFUtility.getCurrentApplication();
//		判断选中（多个）对象中是否有非H8_drawing类型的图纸对象
		InterfaceAIFComponent[] components = app.getTargetComponents();
		try {
			if (components != null && components.length > 0) {
				//遍历每一个选中对象
				for (InterfaceAIFComponent component : components) {
					TCComponentItem item;//存放选中对象的item对象
					String componentType = "";//存放选中对象的类型
					if (component instanceof TCComponentItemRevision) {
						TCComponentItemRevision rev = (TCComponentItemRevision) component;
						item = rev.getItem();
						componentType = "rev";
			    		
					}else if (component instanceof TCComponentItem) {
						item = (TCComponentItem) component;
						componentType = "item";
					}else {
						MessageBox.post("请选择图纸对象","提示",MessageBox.INFORMATION);
						return null;
					}
					//判断选中对象是否是H8_drawing
					String typeString = item.getStringProperty("object_type");
					System.out.println("当前选中对象的item类型=="+typeString);
					//判断选中的对象中是否全是是H8_drawing，如果不是返回null
					if (!typeString.equals("H8_drawing") && !typeString.equals("H8_frockItem") && !typeString.equals("H8_standardParts") && !typeString.equals("H8_Component")) {
						 MessageBox.post("请选择图纸对象","提示",MessageBox.INFORMATION);
						 return null;
					}
				}
			}
			//进度条弹窗
			System.out.println("进度条弹窗即将打开");


			System.out.println("》》》即将开始下发");
//			全是H8_drawing类型的对象，才能转发给action
			SendDrawingToCappAction ac = new SendDrawingToCappAction(app, null, null);
			Thread thread = new Thread(ac);
			thread.start();

			System.out.println("》》》下发完成，开始关闭进度条弹窗");
		} catch (TCException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			
		}
		// TODO Auto-generated method stub
		return null;
	}

}
