9e38cd594467ec426b9568526ee0693b72cdf3f8
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / ltk / ui / actions / RenamePHPIdentifier.java
1 // Copyright (c) 2005 by Leif Frenzel. All rights reserved.
2 // See http://leiffrenzel.de
3 package net.sourceforge.phpdt.ltk.ui.actions;
4
5 import net.sourceforge.phpdt.internal.ui.util.PHPFileUtil;
6 import net.sourceforge.phpdt.ltk.core.RenamePropertyInfo;
7 import net.sourceforge.phpdt.ltk.core.RenamePropertyProcessor;
8 import net.sourceforge.phpdt.ltk.core.RenamePropertyRefactoring;
9 import net.sourceforge.phpdt.ltk.ui.UITexts;
10 import net.sourceforge.phpdt.ltk.ui.wizards.RenamePropertyWizard;
11 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
12 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IWorkspaceRoot;
17 import org.eclipse.core.resources.ResourcesPlugin;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.dialogs.MessageDialog;
20 import org.eclipse.jface.text.BadLocationException;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor;
25 import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation;
26 import org.eclipse.swt.graphics.Point;
27 import org.eclipse.swt.widgets.Shell;
28 import org.eclipse.ui.IEditorActionDelegate;
29 import org.eclipse.ui.IEditorInput;
30 import org.eclipse.ui.IEditorPart;
31 import org.eclipse.ui.IFileEditorInput;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.ide.IDE;
34 import org.eclipse.ui.texteditor.ITextEditor;
35
36 /**
37  * <p>
38  * action that is triggered from the editor context menu.
39  * </p>
40  *
41  * <p>
42  * This action is declared in the <code>plugin.xml</code>.
43  * </p>
44  *
45  * @author Leif Frenzel
46  */
47 public class RenamePHPIdentifier implements IEditorActionDelegate {
48
49         private static final String EXT_PROPERTIES = "properties"; //$NON-NLS-1$
50
51         private ISelection selection;
52
53         private IEditorPart targetEditor;
54
55         private boolean onPropertiesFile;
56
57         private RenamePropertyInfo info = new RenamePropertyInfo();
58
59         // interface methods of IEditorActionDelegate
60         // ///////////////////////////////////////////
61
62         public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
63                 this.targetEditor = targetEditor;
64                 onPropertiesFile = false;
65                 IFile file = getFile();
66
67                 if (file != null && PHPFileUtil.isPHPFile(file)) {
68                         onPropertiesFile = true;
69                 }
70         }
71
72         public void run(final IAction action) {
73                 if (!onPropertiesFile) {
74                         refuse();
75                 } else {
76                         if (selection != null && selection instanceof ITextSelection) {
77                                 String word = null;
78                                 Point point = null;
79                                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
80                                         PHPEditor editor = (PHPEditor) targetEditor;
81                                         if (editor != null) {
82                                                 ITextSelection textSelection = (ITextSelection) editor.getSelectionProvider().getSelection();
83                                                 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
84                                                 int pos = textSelection.getOffset();
85                                                 point = PHPWordExtractor.findWord(doc, pos);
86                                                 if (point != null) {
87                                                         try {
88                                                                 word = doc.get(point.x, point.y);
89                                                         } catch (BadLocationException e) {
90                                                         }
91                                                 }
92                                         }
93                                 }
94                                 applySelection((ITextSelection) selection, word, point);
95                                 if (saveAll()) {
96                                         openWizard();
97                                 }
98                         }
99                 }
100         }
101
102         public void selectionChanged(final IAction action, final ISelection selection) {
103                 this.selection = selection;
104         }
105
106         // helping methods
107         // ////////////////
108
109         private void applySelection(final ITextSelection textSelection, String word, Point point) {
110                 if (word != null) {
111                         info.setOldName(word);
112                         info.setNewName(word);
113                         info.setOffset(point.x);
114                 } else {
115                         info.setOldName(textSelection.getText());
116                         info.setNewName(textSelection.getText());
117                         info.setOffset(textSelection.getOffset());
118                 }
119                 info.setSourceFile(getFile());
120         }
121
122         private void refuse() {
123                 String title = UITexts.renameProperty_refuseDlg_title;
124                 String message = UITexts.renameProperty_refuseDlg_message;
125                 MessageDialog.openInformation(getShell(), title, message);
126         }
127
128         private static boolean saveAll() {
129                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
130                 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
131         }
132
133         private void openWizard() {
134                 RefactoringProcessor processor = new RenamePropertyProcessor(info);
135                 RenamePropertyRefactoring ref = new RenamePropertyRefactoring(processor);
136                 RenamePropertyWizard wizard = new RenamePropertyWizard(ref, info);
137                 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(wizard);
138                 try {
139                         String titleForFailedChecks = ""; //$NON-NLS-1$
140                         op.run(getShell(), titleForFailedChecks);
141                 } catch (final InterruptedException irex) {
142                         // operation was cancelled
143                 }
144         }
145
146         private Shell getShell() {
147                 Shell result = null;
148                 if (targetEditor != null) {
149                         result = targetEditor.getSite().getShell();
150                 } else {
151                         result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
152                 }
153                 return result;
154         }
155
156         private final IFile getFile() {
157                 IFile result = null;
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();
163                         }
164                 }
165                 return result;
166         }
167 }