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