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

import java.util.Arrays;

import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;

import com.teamcenter.rac.aif.AbstractAIFApplication;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.cme.time.common.ActivityUtils;
import com.teamcenter.rac.cme.time.common.CommonUtils;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCComponentBOMLine;
import com.teamcenter.rac.kernel.TCComponentCfgActivityLine;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.util.MessageBox;
import com.teamcenter.rac.util.PlatformHelper;

import cn.net.connor.std.aifrcp.KCommand;
import cn.net.connor.std.aifrcp.util.KUtil;

public class OpenViewCommand extends KCommand {

	public OpenViewCommand(AbstractAIFApplication app, String commandId, String actionInfo) {
		super(app, commandId, actionInfo);
	}
	

	@Override
	protected void executeCommand() throws Exception {
		System.out.println("Open View: " + actionInfo);
		if (KUtil.isEmpty(actionInfo)) {
			return;
		}
		Display.getDefault().asyncExec(new Runnable() {
			@Override
			public void run() {
				try {
					IWorkbenchWindow var1 = AIFUtility.getActiveDesktop().getDesktopWindow();
					IWorkbenchPage var2 = var1.getActivePage();
					IViewPart sv = var2.findView(actionInfo);
					if (sv == null) {
						sv = var2.showView(actionInfo);
					}
					PlatformHelper.getCurrentPage().activate(sv);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}


	protected void test() throws Exception{
		TCComponentBOMLine targetLine = (TCComponentBOMLine) app.getTargetComponent();
		TCComponentCfgActivityLine actLine = (TCComponentCfgActivityLine) targetLine.getReferenceProperty("bl_me_activity_lines");
		if (actLine != null) {
			TCComponent[] cc = ActivityUtils.getSortedActivityChildren(actLine);
			System.out.println(">> " + Arrays.toString(cc));
			int len = KUtil.getLen(cc);
			if (len > 1) {
				CommonUtils.getSession().suspendEvent();
				try {
					ActivityUtils.moveActivityBefore(cc[1], cc[0]);
				} catch (TCException tCException) {
					MessageBox.post(tCException);
				} finally {
					CommonUtils.getSession().resumeEvent();
				}
			}
		}
	}
}
