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.RenamePropertyInfo;
8 import net.sourceforge.phpdt.ltk.core.RenamePropertyProcessor;
9 import net.sourceforge.phpdt.ltk.core.RenamePropertyRefactoring;
10 import net.sourceforge.phpdt.ltk.ui.UITexts;
11 import net.sourceforge.phpdt.ltk.ui.wizards.RenamePropertyWizard;
12 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
13 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IWorkspaceRoot;
18 import org.eclipse.core.resources.ResourcesPlugin;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.text.BadLocationException;
22 import org.eclipse.jface.text.IDocument;
23 import org.eclipse.jface.text.ITextSelection;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
26 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.widgets.Shell;
29 import org.eclipse.ui.IEditorActionDelegate;
30 import org.eclipse.ui.IEditorInput;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IFileEditorInput;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.ide.IDE;
35 import org.eclipse.ui.texteditor.ITextEditor;
39 * action that is triggered from the editor context menu.
43 * This action is declared in the <code>plugin.xml</code>.
46 * @author Leif Frenzel
48 public class RenamePHPIdentifier implements IEditorActionDelegate {
50 // private static final String EXT_PROPERTIES = "properties"; //$NON-NLS-1$
52 private ISelection selection;
54 private IEditorPart targetEditor;
56 private boolean onPropertiesFile;
58 private RenamePropertyInfo info = new RenamePropertyInfo();
60 // interface methods of IEditorActionDelegate
61 // ///////////////////////////////////////////
63 public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
64 this.targetEditor = targetEditor;
65 onPropertiesFile = false;
66 IFile file = getFile();
68 if (file != null && PHPFileUtil.isPHPFile(file)) {
69 onPropertiesFile = true;
73 public void run(final IAction action) {
74 if (!onPropertiesFile) {
77 if (selection != null && selection instanceof ITextSelection) {
80 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
81 PHPEditor editor = (PHPEditor) targetEditor;
83 ITextSelection textSelection = (ITextSelection) editor.getSelectionProvider().getSelection();
84 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
85 int pos = textSelection.getOffset();
86 point = PHPWordExtractor.findWord(doc, pos);
89 word = doc.get(point.x, point.y);
90 } catch (BadLocationException e) {
95 applySelection((ITextSelection) selection, word, point);
103 public void selectionChanged(final IAction action, final ISelection selection) {
104 this.selection = selection;
110 private void applySelection(final ITextSelection textSelection, String word, Point point) {
112 info.setOldName(word);
113 info.setNewName(word);
114 info.setOffset(point.x);
116 info.setOldName(textSelection.getText());
117 info.setNewName(textSelection.getText());
118 info.setOffset(textSelection.getOffset());
120 info.setSourceFile(getFile());
123 private void refuse() {
124 String title = UITexts.renameProperty_refuseDlg_title;
125 String message = UITexts.renameProperty_refuseDlg_message;
126 MessageDialog.openInformation(getShell(), title, message);
129 private static boolean saveAll() {
130 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
131 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
134 private void openWizard() {
135 RefactoringProcessor processor = new RenamePropertyProcessor(info);
136 RenamePropertyRefactoring ref = new RenamePropertyRefactoring(processor);
137 RenamePropertyWizard wizard = new RenamePropertyWizard(ref, info);
138 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
140 String titleForFailedChecks = ""; //$NON-NLS-1$
141 op.run(getShell(), titleForFailedChecks);
142 } catch (final InterruptedException irex) {
143 // operation was cancelled
147 private Shell getShell() {
149 if (targetEditor != null) {
150 result = targetEditor.getSite().getShell();
152 result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
157 private final IFile getFile() {
159 if (targetEditor instanceof ITextEditor) {
160 ITextEditor editor = (ITextEditor) targetEditor;
161 IEditorInput input = editor.getEditorInput();
162 if (input instanceof IFileEditorInput) {
163 result = ((IFileEditorInput) input).getFile();