1 package no.uio.ifi.refaktor.handlers;
3 import no.uio.ifi.refaktor.change.changers.RefaktorChanger;
4 import no.uio.ifi.refaktor.change.exceptions.RefaktorChangerException;
5 import no.uio.ifi.refaktor.debugging.RefaktorDebug;
6 import no.uio.ifi.refaktor.statistics.StatisticsAspect;
7 import no.uio.ifi.refaktor.statistics.StatisticsAspect.Statistics;
9 import org.eclipse.core.commands.AbstractHandler;
10 import org.eclipse.core.commands.ExecutionEvent;
11 import org.eclipse.core.commands.ExecutionException;
12 import org.eclipse.core.resources.IResource;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.NullProgressMonitor;
15 import org.eclipse.jdt.core.IJavaElement;
16 import org.eclipse.jface.dialogs.MessageDialog;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.ITreeSelection;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.handlers.HandlerUtil;
22 public abstract class AbstractSearchBasedExtractAndMoveMethodChangerHandler extends AbstractHandler {
25 public Object execute(ExecutionEvent event) throws ExecutionException {
26 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
27 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
29 assert selection instanceof ITreeSelection;
30 ITreeSelection treeSelection = (ITreeSelection) selection;
31 Object element = treeSelection.getFirstElement();
33 initializeStatistics(element);
35 RefaktorChanger changer = createChanger(element);
37 changer.checkPreconditions();
38 changer.execute(new NullProgressMonitor());
39 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " has been excuted");
41 } catch (RefaktorChangerException e) {
43 } catch (CoreException e) {
46 Statistics snapshot = StatisticsAspect.getSnapshot();
47 RefaktorDebug.println("\n\n" + snapshot);
48 snapshot.generateReportFile();
51 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " did not execute correctly");
55 private void initializeStatistics(Object element) {
56 IResource resource = null;
58 if (element instanceof IJavaElement) {
59 IJavaElement javaElement = (IJavaElement) element;
60 if (javaElement.exists()) {
61 resource = javaElement.getResource();
63 } else if (element instanceof IResource) {
64 resource = (IResource) element;
67 if (resource != null && resource.exists()) {
68 StatisticsAspect.init(resource.getProject().getName());
70 StatisticsAspect.init();
74 protected abstract RefaktorChanger createChanger(Object element);