package no.uio.ifi.refaktor.handlers.entrypoints; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.jdt.core.IMethod; import org.eclipse.jdt.internal.core.JavaProject; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ITreeSelection; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.handlers.HandlerUtil; import no.uio.ifi.refaktor.change.exceptions.RefaktorChangerException; import no.uio.ifi.refaktor.debugging.RefaktorDebug; import no.uio.ifi.refaktor.forTests.Refactorer; @SuppressWarnings("restriction") public abstract class RefactorHandler extends AbstractHandler{ @Override public Object execute(ExecutionEvent event) throws ExecutionException { long startTime = System.currentTimeMillis(); ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); assert selection instanceof ITreeSelection; ITreeSelection treeSelection = (ITreeSelection) selection; Object o = treeSelection.getFirstElement(); Refactorer changer; if(o instanceof IMethod && supports((IMethod) o)){ IMethod method = (IMethod) o; changer = getChanger(method); } else if(o instanceof JavaProject && supports(((JavaProject) o).getProject())){ JavaProject jp = (JavaProject) o; IProject project = jp.getProject(); changer = getChanger(project); } else { MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", "This is not supported"); return null; } try { changer.refactor(new NullProgressMonitor()); MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " has been excuted: " + System.currentTimeMillis()); return null; } catch (RefaktorChangerException e) { RefaktorDebug.log(e); } catch (CoreException e) { RefaktorDebug.log(e); }finally{ RefaktorDebug.log("finished executing: " + (System.currentTimeMillis()-startTime) + " millisec"); } MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", changer.getClass() + " did not execute correctly"); return null; } /** * @param m the method that should be changed * @return true if the subclass supports changing this method ({@link getChanger(IMethod method)} returns not null) * false otherwise ({@link getChanger(IMethod method)} returns null) */ protected abstract boolean supports(IMethod m); /** * @param p the project that should be changed * @return true if the subclass supports changing this project ({@link getChanger(IProject project)} returns not null) * false otherwise ({@link getChanger(IProject project)} returns null) */ protected abstract boolean supports(IProject p); /** * @param method Method to be changed * @return The changer to change the method if supports(IMethod m) returns true for subclass, null otherwise */ protected abstract Refactorer getChanger(IMethod method); /** * @param project Project to be changed * @return The changer to change the project if supports(IProject p) returns true for subclass, null otherwise */ protected abstract Refactorer getChanger(IProject project); }