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