new SF update site
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / ui / actions / RenameLocalVariable.java
1 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
2 // See http://leiffrenzel.de
3 // modified for phpeclipse.de project by axelcl
4 package net.sourceforge.phpdt.ltk.ui.actions;
5
6 import net.sourceforge.phpdt.core.ICompilationUnit;
7 import net.sourceforge.phpdt.core.IJavaElement;
8 import net.sourceforge.phpdt.core.JavaModelException;
9 import net.sourceforge.phpdt.internal.core.SourceMethod;
10 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
11 import net.sourceforge.phpdt.ltk.core.RenameIdentifierInfo;
12 import net.sourceforge.phpdt.ltk.core.RenameIdentifierRefactoring;
13 import net.sourceforge.phpdt.ltk.core.RenameLocalVariableDelegate;
14 import net.sourceforge.phpdt.ltk.core.RenamePHPProcessor;
15 import net.sourceforge.phpdt.ltk.ui.UITexts;
16 import net.sourceforge.phpdt.ltk.ui.wizards.RenameLocalVariableWizard;
17 import net.sourceforge.phpdt.ui.IWorkingCopyManager;
18 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
19 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
20 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
21
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.resources.IWorkspaceRoot;
25 import org.eclipse.core.resources.ResourcesPlugin;
26 import org.eclipse.jface.action.IAction;
27 import org.eclipse.jface.dialogs.MessageDialog;
28 import org.eclipse.jface.text.BadLocationException;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextSelection;
31 import org.eclipse.jface.viewers.ISelection;
32 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
33 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
34 import org.eclipse.swt.graphics.Point;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.ui.IEditorActionDelegate;
37 import org.eclipse.ui.IEditorInput;
38 import org.eclipse.ui.IEditorPart;
39 import org.eclipse.ui.IFileEditorInput;
40 import org.eclipse.ui.PlatformUI;
41 import org.eclipse.ui.ide.IDE;
42 import org.eclipse.ui.texteditor.ITextEditor;
43
44 public class RenameLocalVariable implements IEditorActionDelegate {
45
46         private ISelection selection;
47
48         private IEditorPart targetEditor;
49
50         private boolean onPHPFile;
51
52         private RenameIdentifierInfo info = new RenameIdentifierInfo();
53
54         // interface methods of IEditorActionDelegate
55         // ///////////////////////////////////////////
56
57         public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
58                 this.targetEditor = targetEditor;
59                 onPHPFile = false;
60                 IFile file = getFile();
61
62                 if (file != null && PHPFileUtil.isPHPFile(file)) {
63                         onPHPFile = true;
64                 }
65         }
66
67         public void run(final IAction action) {
68                 if (!onPHPFile) {
69                         refuse();
70                 } else {
71                         if (selection != null && selection instanceof ITextSelection) {
72                                 String word = null;
73                                 Point point = null;
74                                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
75                                         PHPEditor editor = (PHPEditor) targetEditor;
76                                         if (editor != null) {
77                                                 ITextSelection textSelection = (ITextSelection) editor.getSelectionProvider().getSelection();
78                                                 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
79                                                 int pos = textSelection.getOffset();
80                                                 point = PHPWordExtractor.findWord(doc, pos);
81                                                 if (point != null) {
82                                                         try {
83                                                                 word = doc.get(point.x, point.y);
84                                                                 IWorkingCopyManager manager = PHPeclipsePlugin.getDefault().getWorkingCopyManager();
85                                                                 ICompilationUnit unit = manager.getWorkingCopy(editor.getEditorInput());
86                                                                 SourceMethod method = (SourceMethod) findEnclosingElement(point.x, unit, IJavaElement.METHOD);
87                                                                 if (word == null || word.charAt(0) != '$' || method == null || !(method instanceof SourceMethod)) {
88                                                                         refuseLocalVariable();
89                                                                 }
90                                                                 applySelection((ITextSelection) selection, word, point, method);
91
92                                                         } catch (BadLocationException e) {
93                                                         }
94                                                 }
95                                         }
96                                 }
97                                 if (saveAll()) {
98                                         openWizard();
99                                 }
100                         }
101                 }
102         }
103
104         /**
105          * Returns the enclosing element of a particular element type,
106          * <code>null</code> if no enclosing element of that type exists.
107          */
108         public IJavaElement findEnclosingElement(int start, ICompilationUnit cu, int elementType) {
109                 if (cu == null)
110                         return null;
111
112                 try {
113                         IJavaElement element = cu.getElementAt(start);
114                         if (element == null) {
115                                 element = cu;
116                         }
117
118                         return element.getAncestor(elementType);
119
120                 } catch (JavaModelException e) {
121                         return null;
122                 }
123         }
124
125         public void selectionChanged(final IAction action, final ISelection selection) {
126                 this.selection = selection;
127         }
128
129         // helping methods
130         // ////////////////
131
132         private void applySelection(final ITextSelection textSelection, String word, Point point, SourceMethod method) {
133                 if (word != null) {
134                         info.setOldName(word);
135                         info.setNewName(word);
136                         info.setOffset(point.x);
137                 } else {
138                         info.setOldName(textSelection.getText());
139                         info.setNewName(textSelection.getText());
140                         info.setOffset(textSelection.getOffset());
141                 }
142                 info.setMethod(method);
143                 info.setSourceFile(getFile());
144         }
145
146         private void refuseLocalVariable() {
147                 String title = UITexts.renameLocalVariable_refuseDlg_title;
148                 String message = UITexts.renameLocalVariable_refuseDlg_message;
149                 MessageDialog.openInformation(getShell(), title, message);
150         }
151         private void refuse() {
152                 String title = UITexts.renameProperty_refuseDlg_title;
153                 String message = UITexts.renameProperty_refuseDlg_message;
154                 MessageDialog.openInformation(getShell(), title, message);
155         }
156
157         private static boolean saveAll() {
158                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
159                 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
160         }
161
162         private void openWizard() {
163                 RenameLocalVariableDelegate delegate = new RenameLocalVariableDelegate(info);
164                 RefactoringProcessor processor = new RenamePHPProcessor(info, delegate);
165                 RenameIdentifierRefactoring ref = new RenameIdentifierRefactoring(processor);
166                 RenameLocalVariableWizard wizard = new RenameLocalVariableWizard(ref, info);
167                 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
168                 try {
169                         String titleForFailedChecks = ""; //$NON-NLS-1$
170                         op.run(getShell(), titleForFailedChecks);
171                 } catch (final InterruptedException irex) {
172                         // operation was cancelled
173                 }
174         }
175
176         private Shell getShell() {
177                 Shell result = null;
178                 if (targetEditor != null) {
179                         result = targetEditor.getSite().getShell();
180                 } else {
181                         result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
182                 }
183                 return result;
184         }
185
186         private final IFile getFile() {
187                 IFile result = null;
188                 if (targetEditor instanceof ITextEditor) {
189                         ITextEditor editor = (ITextEditor) targetEditor;
190                         IEditorInput input = editor.getEditorInput();
191                         if (input instanceof IFileEditorInput) {
192                                 result = ((IFileEditorInput) input).getFile();
193                         }
194                 }
195                 return result;
196         }
197 }