[Pydev-cvs] org.python.pydev/src/org/python/pydev/editor/actions/refactoring PyExtractMethod.java,1.
Brought to you by:
fabioz
From: Fabio Z. <fa...@us...> - 2004-09-16 15:33:45
|
Update of /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/refactoring In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv26116/src/org/python/pydev/editor/actions/refactoring Modified Files: PyExtractMethod.java PyRefactorAction.java PyRename.java Log Message: Making refactoring. Index: PyRename.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/refactoring/PyRename.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyRename.java 15 Sep 2004 17:36:08 -0000 1.1 --- PyRename.java 16 Sep 2004 15:33:32 -0000 1.2 *************** *** 21,25 **** * renameByCoordinates(filename, line, column, newname) */ ! protected void perform(IAction action) throws Exception { File editorFile = getPyEdit().getEditorFile(); --- 21,25 ---- * renameByCoordinates(filename, line, column, newname) */ ! protected String perform(IAction action, String name, Operation operation) throws Exception { File editorFile = getPyEdit().getEditorFile(); *************** *** 28,41 **** int beginCol = getStartCol(); ! String name = getInput(getPyEdit(),"Please inform the new name."); ! if(name.equals("") == false){ ! PyRefactoring.getPyRefactoring().rename(editorFile, beginLine, beginCol, name); ! refreshEditor(getPyEdit()); } ! } } --- 28,46 ---- int beginCol = getStartCol(); ! String res = ""; if(name.equals("") == false){ ! res = PyRefactoring.getPyRefactoring().rename(editorFile, beginLine, beginCol, name, operation); ! refreshEditors(getPyEdit()); } ! return res; ! } ! ! protected String getInputMessage() { ! return "Please inform the new name."; } + + } Index: PyExtractMethod.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/refactoring/PyExtractMethod.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyExtractMethod.java 15 Sep 2004 17:36:08 -0000 1.1 --- PyExtractMethod.java 16 Sep 2004 15:33:32 -0000 1.2 *************** *** 29,33 **** * @throws CoreException */ ! protected void perform(IAction action) throws BadLocationException, CoreException { File editorFile = getPyEdit().getEditorFile(); --- 29,33 ---- * @throws CoreException */ ! protected String perform(IAction action, String name, Operation operation) throws BadLocationException, CoreException { File editorFile = getPyEdit().getEditorFile(); *************** *** 39,53 **** int endCol = getEndCol(); ! String name = getInput(getPyEdit(),"Please inform the new name."); ! if(name.equals("") == false){ ! PyRefactoring.getPyRefactoring().extract(editorFile, beginLine, beginCol, endLine, endCol, name); ! refreshEditor(getPyEdit()); } } ! --- 39,56 ---- int endCol = getEndCol(); ! String res = ""; if(name.equals("") == false){ ! res = PyRefactoring.getPyRefactoring().extract(editorFile, beginLine, beginCol, endLine, endCol, name, operation); ! refreshEditors(getPyEdit()); } + return res; } ! protected String getInputMessage() { ! return "Please inform the new name."; ! } ! Index: PyRefactorAction.java =================================================================== RCS file: /cvsroot/pydev/org.python.pydev/src/org/python/pydev/editor/actions/refactoring/PyRefactorAction.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** PyRefactorAction.java 15 Sep 2004 17:36:08 -0000 1.1 --- PyRefactorAction.java 16 Sep 2004 15:33:32 -0000 1.2 *************** *** 6,22 **** --- 6,35 ---- package org.python.pydev.editor.actions.refactoring; + import java.lang.reflect.InvocationTargetException; + import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; + import org.eclipse.core.runtime.IProgressMonitor; + import org.eclipse.core.runtime.IStatus; + import org.eclipse.core.runtime.Status; import org.eclipse.jface.action.IAction; + import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.jface.dialogs.InputDialog; import org.eclipse.jface.dialogs.MessageDialog; + import org.eclipse.jface.dialogs.ProgressMonitorDialog; + import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.IEditorPart; + import org.eclipse.ui.IEditorReference; + import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; + import org.eclipse.ui.actions.WorkspaceModifyOperation; import org.eclipse.ui.part.FileEditorInput; import org.python.pydev.editor.PyEdit; import org.python.pydev.editor.actions.PyAction; import org.python.pydev.editor.actions.PySelection; + import org.python.pydev.editor.refactoring.PyRefactoring; + import org.python.pydev.plugin.PydevPlugin; /** *************** *** 25,28 **** --- 38,73 ---- public abstract class PyRefactorAction extends PyAction { + private IWorkbenchWindow workbenchWindow; + + public final class Operation extends WorkspaceModifyOperation { + public String statusOfOperation; + + private final String nameUsed; + + private final IAction action; + + public IProgressMonitor monitor; + + public Operation(String nameUsed, IAction action) { + super(); + this.nameUsed = nameUsed; + this.action = action; + } + + protected void execute(IProgressMonitor monitor) throws CoreException, + InvocationTargetException, InterruptedException { + + try { + this.monitor = monitor; + monitor.beginTask("Refactor", 500); + this.statusOfOperation = perform(action, nameUsed, this); + monitor.done(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + } + /** * @param edit *************** *** 30,36 **** */ protected String getInput(PyEdit edit, String msg) { ! InputDialog d = new InputDialog(edit.getSite().getShell(),"Refactoring", msg,"",null); int retCode = d.open(); ! if(retCode == InputDialog.OK){ return d.getValue(); } --- 75,82 ---- */ protected String getInput(PyEdit edit, String msg) { ! InputDialog d = new InputDialog(getPyEditShell(), "Refactoring", msg, ! "", null); int retCode = d.open(); ! if (retCode == InputDialog.OK) { return d.getValue(); } *************** *** 38,48 **** } /** * @param edit * @throws CoreException */ ! protected void refreshEditor(PyEdit edit) throws CoreException { ! IFile file = (IFile) ((FileEditorInput)edit.getEditorInput()).getAdapter(IFile.class); ! file.refreshLocal(IResource.DEPTH_INFINITE, null); } --- 84,117 ---- } + private void refreshEditor(PyEdit edit) throws CoreException { + IFile file = (IFile) ((FileEditorInput) edit.getEditorInput()) + .getAdapter(IFile.class); + file.refreshLocal(IResource.DEPTH_INFINITE, null); + } + /** * @param edit * @throws CoreException */ ! protected void refreshEditors(PyEdit edit) throws CoreException { ! refreshEditor(edit); ! ! ! ! IWorkbenchPage[] pages = workbenchWindow.getPages(); ! for (int i = 0; i < pages.length; i++) { ! IEditorReference[] editorReferences = pages[i] ! .getEditorReferences(); ! ! for (int j = 0; j < editorReferences.length; j++) { ! if (editorReferences[j] instanceof PyEdit) { ! PyEdit e = (PyEdit) editorReferences[j]; ! if (e != edit) { ! System.out.println("REFRESHING OTHER"); ! refreshEditor(e); ! } ! } ! } ! } } *************** *** 51,68 **** */ protected boolean areRefactorPreconditionsOK(PyEdit edit) { ! IWorkbenchWindow workbenchWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); ! IEditorPart[] dirtyEditors = workbenchWindow.getActivePage().getDirtyEditors(); ! boolean saveEditors = false; ! if(dirtyEditors.length > 0){ ! saveEditors = MessageDialog.openQuestion(edit.getSite().getShell(), "Save All?", "All the editors must be saved to make this operation.\nIs it ok to save them?"); ! if(saveEditors==false){ return false; } ! } ! ! if(saveEditors){ ! boolean editorsSaved = workbenchWindow.getActivePage().saveAllEditors(false); ! if (!editorsSaved){ return false; } --- 120,142 ---- */ protected boolean areRefactorPreconditionsOK(PyEdit edit) { ! workbenchWindow = PlatformUI.getWorkbench() ! .getActiveWorkbenchWindow(); ! IEditorPart[] dirtyEditors = workbenchWindow.getActivePage() ! .getDirtyEditors(); ! boolean saveEditors = false; ! if (dirtyEditors.length > 0) { ! saveEditors = MessageDialog ! .openQuestion(getPyEditShell(), "Save All?", ! "All the editors must be saved to make this operation.\nIs it ok to save them?"); ! if (saveEditors == false) { return false; } ! } ! ! if (saveEditors) { ! boolean editorsSaved = workbenchWindow.getActivePage() ! .saveAllEditors(false); ! if (!editorsSaved) { return false; } *************** *** 84,88 **** */ protected int getEndLine() { ! return ps.endLineIndex+1; } --- 158,162 ---- */ protected int getEndLine() { ! return ps.endLineIndex + 1; } *************** *** 98,135 **** */ protected int getStartLine() { ! return ps.startLineIndex+1; } ! /* (non-Javadoc) * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ ! public void run(IAction action) { ! System.out.println("PyExtractMethod "); ! try ! { ! // Select from text editor ! ps = new PySelection ( getTextEditor ( ), false ); ! ! if(areRefactorPreconditionsOK(getPyEdit())==false){ ! return; ! } ! // Perform the action ! perform ( action ); ! // Put cursor at the first area of the selection ! getTextEditor ( ).selectAndReveal ( ps.endLine.getOffset ( ), 0 ); ! } ! catch ( Exception e ) ! { ! beep ( e ); ! } } /** * @param action */ ! protected abstract void perform(IAction action) throws Exception; ! } --- 172,266 ---- */ protected int getStartLine() { ! return ps.startLineIndex + 1; } ! /* ! * (non-Javadoc) ! * * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction) */ ! public void run(final IAction action) { ! // Select from text editor ! ps = new PySelection(getTextEditor(), false); ! if (areRefactorPreconditionsOK(getPyEdit()) == false) { ! return; ! } ! String msg = getInputMessage(); ! String name = ""; ! if (msg != null) ! name = getInput(getPyEdit(), msg); ! ! final String nameUsed = name; ! Operation operation = new Operation(nameUsed, action); ! ! ProgressMonitorDialog monitorDialog = new ProgressMonitorDialog( ! getPyEditShell()); ! monitorDialog.setBlockOnOpen(false); ! try { ! monitorDialog.run(true, false, operation); ! // Perform the action ! } catch (InvocationTargetException e) { ! e.printStackTrace(); ! } catch (InterruptedException e) { ! e.printStackTrace(); ! } ! ! if (operation.statusOfOperation.startsWith("ERROR:")) { ! restartRefactorShell(); ! String[] strings = operation.statusOfOperation.split("DETAILS:"); ! if (strings.length == 2) { ! IStatus status = new Status(IStatus.ERROR, PydevPlugin ! .getPluginID(), 0, strings[0], ! new Exception(strings[0])); ! ErrorDialog.openError(getPyEditShell(), "ERROR", strings[0], ! status); ! } else { ! MessageDialog.openError(getPyEditShell(), "ERROR", ! operation.statusOfOperation); ! } ! throw new RuntimeException(strings[1]); ! } ! ! // Put cursor at the first area of the selection ! getTextEditor().selectAndReveal(ps.endLine.getOffset(), 0); ! ! } ! ! /** ! * ! */ ! private void restartRefactorShell() { ! new Thread() { ! public void run() { ! PyRefactoring.getPyRefactoring().restartShell(); ! } ! }.start(); } /** + * @return + */ + protected Shell getPyEditShell() { + return getPyEdit().getSite().getShell(); + } + + /** + * * @param action + * @param name + * @param operation + * @return the status returned by the server for the refactoring. + * @throws Exception */ ! protected abstract String perform(IAction action, String name, ! Operation operation) throws Exception; ! /** ! * ! * @return null if no input message is needed. ! */ ! protected abstract String getInputMessage(); ! } \ No newline at end of file |