Newer
Older
org.ntlab.reverseDebugger / org.ntlab.reverseDebugger / src / org / ntlab / reverseDebugger / CallStackView.java
package org.ntlab.reverseDebugger;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeNode;
import org.eclipse.jface.viewers.TreeNodeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.ntlab.onlineAccessor.JDIInstanceMethodCaller;

public class CallStackView extends ViewPart {
	private static TreeViewer viewer;
	private IAction refreshAction;
	private static CallStackModels callStackModels = new CallStackModels();
	private JavaEditorOperator javaEditorOperator = new JavaEditorOperator();
	public static final String ID = "org.ntlab.reverseDebugger.callStackView";
	
	public CallStackView() {
		// TODO Auto-generated constructor stub
		System.out.println("callStackViewクラスが生成されたよ");
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		System.out.println("CallStackView#createPartControl(Composite)が呼ばれたよ!");
		viewer = new TreeViewer(parent);
		viewer.setContentProvider(new TreeNodeContentProvider());
		viewer.setLabelProvider(new CallStackLabelProvider());
		viewer.expandAll();
		
		// 選択したカラムに対応するメソッド実行のソースファイルを開かせるリスナーを登録する
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel = (IStructuredSelection)event.getSelection();
				Object element = sel.getFirstElement();
				if (element instanceof TreeNode) {
					Object value = ((TreeNode)element).getValue();
					if (value instanceof CallStackModel) {
						CallStackModel callStackModel = (CallStackModel)value;
						JDIInstanceMethodCaller methodExecution = callStackModel.getMethodCaller();
						SeedAliasView.createSeedAliasesByMethodExecution(methodExecution);
						SeedAliasView.refresh();
						javaEditorOperator.openSrcFileOfMethodExecution(methodExecution, callStackModel.getCallLineNo());
					}
				}
			}
		});
		createActions();
		createToolBar();
		createMenuBar();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		viewer.getControl().setFocus();
	}
	
	private void createActions() {
		refreshAction = new Action() {
			@Override
			public void run() {
				refresh();
			}
		};
		refreshAction.setText("refresh");
		refreshAction.setToolTipText("refresh");
		refreshAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ELCL_SYNCED));		
	}
	
	private void createToolBar() {
		IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
		mgr.add(refreshAction);
	}
	
	private void createMenuBar() {
		IMenuManager mgr = getViewSite().getActionBars().getMenuManager();
		mgr.add(refreshAction);
	}
	
	public static void updateByAlias(JDIInstanceMethodCaller alias) {
		callStackModels.updateByAlias(alias);
	}

	public static void refresh() {
		if (callStackModels.getCallStackModels().isEmpty()) {
			JDIInstanceMethodCaller currentMe = SeedAliasView.getDebuggingMethodExecution();
			callStackModels.updateByDebuggerStopMethodExecution(currentMe);
		}
		TreeNode[] nodes = callStackModels.getCallStackModelsTreeNodes();
		if (nodes == null || nodes[0] == null) {
			return;
		}
		viewer.setInput(nodes);
		viewer.expandAll();
	}
	
	public static void reset() {
		callStackModels.reset();
		refresh();
	}
}