4c588917b0b307fb8abd7c16caa7787a5cf19b17
[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 // modified for phpeclipse.de project by axelcl
4 package net.sourceforge.phpdt.ltk.ui.actions;
5
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;
14
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;
36
37 /**
38  * <p>
39  * action that is triggered from the editor context menu.
40  * </p>
41  *
42  * <p>
43  * This action is declared in the <code>plugin.xml</code>.
44  * </p>
45  *
46  * @author Leif Frenzel
47  */
48 public class RenamePHPIdentifier implements IEditorActionDelegate {
49
50 //      private static final String EXT_PROPERTIES = "properties"; //$NON-NLS-1$
51
52         private ISelection selection;
53
54         private IEditorPart targetEditor;
55
56         private boolean onPropertiesFile;
57
58         private RenamePropertyInfo info = new RenamePropertyInfo();
59
60         // interface methods of IEditorActionDelegate
61         // ///////////////////////////////////////////
62
63         public void setActiveEditor(final IAction action, final IEditorPart targetEditor) {
64                 this.targetEditor = targetEditor;
65                 onPropertiesFile = false;
66                 IFile file = getFile();
67
68                 if (file != null && PHPFileUtil.isPHPFile(file)) {
69                         onPropertiesFile = true;
70                 }
71         }
72
73         public void run(final IAction action) {
74                 if (!onPropertiesFile) {
75                         refuse();
76                 } else {
77                         if (selection != null && selection instanceof ITextSelection) {
78                                 String word = null;
79                                 Point point = null;
80                                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
81                                         PHPEditor editor = (PHPEditor) targetEditor;
82                                         if (editor != null) {
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);
87                                                 if (point != null) {
88                                                         try {
89                                                                 word = doc.get(point.x, point.y);
90                                                         } catch (BadLocationException e) {
91                                                         }
92                                                 }
93                                         }
94                                 }
95                                 applySelection((ITextSelection) selection, word, point);
96                                 if (saveAll()) {
97                                         openWizard();
98                                 }
99                         }
100                 }
101         }
102
103         public void selectionChanged(final IAction action, final ISelection selection) {
104                 this.selection = selection;
105         }
106
107         // helping methods
108         // ////////////////
109
110         private void applySelection(final ITextSelection textSelection, String word, Point point) {
111                 if (word != null) {
112                         info.setOldName(word);
113                         info.setNewName(word);
114                         info.setOffset(point.x);
115                 } else {
116                         info.setOldName(textSelection.getText());
117                         info.setNewName(textSelection.getText());
118                         info.setOffset(textSelection.getOffset());
119                 }
120                 info.setSourceFile(getFile());
121         }
122
123         private void refuse() {
124                 String title = UITexts.renameProperty_refuseDlg_title;
125                 String message = UITexts.renameProperty_refuseDlg_message;
126                 MessageDialog.openInformation(getShell(), title, message);
127         }
128
129         private static boolean saveAll() {
130                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
131                 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
132         }
133
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);
139                 try {
140                         String titleForFailedChecks = ""; //$NON-NLS-1$
141                         op.run(getShell(), titleForFailedChecks);
142                 } catch (final InterruptedException irex) {
143                         // operation was cancelled
144                 }
145         }
146
147         private Shell getShell() {
148                 Shell result = null;
149                 if (targetEditor != null) {
150                         result = targetEditor.getSite().getShell();
151                 } else {
152                         result = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
153                 }
154                 return result;
155         }
156
157         private final IFile getFile() {
158                 IFile result = null;
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();
164                         }
165                 }
166                 return result;
167         }
168 }