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.internal.ui.util.PHPFileUtil;
7 import net.sourceforge.phpdt.ltk.core.RenameIdentifierDelegate;
8 import net.sourceforge.phpdt.ltk.core.RenameIdentifierInfo;
9 import net.sourceforge.phpdt.ltk.core.RenamePHPProcessor;
10 import net.sourceforge.phpdt.ltk.core.RenameIdentifierRefactoring;
11 import net.sourceforge.phpdt.ltk.ui.UITexts;
12 import net.sourceforge.phpdt.ltk.ui.wizards.RenameIdentifierWizard;
13 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
14 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.resources.IWorkspaceRoot;
19 import org.eclipse.core.resources.ResourcesPlugin;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.dialogs.MessageDialog;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
27 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
28 import org.eclipse.swt.graphics.Point;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IEditorActionDelegate;
31 import org.eclipse.ui.IEditorInput;
32 import org.eclipse.ui.IEditorPart;
33 import org.eclipse.ui.IFileEditorInput;
34 import org.eclipse.ui.PlatformUI;
35 import org.eclipse.ui.ide.IDE;
36 import org.eclipse.ui.texteditor.ITextEditor;
40 * action that is triggered from the editor context menu.
44 * This action is declared in the <code>plugin.xml</code>.
48 public class RenamePHPIdentifier implements IEditorActionDelegate {
50 private ISelection selection;
52 private IEditorPart targetEditor;
54 private boolean onPHPFile;
56 private RenameIdentifierInfo info = new RenameIdentifierInfo();
58 // interface methods of IEditorActionDelegate
59 // ///////////////////////////////////////////
61 public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
62 this.targetEditor = targetEditor;
64 IFile file = getFile();
66 if (file != null && PHPFileUtil.isPHPFile(file)) {
71 public void run(final IAction action) {
75 if (selection != null && selection instanceof ITextSelection) {
78 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
79 PHPEditor editor = (PHPEditor) targetEditor;
81 ITextSelection textSelection = (ITextSelection) editor.getSelectionProvider().getSelection();
82 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
83 int pos = textSelection.getOffset();
84 point = PHPWordExtractor.findWord(doc, pos);
87 word = doc.get(point.x, point.y);
88 } catch (BadLocationException e) {
93 applySelection((ITextSelection) selection, word, point);
101 public void selectionChanged(final IAction action, final ISelection selection) {
102 this.selection = selection;
108 private void applySelection(final ITextSelection textSelection, String word, Point point) {
110 info.setOldName(word);
111 info.setNewName(word);
112 info.setOffset(point.x);
114 info.setOldName(textSelection.getText());
115 info.setNewName(textSelection.getText());
116 info.setOffset(textSelection.getOffset());
118 info.setSourceFile(getFile());
121 private void refuse() {
122 String title = UITexts.renameProperty_refuseDlg_title;
123 String message = UITexts.renameProperty_refuseDlg_message;
124 MessageDialog.openInformation(getShell(), title, message);
127 private static boolean saveAll() {
128 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
129 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
132 private void openWizard() {
133 RenameIdentifierDelegate delegate = new RenameIdentifierDelegate( info );
134 RefactoringProcessor processor = new RenamePHPProcessor(info, delegate);
135 RenameIdentifierRefactoring ref = new RenameIdentifierRefactoring(processor);
136 RenameIdentifierWizard wizard = new RenameIdentifierWizard(ref, info);
137 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
139 String titleForFailedChecks = ""; //$NON-NLS-1$
140 op.run(getShell(), titleForFailedChecks);
141 } catch (final InterruptedException irex) {
142 // operation was cancelled
146 private Shell getShell() {
148 if (targetEditor != null) {
149 result = targetEditor.getSite().getShell();
151 result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
156 private final IFile getFile() {
158 if (targetEditor instanceof ITextEditor) {
159 ITextEditor editor = (ITextEditor) targetEditor;
160 IEditorInput input = editor.getEditorInput();
161 if (input instanceof IFileEditorInput) {
162 result = ((IFileEditorInput) input).getFile();