From: Erlend Kristiansen Date: Wed, 22 Jan 2014 19:35:07 +0000 (+0100) Subject: Adding command + handler instead of SearchBasedExtractAndMoveMethodAnalysisAction. X-Git-Url: http://git.uio.no/git/?a=commitdiff_plain;h=c0b43d62c31abbe50944d9c6d6a3b9ff34e7610a;p=ifi-stolz-refaktor.git Adding command + handler instead of SearchBasedExtractAndMoveMethodAnalysisAction. This includes creating a TreeSelectionPropertyTester with the property 'elementType' that can be used in visibleWhen-tests in menus. --- diff --git a/software/no.uio.ifi.refaktor/META-INF/MANIFEST.MF b/software/no.uio.ifi.refaktor/META-INF/MANIFEST.MF index 20772ddd..dc616db2 100644 --- a/software/no.uio.ifi.refaktor/META-INF/MANIFEST.MF +++ b/software/no.uio.ifi.refaktor/META-INF/MANIFEST.MF @@ -19,7 +19,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.ui.editors, org.eclipse.jdt.annotation, org.hamcrest.core, - org.junit + org.junit, + org.eclipse.core.expressions Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy Import-Package: org.eclipse.jdt.internal.corext.refactoring, diff --git a/software/no.uio.ifi.refaktor/plugin.xml b/software/no.uio.ifi.refaktor/plugin.xml index 73a82ec6..799a71f4 100644 --- a/software/no.uio.ifi.refaktor/plugin.xml +++ b/software/no.uio.ifi.refaktor/plugin.xml @@ -34,6 +34,10 @@ id="no.uio.ifi.refaktor.commands.treeSelectionInfo" name="Tree Selection Info"> + + @@ -69,6 +73,10 @@ class="no.uio.ifi.refaktor.handlers.TreeSelectionInfoHandler" commandId="no.uio.ifi.refaktor.commands.treeSelectionInfo"> + + @@ -140,6 +148,22 @@ label="Tree Selection Info" style="push"> + + + + + + + + @@ -173,13 +197,6 @@ label="Compute Cyclomatic Complexity" menubarPath="no.uio.ifi.refaktor.menu1/group2"> - - + + + + diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/SearchBasedExtractAndMoveMethodAnalysisHandler.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/SearchBasedExtractAndMoveMethodAnalysisHandler.java new file mode 100644 index 00000000..4280c448 --- /dev/null +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/SearchBasedExtractAndMoveMethodAnalysisHandler.java @@ -0,0 +1,50 @@ +package no.uio.ifi.refaktor.handlers; + +import static no.uio.ifi.refaktor.RefaktorAssert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; +import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodAnalysisResult; +import no.uio.ifi.refaktor.analyze.FavorNoUnfixesAnalysisResultComparator; +import no.uio.ifi.refaktor.analyze.NoTargetFoundException; +import no.uio.ifi.refaktor.analyze.SearchBasedExtractAndMoveMethodAnalyzer; + +import org.eclipse.core.commands.AbstractHandler; +import org.eclipse.core.commands.ExecutionEvent; +import org.eclipse.core.commands.ExecutionException; +import org.eclipse.jdt.core.IMethod; +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; + +public class SearchBasedExtractAndMoveMethodAnalysisHandler extends AbstractHandler { + + @Override + public Object execute(ExecutionEvent event) throws ExecutionException { + ISelection selection = HandlerUtil.getCurrentSelectionChecked(event); + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); + + assertThat(selection, instanceOf(ITreeSelection.class)); + ITreeSelection treeSelection = (ITreeSelection) selection; + Object o = treeSelection.getFirstElement(); + assertThat(o, instanceOf(IMethod.class)); + IMethod method = (IMethod) o; + + String message = ""; + try { + SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(method, new FavorNoUnfixesAnalysisResultComparator()); + analyzer.analyze(); + ExtractAndMoveMethodAnalysisResult result = analyzer.getResult(); + message = "Result:\n\nText selection:\n" + result.textSelection + + "\n\nSelected text:\n" + result.textSelection.getText() + + "\n\nNumber of text selections analyzed:\n" + result.numberOfTextSelectionsAnalyzed + + "\n\nOriginal target:\n" + result.calculateOriginalTarget(); + } catch (NoTargetFoundException e) { + message = "No solution found for " + method; + } + + MessageDialog.openInformation(window.getShell(), "no.uio.ifi.refaktor", message); + return null; + } + +} diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/popup/actions/SearchBasedExtractAndMoveMethodAnalysisAction.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/popup/actions/SearchBasedExtractAndMoveMethodAnalysisAction.java deleted file mode 100644 index e772fd2c..00000000 --- a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/popup/actions/SearchBasedExtractAndMoveMethodAnalysisAction.java +++ /dev/null @@ -1,26 +0,0 @@ -package no.uio.ifi.refaktor.popup.actions; - -import no.uio.ifi.refaktor.analyze.ExtractAndMoveMethodAnalysisResult; -import no.uio.ifi.refaktor.analyze.FavorNoUnfixesAnalysisResultComparator; -import no.uio.ifi.refaktor.analyze.SearchBasedExtractAndMoveMethodAnalyzer; - -import org.eclipse.jface.action.IAction; -import org.eclipse.jface.dialogs.MessageDialog; - -public class SearchBasedExtractAndMoveMethodAnalysisAction extends IMethodAction { - - @Override - public void run(IAction action) { - SearchBasedExtractAndMoveMethodAnalyzer analyzer = new SearchBasedExtractAndMoveMethodAnalyzer(m, new FavorNoUnfixesAnalysisResultComparator()); - analyzer.analyze(); - ExtractAndMoveMethodAnalysisResult result = analyzer.getResult(); - - String message = "Result:\n\nText selection:\n" + result.textSelection - + "\n\nSelected text:\n" + result.textSelection.getText() - + "\n\nNumber of text selections analyzed:\n" + result.numberOfTextSelectionsAnalyzed - + "\n\nOriginal target:\n" + result.calculateOriginalTarget(); - - MessageDialog.openInformation(shell, "no.uio.ifi.refaktor", message); - } - -} diff --git a/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/propertyTesters/TreeSelectionPropertyTester.java b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/propertyTesters/TreeSelectionPropertyTester.java new file mode 100644 index 00000000..1f5c93d2 --- /dev/null +++ b/software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/propertyTesters/TreeSelectionPropertyTester.java @@ -0,0 +1,36 @@ +package no.uio.ifi.refaktor.propertyTesters; + +import static no.uio.ifi.refaktor.RefaktorAssert.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; +import no.uio.ifi.refaktor.utils.RefaktorDebug; + +import org.eclipse.core.expressions.PropertyTester; +import org.eclipse.jface.viewers.ITreeSelection; + +public class TreeSelectionPropertyTester extends PropertyTester{ + + private static final String ELEMENT_TYPE = "elementType"; + + @Override + public boolean test(Object receiver, String property, Object[] args, Object expectedValue) { + assertThat(receiver, instanceOf(ITreeSelection.class)); + ITreeSelection treeSelection = (ITreeSelection) receiver; + + if (property.equals(ELEMENT_TYPE) && expectedValue instanceof String) { + return selectedTypeIsOfExpectedType(treeSelection, (String) expectedValue); + } + + return false; + } + + private boolean selectedTypeIsOfExpectedType(ITreeSelection treeSelection, String expectedTypeName) { + try { + Class expectedClass = Class.forName(expectedTypeName); + return expectedClass.isInstance(treeSelection.getFirstElement()); + } catch (ClassNotFoundException e) { + RefaktorDebug.log(e); + return false; + } + } + +}