]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/AbstractSearchBasedExtractAndMoveMethodChangerHandler.java
now also works with blockless constructs (if, for, etc)
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / handlers / AbstractSearchBasedExtractAndMoveMethodChangerHandler.java
1 package no.uio.ifi.refaktor.handlers;
2
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;
8
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;
21
22 public abstract class AbstractSearchBasedExtractAndMoveMethodChangerHandler extends AbstractHandler {
23
24         @Override
25         public Object execute(ExecutionEvent event) throws ExecutionException {
26                 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
27                 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
28         
29                 assert selection instanceof ITreeSelection;
30                 ITreeSelection treeSelection = (ITreeSelection) selection;
31                 Object element = treeSelection.getFirstElement();
32                 
33                 initializeStatistics(element);
34         
35                 RefaktorChanger changer = createChanger(element);
36                 try {
37                         changer.checkPreconditions();
38                         changer.execute(new NullProgressMonitor());
39                         MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " has been excuted");
40                         return null;
41                 } catch (RefaktorChangerException e) {
42                         RefaktorDebug.log(e);
43                 } catch (CoreException e) {
44                         RefaktorDebug.log(e);
45                 } finally {
46                         Statistics snapshot = StatisticsAspect.getSnapshot();
47                         RefaktorDebug.println("\n\n" + snapshot);
48                         snapshot.generateReportFile();
49                 }
50         
51                 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " did not execute correctly");
52                 return null;
53         }
54
55         private void initializeStatistics(Object element) {
56                 IResource resource = null;
57                 
58                 if (element instanceof IJavaElement) {
59                         IJavaElement javaElement = (IJavaElement) element;
60                         if (javaElement.exists()) {
61                                 resource = javaElement.getResource();
62                         }
63                 } else if (element instanceof IResource) {
64                         resource = (IResource) element;
65                 }
66                 
67                 if (resource != null && resource.exists()) {
68                         StatisticsAspect.init(resource.getProject().getName());
69                 } else {
70                         StatisticsAspect.init();
71                 }
72         }
73
74         protected abstract RefaktorChanger createChanger(Object element);
75
76 }