package com.kostech.createnewfolder.handlers;

import javax.mail.Message;

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

import com.teamcenter.rac.aif.kernel.InterfaceAIFComponent;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponentFolder;
import com.teamcenter.rac.kernel.TCComponentFolderType;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

public class CreateNewFolder extends AbstractHandler{

	public CreateNewFolder() {
		super();
	}
	//create Menu
	//create folder
	//add ...
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		//get TCsession 
		TCSession session = (TCSession) AIFUtility.getDefaultSession();
		// get choose target
		
		InterfaceAIFComponent target = AIFUtility.getCurrentApplication().getTargetComponent();
		try
		{
			TCComponentFolderType foldertype = (TCComponentFolderType) session.getTypeComponent("Folder");
			//foldername,folderdesc,type
			TCComponentFolder  folder = foldertype.create("TestCreateNewFolder", "test", "Folder");
			//jiazai
			if (target  instanceof TCComponentFolder)
			{
				TCComponentFolder targetFolder = (TCComponentFolder)target;
				targetFolder.add("contents", folder);
			}
			else 
			{
				MessageBox.post("Please choose a Folder", "", "", MessageBox.ERROR);
			}
		} 
		catch (TCException e)
		{
			e.printStackTrace();
		}
		return null;
	}

}
