package cn.com.origin.autocodemanager.handlers;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import com.teamcenter.rac.aif.AbstractAIFUIApplication;
import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentCfgAttachmentLine;

public class CodeCopyHandler extends AbstractHandler {


	public Object execute(ExecutionEvent execut){
		try {
			AbstractAIFUIApplication abstractAIFUIApplication = AIFUtility.getCurrentApplication();
			String id="";
			InterfaceAIFComponent target=abstractAIFUIApplication.getTargetComponent();
			if(target.getClass().toString().indexOf("TCComponentItem")>=0){
				id=target.getProperty("item_id");
			}else if(target.getClass().toString().indexOf("TCComponentItemRevision")>=0){
				id=target.getProperty("item_id");
			}else if(target.getClass().toString().indexOf("TCComponentBOMLine")>=0){
				TCComponentBOMLine targetLine=(TCComponentBOMLine) target;
				id=targetLine.getItem().getProperty("item_id");
			}else if(target.getClass().toString().indexOf("TCComponentCfgAttachmentLine")>=0){
				TCComponentCfgAttachmentLine cfgLine=(TCComponentCfgAttachmentLine) target;
				id=cfgLine.getRelatedComponent("me_cl_source").getProperty("item_id");
			}else if(target.getClass().toString().indexOf("TCComponentMfgBvrProcess")>=0){
				id=target.getProperty("bl_item_item_id");
			}
			if(!"".equals(id)){
				setClipboard(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	public void setClipboard(String str) {
		StringSelection ss = new StringSelection(str);
		Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
	}

}
