]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
Adding command + handler instead of SearchBasedExtractAndMoveMethodAnalysisAction.
authorErlend Kristiansen <erlend.k@gmail.com>
Wed, 22 Jan 2014 19:35:07 +0000 (20:35 +0100)
committerErlend Kristiansen <erlend.k@gmail.com>
Wed, 22 Jan 2014 19:35:07 +0000 (20:35 +0100)
This includes creating a TreeSelectionPropertyTester with the property
'elementType' that can be used in visibleWhen-tests in menus.

software/no.uio.ifi.refaktor/META-INF/MANIFEST.MF
software/no.uio.ifi.refaktor/plugin.xml
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/SearchBasedExtractAndMoveMethodAnalysisHandler.java [new file with mode: 0644]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/popup/actions/SearchBasedExtractAndMoveMethodAnalysisAction.java [deleted file]
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/propertyTesters/TreeSelectionPropertyTester.java [new file with mode: 0644]

index 20772ddd871edaf99991821824434e17cdb7939f..dc616db23b3530f35d2f8630f2c67e0114d025e3 100644 (file)
@@ -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,
index 73a82ec6350124b5d866d1251763365d50ea8e5d..799a71f4f515dae41cced95039862b13611e7aef 100644 (file)
             id="no.uio.ifi.refaktor.commands.treeSelectionInfo"
             name="Tree Selection Info">
       </command>
+      <command
+            id="no.uio.ifi.refaktor.commands.searchBasedExtractAndMoveMethodAnalysis"
+            name="Search Based Extract And Move Method Analysis">
+      </command>
    </extension>
    <extension
          point="org.eclipse.ui.handlers">
             class="no.uio.ifi.refaktor.handlers.TreeSelectionInfoHandler"
             commandId="no.uio.ifi.refaktor.commands.treeSelectionInfo">
       </handler>
+      <handler
+            class="no.uio.ifi.refaktor.handlers.SearchBasedExtractAndMoveMethodAnalysisHandler"
+            commandId="no.uio.ifi.refaktor.commands.searchBasedExtractAndMoveMethodAnalysis">
+      </handler>
    </extension>
    <extension
          point="org.eclipse.ui.menus">
                   label="Tree Selection Info"
                   style="push">
             </command>
+            <command
+                  commandId="no.uio.ifi.refaktor.commands.searchBasedExtractAndMoveMethodAnalysis"
+                  label="Search Based Extract And Move Method Analysis"
+                  style="push">
+               <visibleWhen
+                     checkEnabled="false">
+                  <with
+                        variable="selection">
+                     <test
+                           forcePluginActivation="true"
+                           property="no.uio.ifi.refaktor.propertyTesters.treeSelectionPropertyTester.elementType"
+                           value="org.eclipse.jdt.core.IMethod">
+                     </test>
+                  </with>
+               </visibleWhen>
+            </command>
          </menu>
       </menuContribution>
    </extension>
                label="Compute Cyclomatic Complexity"
                menubarPath="no.uio.ifi.refaktor.menu1/group2">
          </action>
-         <action
-               class="no.uio.ifi.refaktor.popup.actions.SearchBasedExtractAndMoveMethodAnalysisAction"
-               enablesFor="1"
-               id="no.uio.ifi.refaktor.searchBasedExtractAndMoveMethodAnalysis"
-               label="Search Based Extract and Move Method Analysis"
-               menubarPath="no.uio.ifi.refaktor.menu1/group2">
-         </action>
       </objectContribution>
    </extension>
    <extension
          </view>
       </perspectiveExtension>
    </extension>
+   <extension
+         point="org.eclipse.core.expressions.propertyTesters">
+      <propertyTester
+            class="no.uio.ifi.refaktor.propertyTesters.TreeSelectionPropertyTester"
+            id="no.uio.ifi.refaktor.propertyTesters.treeSelectionPropertyTester"
+            namespace="no.uio.ifi.refaktor.propertyTesters.treeSelectionPropertyTester"
+            properties="elementType"
+            type="org.eclipse.jface.viewers.ITreeSelection">
+      </propertyTester>
+   </extension>
 
 </plugin>
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 (file)
index 0000000..4280c44
--- /dev/null
@@ -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 (file)
index e772fd2..0000000
+++ /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 (file)
index 0000000..1f5c93d
--- /dev/null
@@ -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;
+               }
+       }
+
+}