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

import java.awt.Frame;
import java.util.List;

import javax.swing.JApplet;

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

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

import cn.net.connor.std.processedit.util.ProcessUtil;

public class ProcessPropertyView extends AbstractContentViewPart {
	public static final String ID = ProcessPropertyView.class.getName();
	private ProcessPropertyEditor panel;
	private Object currentObj = null;
	
	@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() {
					panel.updateSelection(target);
				}
			}).start();
		}
	}

	public ProcessPropertyView() {
		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());
		Composite swt_compostite = new Composite(parent, 16777216);
		Frame awt_frame = SWT_AWT.new_Frame(swt_compostite);
		JApplet applet = new JApplet();
		this.panel=new ProcessPropertyEditor();
		try {
			this.panel.initUI();
		} catch (Exception e) {
			ProcessUtil.errorSWT(this.getViewSite().getShell(), e.getMessage(), "");
			e.printStackTrace();
			return;
		}
		applet.add(panel);
		awt_frame.add(applet);
		System.out.println("Init UI Complete");
	}

}