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());
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) {
// 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);
return reason;
}
+ public String getNewMethodName() {
+ return newMethodName;
+ }
+
+ public void setNewMethodName(String newMethodName) {
+ this.newMethodName = newMethodName;
+ }
+
}
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 {
@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
}
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(),
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
+ destroyChanger();
if (preconditionsAreMet(event) && userConfirms(event)) {
try {
modifyWorkspace();
}
};
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;
}