Newer
Older
org.ntlab.reverseDebugger / org.ntlab.reverseDebugger / src / org / ntlab / reversedebugger / SeedAliasView.java
package org.ntlab.reversedebugger;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.IWorkbenchActionConstants;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.ntlab.debuggingControl.MethodCaller;

public class SeedAliasView extends ViewPart {
	private static TableViewer viewer;
	private IAction refreshAction;
	private IAction resetAction;
	private IAction getObjectFlowAction;
	private MethodCaller selectSeedAlias;
	private static SeedAliases seedAliases = new SeedAliases();
	private static JavaEditorOperator javaEditorOperator = new JavaEditorOperator();
	public static final String ID = "org.ntlab.reverseDebugger.seedAliasView";
		
	public SeedAliasView() {
		// TODO Auto-generated constructor stub
		System.out.println("SeedAliasViewクラスが生成されたよ!");
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		System.out.println("SeedAliasView#createPartControl(Composite)が呼ばれたよ!");
		viewer = new TableViewer(parent, SWT.FULL_SELECTION);
		Table table = viewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);

		// テーブルのカラムを作成
		String[] tableColumnTexts = {"objectID", "objectType", "srcFile", "lineNo", "statementType", "statementSignature", "occurrence"};
		int[] tableColumnWidth = {150, 160, 160, 80, 180, 300, 110};
		TableColumn[] tableColumns = new TableColumn[tableColumnTexts.length];
		for (int i = 0; i < tableColumns.length; i++) {
			tableColumns[i] = new TableColumn(table, SWT.NULL);
			tableColumns[i].setText(tableColumnTexts[i]);
			tableColumns[i].setWidth(tableColumnWidth[i]);
		}
		viewer.setContentProvider(new ArrayContentProvider());
		viewer.setLabelProvider(new SeedAliasLabelProvider());	
		viewer.setInput(seedAliases.getSeedAliases());
		CallStackView.refresh();
		
		// 選択したカラムに対応するシードエイリアスを起点にオブジェクトフローを実行させるリスナーを登録
//		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
//			@Override
//			public void selectionChanged(SelectionChangedEvent event) {
//				IStructuredSelection sel = (IStructuredSelection)event.getSelection();
//				Object element = sel.getFirstElement();
//				if (element instanceof MethodCaller) {
//					MethodCaller seedAlias = (MethodCaller)element;
//					ObjectFlowAliasView.createObjectFlow(seedAlias);
//					ObjectFlowAliasView.refresh();
//				}
//			}
//		});

		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				IStructuredSelection sel = (IStructuredSelection)event.getSelection();
				Object element = sel.getFirstElement();
				if (element instanceof MethodCaller) {
					selectSeedAlias = (MethodCaller)element;
					javaEditorOperator.openSrcFileOfAlias(selectSeedAlias);
				}
			}
		});

		createActions();
		createToolBar();
		createMenuBar();
		createPopupMenu();
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub
		viewer.getControl().setFocus();
//		viewer.refresh();
	}
	
	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));
		
		resetAction = new Action() {
			@Override
			public void run() {
				reset();
			}	
		};
		resetAction.setText("reset");
		resetAction.setToolTipText("reset");
		resetAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_ETOOL_HOME_NAV));
		
		getObjectFlowAction = new Action() {
			@Override
			public void run() {
				getObjectFlow();
			}
		};
		getObjectFlowAction.setText("getObjectFlow");
		getObjectFlowAction.setToolTipText("getObjectFlow");
		getObjectFlowAction.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_OBJS_TASK_TSK));
	}
	
	private void createToolBar() {
		IToolBarManager mgr = getViewSite().getActionBars().getToolBarManager();
		mgr.add(refreshAction);
		mgr.add(resetAction);
	}
	
	private void createMenuBar() {
		IMenuManager mgr = getViewSite().getActionBars().getMenuManager();
		mgr.add(refreshAction);
		mgr.add(resetAction);
	}
	
	private void createPopupMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			@Override
			public void menuAboutToShow(IMenuManager manager) {
				manager.add(getObjectFlowAction);
				manager.add(new Separator(IWorkbenchActionConstants.MB_ADDITIONS));
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	public static MethodCaller getDebuggerStopMethodExecution() {
		return seedAliases.getDebuggerStopMethodExecution();
	}
	
	public static void createSeedAliasesByAlias(MethodCaller alias) {
		seedAliases.createSeedAliasesByAlias(alias);
	}
	
	public static void createSeedAliasesByMethodExecution(MethodCaller methodExecution) {
		seedAliases.createSeedAliasesByMethodExecution(methodExecution);
	}

	public static void refresh() {
		if (seedAliases.getSeedAliases().isEmpty()) {
			seedAliases.initSeedAliases();
			javaEditorOperator.openSrcFileOfMethodExecution(seedAliases.getDebuggerStopMethodExecution());
			CallStackView.reset();
		}
		viewer.refresh();
//		CallStackView.refresh();
	}
	
	private void reset() {
		seedAliases.reset();
		ObjectFlowAliasView.reset();
		refresh();
	}
	
	private void getObjectFlow() {
		if (selectSeedAlias != null) {
			ObjectFlowAliasView.createObjectFlow(selectSeedAlias);
			ObjectFlowAliasView.refresh();			
		}
	}
}