]> git.uio.no Git - ifi-stolz-refaktor.git/blob - software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/entrypoints/RefactorHandler.java
removing obsolite code and menus
[ifi-stolz-refaktor.git] / software / no.uio.ifi.refaktor / src / no / uio / ifi / refaktor / handlers / entrypoints / RefactorHandler.java
1 package no.uio.ifi.refaktor.handlers.entrypoints;
2
3 import org.eclipse.core.commands.AbstractHandler;
4 import org.eclipse.core.commands.ExecutionEvent;
5 import org.eclipse.core.commands.ExecutionException;
6 import org.eclipse.core.resources.IProject;
7 import org.eclipse.core.runtime.CoreException;
8 import org.eclipse.core.runtime.NullProgressMonitor;
9 import org.eclipse.jdt.core.IMethod;
10 import org.eclipse.jdt.internal.core.JavaProject;
11 import org.eclipse.jface.dialogs.MessageDialog;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.jface.viewers.ITreeSelection;
14 import org.eclipse.ui.IWorkbenchWindow;
15 import org.eclipse.ui.handlers.HandlerUtil;
16
17 import no.uio.ifi.refaktor.change.exceptions.RefaktorChangerException;
18 import no.uio.ifi.refaktor.debugging.RefaktorDebug;
19 import no.uio.ifi.refaktor.forTests.Refactorer;
20
21 @SuppressWarnings("restriction")
22 public abstract class RefactorHandler extends AbstractHandler{
23
24         @Override
25         public Object execute(ExecutionEvent event) throws ExecutionException {
26                 long startTime = System.currentTimeMillis();
27                 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
28                 IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
29
30                 assert selection instanceof ITreeSelection;
31                 ITreeSelection treeSelection = (ITreeSelection) selection;
32                 Object o = treeSelection.getFirstElement();
33                 Refactorer changer;
34                 if(o instanceof IMethod && supports((IMethod) o)){
35                         IMethod method = (IMethod) o;
36                         changer = getChanger(method);
37                 }
38                 else if(o instanceof JavaProject && supports(((JavaProject) o).getProject())){
39                         JavaProject jp = (JavaProject) o;
40                         IProject project = jp.getProject();
41                         changer = getChanger(project);
42                 }
43                 else {
44                         MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", "This is not supported");
45                         return null;
46                 }
47                 
48                 try {
49                         changer.refactor(new NullProgressMonitor());
50                         MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " has been excuted: " + System.currentTimeMillis());
51                         return null;
52                 } catch (RefaktorChangerException e) {
53                         RefaktorDebug.log(e);
54                 } catch (CoreException e) {
55                         RefaktorDebug.log(e);
56                 }finally{
57                         RefaktorDebug.log("finished executing: " + (System.currentTimeMillis()-startTime) + " millisec");
58                 }
59                 
60                 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " did not execute correctly");
61                 return null;
62         }
63         
64
65
66         /**
67          * @param m the method that should be changed
68          * @return true if the subclass supports changing this method ({@link getChanger(IMethod method)} returns not null)
69          *                      false otherwise ({@link getChanger(IMethod method)} returns null)
70          */
71         protected abstract boolean supports(IMethod m);
72         /**
73          * @param p the project that should be changed
74          * @return true if the subclass supports changing this project ({@link getChanger(IProject project)} returns not null)
75          *                      false otherwise ({@link getChanger(IProject project)} returns null)
76          */
77         protected abstract boolean supports(IProject p);
78
79         /**
80          * @param method Method to be changed
81          * @return The changer to change the method if supports(IMethod m) returns true for subclass, null otherwise
82          */
83         protected abstract Refactorer getChanger(IMethod method);
84         /**
85          * @param project Project to be changed
86          * @return The changer to change the project if supports(IProject p) returns true for subclass, null otherwise
87          */
88         protected abstract Refactorer getChanger(IProject project);
89 }