package interactivefeaturelocator.views;

import interactivefeaturelocator.model.DerivationSession;
import interactivefeaturelocator.model.CallTreeGraphConnection;
import interactivefeaturelocator.model.CallTreeGraphLayoutAlgorithm;
import interactivefeaturelocator.model.CallTreeGraphLayoutAlgorithm;
import interactivefeaturelocator.model.CallTreeGraphNode;

import org.eclipse.core.resources.IFile;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.WorkbenchWindow;
import org.eclipse.zest.core.widgets.Graph;
import org.eclipse.zest.core.widgets.IContainer;
import org.eclipse.zest.core.widgets.ZestStyles;
import org.ntlab.trace.MethodExecution;

public class CallTreeGraph extends Graph {
	private CallTreeGraphNode currentNode;

	public CallTreeGraph(Composite parent, int style) {
		super(parent, style);
		// TODO Auto-generated constructor stub
	}

	public void createGraph(DerivationSession session) {
//		CallTreeGraphNode n1 = new CallTreeGraphNode(this, SWT.NONE, "ノード1");
//		CallTreeGraphNode n2 = new CallTreeGraphNode(this, SWT.NONE, "ノード2");
//		CallTreeGraphNode n3 = new CallTreeGraphNode(this, SWT.NONE, "ノード3");
		
		CallTreeGraphNode node = createTreeGraphNode(this, SWT.NONE, session.getCurrentMethodExecution());
		setCurrentNode(node);
//		new CallTreeGraphConnection(this, ZestStyles.CONNECTIONS_DIRECTED, n1, n2);
//		new CallTreeGraphConnection(this, SWT.NONE, n2, n3);
		this.setLayoutAlgorithm(new CallTreeGraphLayoutAlgorithm(ZestStyles.NODES_NO_LAYOUT_RESIZE), true);
	}
	
	public CallTreeGraphNode createTreeGraphNode(IContainer graph, int style, MethodExecution method) {
		CallTreeGraphNode node = new CallTreeGraphNode(this, SWT.NONE, method);
		applyLayout();
		return node;
	}
	
	public CallTreeGraphNode createCallerTreeGraphNode(CallTreeGraphNode calleeNode) {
		MethodExecution parent = calleeNode.getMethod().getCallerMethodExecution();
		if (parent == null) return null;
		CallTreeGraphNode callerNode = createTreeGraphNode(this, SWT.NONE, parent);
		setCurrentNode(callerNode);
		new CallTreeGraphConnection(this, ZestStyles.CONNECTIONS_DIRECTED, callerNode, calleeNode);
		applyLayout();
		return callerNode;
	}
	
	public void openFile() {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
//		IEditorPart editorPart = page.openEditor
		
	}
	
	public CallTreeGraphNode getCurrentNode() {
		return currentNode;
	}
	public void setCurrentNode(CallTreeGraphNode currentNode) {
		this.currentNode = currentNode;
	}
}
