removed unused methods: /net.sourceforge.phpeclipse.xdebug.core/src/net/sourceforge...
[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.RenameIdentifierDelegate;
8 import net.sourceforge.phpdt.ltk.core.RenameIdentifierInfo;
9 import net.sourceforge.phpdt.ltk.core.RenameIdentifierRefactoring;
10 import net.sourceforge.phpdt.ltk.core.RenamePHPProcessor;
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;
15
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;
37
38 /**
39  * <p>
40  * action that is triggered from the editor context menu.
41  * </p>
42  * 
43  * <p>
44  * This action is declared in the <code>plugin.xml</code>.
45  * </p>
46  * 
47  */
48 public class RenamePHPIdentifier implements IEditorActionDelegate {
49
50         private ISelection selection;
51
52         private IEditorPart targetEditor;
53
54         private boolean onPHPFile;
55
56         private RenameIdentifierInfo info = new RenameIdentifierInfo();
57
58         // interface methods of IEditorActionDelegate
59         // ///////////////////////////////////////////
60
61         public void setActiveEditor(final IAction action,
62                         final IEditorPart targetEditor) {
63                 this.targetEditor = targetEditor;
64                 onPHPFile = false;
65                 IFile file = getFile();
66
67                 if (file != null && PHPFileUtil.isPHPFile(file)) {
68                         onPHPFile = true;
69                 }
70         }
71
72         public void run(final IAction action) {
73                 if (!onPHPFile) {
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
83                                                                 .getSelectionProvider().getSelection();
84                                                 IDocument doc = editor.getDocumentProvider()
85                                                                 .getDocument(editor.getEditorInput());
86                                                 int pos = textSelection.getOffset();
87                                                 point = PHPWordExtractor.findWord(doc, pos);
88                                                 if (point != null) {
89                                                         try {
90                                                                 word = doc.get(point.x, point.y);
91                                                         } catch (BadLocationException e) {
92                                                         }
93                                                 }
94                                         }
95                                 }
96                                 applySelection((ITextSelection) selection, word, point);
97                                 if (saveAll()) {
98                                         openWizard();
99                                 }
100                         }
101                 }
102         }
103
104         public void selectionChanged(final IAction action,
105                         final ISelection selection) {
106                 this.selection = selection;
107         }
108
109         // helping methods
110         // ////////////////
111
112         private void applySelection(final ITextSelection textSelection,
113                         String word, Point point) {
114                 if (word != null) {
115                         info.setOldName(word);
116                         info.setNewName(word);
117                         info.setOffset(point.x);
118                 } else {
119                         info.setOldName(textSelection.getText());
120                         info.setNewName(textSelection.getText());
121                         info.setOffset(textSelection.getOffset());
122                 }
123                 info.setSourceFile(getFile());
124         }
125
126         private void refuse() {
127                 String title = UITexts.renameProperty_refuseDlg_title;
128                 String message = UITexts.renameProperty_refuseDlg_message;
129                 MessageDialog.openInformation(getShell(), title, message);
130         }
131
132         private static boolean saveAll() {
133                 IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
134                 return IDE.saveAllEditors(new IResource[] { workspaceRoot }, false);
135         }
136
137         private void openWizard() {
138                 RenameIdentifierDelegate delegate = new RenameIdentifierDelegate(info);
139                 RefactoringProcessor processor = new RenamePHPProcessor(info, delegate);
140                 RenameIdentifierRefactoring ref = new RenameIdentifierRefactoring(
141                                 processor);
142                 RenameIdentifierWizard wizard = new RenameIdentifierWizard(ref, info);
143                 RefactoringWizardOpenOperation op = new RefactoringWizardOpenOperation(
144                                 wizard);
145                 try {
146                         String titleForFailedChecks = ""; //$NON-NLS-1$
147                         op.run(getShell(), titleForFailedChecks);
148                 } catch (final InterruptedException irex) {
149                         // operation was cancelled
150                 }
151         }
152
153         private Shell getShell() {
154                 Shell result = null;
155                 if (targetEditor != null) {
156                         result = targetEditor.getSite().getShell();
157                 } else {
158                         result = PlatformUI.getWorkbench().getActiveWorkbenchWindow()
159                                         .getShell();
160                 }
161                 return result;
162         }
163
164         private final IFile getFile() {
165                 IFile result = null;
166                 if (targetEditor instanceof ITextEditor) {
167                         ITextEditor editor = (ITextEditor) targetEditor;
168                         IEditorInput input = editor.getEditorInput();
169                         if (input instanceof IFileEditorInput) {
170                                 result = ((IFileEditorInput) input).getFile();
171                         }
172                 }
173                 return result;
174         }
175 }