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

import java.util.List;

import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPart;

import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.ui.views.AbstractContentViewPart;

import cn.net.connor.std.processedit.meopviewfx.ui.MEOPPanel;
import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;

public class MEOPEditView extends AbstractContentViewPart {
	public static final String ID = MEOPEditView.class.getName();
	private Object currentObj = null;
	private MEOPPanel meopPanel;

	@Override
	protected void processEmptySelection(IWorkbenchPart iWorkbenchPart, List<Object> list) {
		if (list == null || list.size() != 1) {
			return;
		}
		final Object target = list.get(0);
		if (target != null && !(target.equals(currentObj))) {
			System.out.println("Select: " + target);
			currentObj = target;
			new Thread(new Runnable() {
				@Override
				public void run() {
					if (meopPanel != null)
						meopPanel.updateSelection(target);
				}
			}).start();
		}
	}

	public MEOPEditView() {
		this.setProcessNullSelectionsEnabled(true);
		this.setUseScrolledForm(false);
		this.setShowHeader(true);
		this.setMultiSelectionSupported(true);
		this.setUISupportsAssociate(false);
		this.setUISupportsSetScope(false);
		this.setUISupportsViewLocking(true);
		this.setUseInitialInputJobEnabled(false);
		this.setProcessSelectionFromSiblingViewEnabled(true);
	}

	@Override
	protected void createContent(Composite parent) {
		System.out.println("Init UI...");
		parent.setLayout(new FillLayout());
		FXCanvas fxCanvas = new FXCanvas(parent, 16777216);
		this.meopPanel = new MEOPPanel(AIFUtility.getCurrentApplication().getDesktop());
		Scene scene = this.meopPanel.getScene();
		System.out.println("Init UI Complete");
		fxCanvas.setScene(scene);
	}

}