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;
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;
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;
45 public class RenameLocalVariable implements IEditorActionDelegate {
47 private ISelection selection;
49 private IEditorPart targetEditor;
51 private boolean onPHPFile;
53 private RenameIdentifierInfo info = new RenameIdentifierInfo();
55 public void setActiveEditor(final IAction action,
56 final IEditorPart targetEditor) {
57 this.targetEditor = targetEditor;
59 IFile file = getFile();
61 if (file != null && PHPFileUtil.isPHPFile(file)) {
66 public void run(final IAction action) {
70 if (selection != null && selection instanceof ITextSelection) {
73 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
74 PHPEditor editor = (PHPEditor) targetEditor;
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);
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) != '$'
93 || !(method instanceof SourceMethod)) {
94 refuseLocalVariable();
96 applySelection((ITextSelection) selection,
102 } catch (BadLocationException e) {
112 * Returns the enclosing element of a particular element type,
113 * <code>null</code> if no enclosing element of that type exists.
115 public IJavaElement findEnclosingElement(int start, ICompilationUnit cu,
121 IJavaElement element = cu.getElementAt(start);
122 if (element == null) {
126 return element.getAncestor(elementType);
128 } catch (JavaModelException e) {
133 public void selectionChanged(final IAction action,
134 final ISelection selection) {
135 this.selection = selection;
141 private void applySelection(final ITextSelection textSelection,
142 String word, Point point, SourceMethod method) {
144 info.setOldName(word);
145 info.setNewName(word);
146 info.setOffset(point.x);
148 info.setOldName(textSelection.getText());
149 info.setNewName(textSelection.getText());
150 info.setOffset(textSelection.getOffset());
152 info.setMethod(method);
153 info.setSourceFile(getFile());
156 private void refuseLocalVariable() {
157 String title = UITexts.renameLocalVariable_refuseDlg_title;
158 String message = UITexts.renameLocalVariable_refuseDlg_message;
159 MessageDialog.openInformation(getShell(), title, message);
162 private void refuse() {
163 String title = UITexts.renameProperty_refuseDlg_title;
164 String message = UITexts.renameProperty_refuseDlg_message;
165 MessageDialog.openInformation(getShell(), title, message);
168 private static boolean saveAll() {
169 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
170 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
173 private void openWizard() {
174 RenameLocalVariableDelegate delegate = new RenameLocalVariableDelegate(
176 RefactoringProcessor processor = new RenamePHPProcessor(info, delegate);
177 RenameIdentifierRefactoring ref = new RenameIdentifierRefactoring(
179 RenameLocalVariableWizard wizard = new RenameLocalVariableWizard(ref,
181 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(
184 String titleForFailedChecks = ""; //$NON-NLS-1$
185 op.run(getShell(), titleForFailedChecks);
186 } catch (final InterruptedException irex) {
187 // operation was cancelled
191 private Shell getShell() {
193 if (targetEditor != null) {
194 result = targetEditor.getSite().getShell();
196 result = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
202 private final IFile getFile() {
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();