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

import java.util.Vector;

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.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentItem;
import com.teamcenter.rac.kernel.TCComponentMEProcess;
import com.teamcenter.rac.kernel.TCSession;
import com.teamcenter.rac.util.MessageBox;

//创建常规文件夹结构
public class ProcessMultiplexHandler extends AbstractHandler implements IHandler{
	
	public Object execute(ExecutionEvent arg0) throws ExecutionException {
		System.out.println("ProcessBorrowHandler");
		AbstractAIFUIApplication app = AIFUtility.getCurrentApplication();
		TCSession session = (TCSession) app.getSession();
		AIFDesktop desktop = AIFUtility.getActiveDesktop();
		try {
			TCComponent target = (TCComponent)app.getTargetComponent();
			if(!(target instanceof TCComponentBOMLine)) {
				MessageBox.post(desktop, "请选择工艺BOM进行创建", "ERROR", MessageBox.ERROR);
				return null;
			}
			if(((TCComponentBOMLine)target).parent() != null) {
				MessageBox.post(desktop, "请选择工艺BOM进行创建", "ERROR", MessageBox.ERROR);
				return null;
			}
			TCComponentItem item = ((TCComponentBOMLine)target).getItem();
			if(!(item instanceof TCComponentMEProcess)) {
				MessageBox.post(desktop, "请选择工艺BOM进行创建", "ERROR", MessageBox.ERROR);
				return null;
			}
			String[] prefs = new String[] { "Connor_LD_GYJY_Item", "Connor_LD_GYJY_Process", "Connor_LD_GYJY_Borrow", "Connor_LD_GYJY_Prop", "Connor_LD_GYJY_Clear" };
			Vector<String[]> options = new Vector<>();
			for(String s : prefs) {
//				if(s.equals("Connor_AVIC_GYJY_Borrow")) {
//					options.add(new String[] {});
//					continue;
//				}
				String searchOptions[] = session.getPreferenceService().getStringValues(s);
				if(searchOptions == null) {
//					MessageBox.post(desktop, "首选项"+s+"配置不正确", "ERROR", MessageBox.ERROR);
//					return null;
					options.add(new String[] {});
					continue;
				}
				options.add(searchOptions);
			}
			new ProcessMultiplexDialog(session, (TCComponentBOMLine)target, options);
		}catch(Exception e) {
			e.printStackTrace();
			MessageBox.post(desktop, "错误："+e.getMessage(), "ERROR", MessageBox.ERROR);
			return null;
		}
		return null;
	}
}
