1 package no.uio.ifi.refaktor.handlers.entrypoints;
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;
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;
21 @SuppressWarnings("restriction")
22 public abstract class RefactorHandler extends AbstractHandler{
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);
30 assert selection instanceof ITreeSelection;
31 ITreeSelection treeSelection = (ITreeSelection) selection;
32 Object o = treeSelection.getFirstElement();
34 if(o instanceof IMethod && supports((IMethod) o)){
35 IMethod method = (IMethod) o;
36 changer = getChanger(method);
38 else if(o instanceof JavaProject && supports(((JavaProject) o).getProject())){
39 JavaProject jp = (JavaProject) o;
40 IProject project = jp.getProject();
41 changer = getChanger(project);
44 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", "This is not supported");
49 changer.refactor(new NullProgressMonitor());
50 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " has been excuted: " + System.currentTimeMillis());
52 } catch (RefaktorChangerException e) {
54 } catch (CoreException e) {
57 RefaktorDebug.log("finished executing: " + (System.currentTimeMillis()-startTime) + " millisec");
60 MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " did not execute correctly");
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)
71 protected abstract boolean supports(IMethod m);
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)
77 protected abstract boolean supports(IProject p);
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
83 protected abstract Refactorer getChanger(IMethod method);
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
88 protected abstract Refactorer getChanger(IProject project);