]> git.uio.no Git - ifi-stolz-refaktor.git/commitdiff
ExtractAndMoveMethodHandler: Adding the possibility for the user to change the name...
authorErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 24 Oct 2013 13:38:58 +0000 (15:38 +0200)
committerErlend Kristiansen <erlenkr@ifi.uio.no>
Thu, 24 Oct 2013 13:38:58 +0000 (15:38 +0200)
software/no.uio.ifi.refaktor.tests/src/no/uio/ifi/refaktor/tests/ExtractAndMoveMethodChangerTest.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/changers/ExtractAndMoveMethodChanger.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/ExtractAndMoveMethodHandler.java
software/no.uio.ifi.refaktor/src/no/uio/ifi/refaktor/handlers/WorkspaceModificationHandler.java

index ab1fa23a0802b31496684b70e873b693fe45a87e..7b8ce2b384e5e0103e0a335c0b7f63c1ef5e547e 100644 (file)
@@ -69,7 +69,7 @@ public class ExtractAndMoveMethodChangerTest {
 
                                IDocument document = new Document(testingInMain.getCompilationUnit().getSource());
                                SmartTextSelection smartTextSelection = new SmartTextSelection(document, offset, length);
-                               ExtractAndMoveMethodChanger changer = new ExtractAndMoveMethodChanger(project, smartTextSelection);
+                               ExtractAndMoveMethodChanger changer = new ExtractAndMoveMethodChanger(project, smartTextSelection, "extractedAndNew");
                                
                                try {
                                        assertTrue(changer.preconditionsAreMet());
index e068fcbff1a936b40c213c2efdd4c303da3c13d8..db0804a5df883ee35847829be07992bd732cc107 100644 (file)
@@ -49,10 +49,20 @@ public class ExtractAndMoveMethodChanger extends RefaktorChanger {
        private SmartTextSelection smartTextSelection;
        private UnionOfLongestCommonPrefixesExtractor extractor;
        private String reason;
+       private String newMethodName;
 
        public ExtractAndMoveMethodChanger(IProject project, SmartTextSelection smartTextSelection) {
+               this(project, smartTextSelection, generateName());
+       }
+
+       public static String generateName() {
+               return "generated_" + Math.abs(new Random().nextLong());
+       }
+       
+       public ExtractAndMoveMethodChanger(IProject project, SmartTextSelection smartTextSelection, String newMethodName) {
                this.project = project;
                this.smartTextSelection = smartTextSelection;
+               this.setNewMethodName(newMethodName);
                try {
                        initializeExtractor();
                } catch (ExecutionException e) {
@@ -162,8 +172,7 @@ public class ExtractAndMoveMethodChanger extends RefaktorChanger {
                //              ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(cu, prefix.statementStartPosition(), prefix.statementLength());
                ExtractMethodRefactoring refactoring = new ExtractMethodRefactoring(cu, extractor.getSelection().getOffset(), extractor.getSelection().getLength());
 
-               String name = "generated_" + Math.abs(new Random().nextLong());
-               refactoring.setMethodName(name);
+               refactoring.setMethodName(getNewMethodName());
                refactoring.setValidationContext(null);
                refactoring.setReplaceDuplicates(true);
                refactoring.setVisibility(Modifier.PUBLIC);
@@ -220,4 +229,12 @@ public class ExtractAndMoveMethodChanger extends RefaktorChanger {
                return reason;
        }
 
+       public String getNewMethodName() {
+               return newMethodName;
+       }
+
+       public void setNewMethodName(String newMethodName) {
+               this.newMethodName = newMethodName;
+       }
+
 }
index 831bb499c3f4d02f17b8ba725031b4f79dde52db..edd9864e7b6fd701e93a399c2442f22612d0e38c 100644 (file)
@@ -9,9 +9,11 @@ import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IProject;
+import org.eclipse.jface.dialogs.InputDialog;
 import org.eclipse.jface.dialogs.MessageDialog;
 import org.eclipse.ui.IEditorInput;
 import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.IWorkbenchWindow;
 import org.eclipse.ui.handlers.HandlerUtil;
 
 public class ExtractAndMoveMethodHandler extends WorkspaceModificationHandler {
@@ -21,12 +23,12 @@ public class ExtractAndMoveMethodHandler extends WorkspaceModificationHandler {
 
        @Override
        protected String getTitle() {
-               return "Extract and move method";
+               return "Extract and Move Method";
        }
 
        @Override
        protected String getMessage() {
-               return "Go through with this?";
+               return "Name of extracted method:";
        }
 
        @Override
@@ -46,6 +48,17 @@ public class ExtractAndMoveMethodHandler extends WorkspaceModificationHandler {
                }
                return preconditionsAreMet;
        }
+       
+       @Override
+       protected boolean userConfirms(ExecutionEvent event)
+                       throws ExecutionException {
+               IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
+               InputDialog dialog = new InputDialog(window.getShell(), getTitle(), getMessage(), ExtractAndMoveMethodChanger.generateName(), null);
+               int returnCode = dialog.open();
+               String newMethodName = dialog.getValue().trim();
+               ((ExtractAndMoveMethodChanger) getChanger()).setNewMethodName(newMethodName);
+               return returnCode == InputDialog.OK;
+       }
 
        private void giveMessage(String reason, ExecutionEvent event) throws ExecutionException {
                MessageDialog.openInformation(HandlerUtil.getActiveWorkbenchWindowChecked(event).getShell(), 
index 31f25dcdd1c1362177ef6631ae6a092f55549fee..6ba6dfdbb82e8b91afb66d67f2496a8706ba7128 100644 (file)
@@ -30,6 +30,7 @@ public abstract class WorkspaceModificationHandler extends AbstractHandler {
        
        @Override
        public Object execute(ExecutionEvent event) throws ExecutionException {
+               destroyChanger();
                if (preconditionsAreMet(event) && userConfirms(event)) {
                        try {
                                modifyWorkspace();
@@ -62,15 +63,14 @@ public abstract class WorkspaceModificationHandler extends AbstractHandler {
                        }
                };
                op.run(new NullProgressMonitor());
-               destroyChanger();
        }
 
-       private boolean userConfirms(ExecutionEvent event) throws ExecutionException {
+       protected boolean userConfirms(ExecutionEvent event) throws ExecutionException {
                IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
                return MessageDialog.openQuestion(window.getShell(), getTitle(), getMessage());
        }
        
-       private void destroyChanger() {
+       protected void destroyChanger() {
                changer = null;
        }