Fix import
[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         public void setActiveEditor(final IAction action,
55                         final IEditorPart targetEditor) {
56                 this.targetEditor = targetEditor;
57                 onPHPFile = false;
58                 IFile file = getFile();
59
60                 if (file != null && PHPFileUtil.isPHPFile(file)) {
61                         onPHPFile = true;
62                 }
63         }
64
65         public void run(final IAction action) {
66                 if (!onPHPFile) {
67                         refuse();
68                 } else {
69                         if (selection != null && selection instanceof ITextSelection) {
70                                 String word = null;
71                                 Point point = null;
72                                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
73                                         PHPEditor editor = (PHPEditor) targetEditor;
74                                         if (editor != null) {
75                                                 ITextSelection textSelection = (ITextSelection) editor
76                                                                 .getSelectionProvider().getSelection();
77                                                 IDocument doc = editor.getDocumentProvider()
78                                                                 .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
85                                                                                 .getDefault().getWorkingCopyManager();
86                                                                 ICompilationUnit unit = manager
87                                                                                 .getWorkingCopy(editor.getEditorInput());
88                                                                 SourceMethod method = (SourceMethod) findEnclosingElement(
89                                                                                 point.x, unit, IJavaElement.METHOD);
90                                                                 if (word == null || word.charAt(0) != '$'
91                                                                                 || method == null
92                                                                                 || !(method instanceof SourceMethod)) {
93                                                                         refuseLocalVariable();
94                                                                 } else {
95                                                                         applySelection((ITextSelection) selection,
96                                                                                         word, point, method);
97                                                                         if (saveAll()) {
98                                                                                 openWizard();
99                                                                         }
100                                                                 }
101                                                         } catch (BadLocationException e) {
102                                                         }
103                                                 }
104                                         }
105                                 }
106                         }
107                 }
108         }
109
110         /**
111          * Returns the enclosing element of a particular element type,
112          * <code>null</code> if no enclosing element of that type exists.
113          */
114         public IJavaElement findEnclosingElement(int start, ICompilationUnit cu,
115                         int elementType) {
116                 if (cu == null)
117                         return null;
118
119                 try {
120                         IJavaElement element = cu.getElementAt(start);
121                         if (element == null) {
122                                 element = cu;
123                         }
124
125                         return element.getAncestor(elementType);
126
127                 } catch (JavaModelException e) {
128                         return null;
129                 }
130         }
131
132         public void selectionChanged(final IAction action,
133                         final ISelection selection) {
134                 this.selection = selection;
135         }
136
137         // helping methods
138         // ////////////////
139
140         private void applySelection(final ITextSelection textSelection,
141                         String word, Point point, SourceMethod method) {
142                 if (word != null) {
143                         info.setOldName(word);
144                         info.setNewName(word);
145                         info.setOffset(point.x);
146                 } else {
147                         info.setOldName(textSelection.getText());
148                         info.setNewName(textSelection.getText());
149                         info.setOffset(textSelection.getOffset());
150                 }
151                 info.setMethod(method);
152                 info.setSourceFile(getFile());
153         }
154
155         private void refuseLocalVariable() {
156                 String title = UITexts.renameLocalVariable_refuseDlg_title;
157                 String message = UITexts.renameLocalVariable_refuseDlg_message;
158                 MessageDialog.openInformation(getShell(), title, message);
159         }
160
161         private void refuse() {
162                 String title = UITexts.renameProperty_refuseDlg_title;
163                 String message = UITexts.renameProperty_refuseDlg_message;
164                 MessageDialog.openInformation(getShell(), title, message);
165         }
166
167         private static boolean saveAll() {
168                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
169                 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
170         }
171
172         private void openWizard() {
173                 RenameLocalVariableDelegate delegate = new RenameLocalVariableDelegate(
174                                 info);
175                 RefactoringProcessor processor = new RenamePHPProcessor(info, delegate);
176                 RenameIdentifierRefactoring ref = new RenameIdentifierRefactoring(
177                                 processor);
178                 RenameLocalVariableWizard wizard = new RenameLocalVariableWizard(ref,
179                                 info);
180                 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(
181                                 wizard);
182                 try {
183                         String titleForFailedChecks = ""; //$NON-NLS-1$
184                         op.run(getShell(), titleForFailedChecks);
185                 } catch (final InterruptedException irex) {
186                         // operation was cancelled
187                 }
188         }
189
190         private Shell getShell() {
191                 Shell result = null;
192                 if (targetEditor != null) {
193                         result = targetEditor.getSite().getShell();
194                 } else {
195                         result = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
196                                         .getShell();
197                 }
198                 return result;
199         }
200
201         private final IFile getFile() {
202                 IFile result = null;
203                 if (targetEditor instanceof ITextEditor) {
204                         ITextEditor editor = (ITextEditor) targetEditor;
205                         IEditorInput input = editor.getEditorInput();
206                         if (input instanceof IFileEditorInput) {
207                                 result = ((IFileEditorInput) input).getFile();
208                         }
209                 }
210                 return result;
211         }
212 }