Refactory: removed unnecessary local variables and imports.
[phpeclipse.git] / net.sourceforge.phpeclipse.ui / src / net / sourceforge / phpeclipse / actions / PHPOpenAllIncludesEditorAction.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2002 IBM Corp. and others. All rights reserved. This
3  * program and the accompanying materials are made available under the terms of
4  * the Common Public License v1.0 which accompanies this distribution, and is
5  * available at http://www.eclipse.org/legal/cpl-v10.html
6  * 
7  * Contributors: www.phpeclipse.de
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.io.File;
12 import java.util.List;
13
14 import net.sourceforge.phpdt.internal.ui.viewsupport.ListContentProvider;
15 //import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
17 import net.sourceforge.phpeclipse.ui.WebUI;
18
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.runtime.CoreException;
23 //import org.eclipse.core.runtime.Path;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.TextSelection;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IEditorActionDelegate;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.IFileEditorInput;
36 //import org.eclipse.ui.IWorkbenchPage;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.actions.ActionDelegate;
39 import org.eclipse.ui.dialogs.ListSelectionDialog;
40
41 public class PHPOpenAllIncludesEditorAction extends ActionDelegate implements
42                 IEditorActionDelegate {
43
44         private IWorkbenchWindow fWindow;
45
46         private PHPEditor fEditor;
47
48         private IProject fProject;
49
50         private IncludesScanner fIncludesScanner;
51
52         public void dispose() {
53         }
54
55         public void init(IWorkbenchWindow window) {
56                 this.fWindow = window;
57         }
58
59         public void selectionChanged(IAction action, ISelection selection) {
60                 if (!selection.isEmpty()) {
61                         if (selection instanceof TextSelection) {
62                                 action.setEnabled(true);
63                         } else if (fWindow.getActivePage() != null
64                                         && fWindow.getActivePage().getActivePart() != null) {
65                                 //
66                         }
67                 }
68         }
69
70 //      private IWorkbenchPage getActivePage() {
71 //              IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
72 //                              .getWorkbenchWindow();
73 //              IWorkbenchPage page = workbenchWindow.getActivePage();
74 //              return page;
75 //      }
76
77         public IContainer getWorkingLocation(IFileEditorInput editorInput) {
78                 if (editorInput == null || editorInput.getFile() == null) {
79                         return null;
80                 }
81                 return editorInput.getFile().getParent();
82         }
83
84 //      private IFile getIncludeFile(IProject project,
85 //                      IFileEditorInput editorInput, String relativeFilename) {
86 //              IContainer container = getWorkingLocation(editorInput);
87 //              String fullPath = project.getFullPath().toString();
88 //              IFile file = null;
89 //              if (relativeFilename.startsWith("../")) {
90 //                      Path path = new Path(relativeFilename);
91 //                      file = container.getFile(path);
92 //                      return file;
93 //              }
94 //              int index = relativeFilename.lastIndexOf('/');
95 //
96 //              if (index >= 0) {
97 //                      Path path = new Path(relativeFilename);
98 //                      file = project.getFile(path);
99 //                      if (file.exists()) {
100 //                              return file;
101 //                      }
102 //              }
103 //
104 //              Path path = new Path(relativeFilename);
105 //              file = container.getFile(path);
106 //
107 //              return file;
108 //      }
109
110         public void run(IAction action) {
111                 if (fEditor == null) {
112                         IEditorPart targetEditor = fWindow.getActivePage()
113                                         .getActiveEditor();
114                         if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
115                                 fEditor = (PHPEditor) targetEditor;
116                         }
117                 }
118                 if (fEditor != null) {
119                         // determine the current Project from a (file-based) Editor
120                         IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
121                         fProject = f.getProject();
122                         // System.out.println(fProject.toString());
123
124                         ITextSelection selection = (ITextSelection) fEditor
125                                         .getSelectionProvider().getSelection();
126                         IDocument doc = fEditor.getDocumentProvider().getDocument(
127                                         fEditor.getEditorInput());
128                         fIncludesScanner = new IncludesScanner(fProject,
129                                         (IFileEditorInput) fEditor.getEditorInput());
130                         int pos = selection.getOffset();
131                         // System.out.println(selection.getText());
132                         String filename = getPHPIncludeText(doc, pos);
133
134                         if (filename != null && !filename.equals("")) {
135                                 try {
136                                         IFile file = fIncludesScanner.getIncludeFile(filename);
137                                         fIncludesScanner.addFile(file);
138                                 } catch (Exception e) {
139                                         // ignore
140                                 }
141
142                                 try {
143
144                                         List list = fIncludesScanner.getList();
145                                         if (list != null && list.size() > 0) {
146                                                 // String workspaceLocation =
147                                                 // PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
148                                                 String workspaceLocation = fProject.getFullPath()
149                                                                 .toString()
150                                                                 + File.separatorChar;
151
152                                                 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
153                                                                 WebUI.getDefault().getWorkbench()
154                                                                                 .getActiveWorkbenchWindow().getShell(),
155                                                                 list, new ListContentProvider(),
156                                                                 new LabelProvider(),
157                                                                 "Select the includes to open.");
158                                                 listSelectionDialog.setTitle("Multiple includes found");
159                                                 if (listSelectionDialog.open() == Window.OK) {
160                                                         Object[] locations = listSelectionDialog
161                                                                         .getResult();
162                                                         if (locations != null) {
163                                                                 try {
164                                                                         for (int i = 0; i < locations.length; i++) {
165                                                                                 // PHPIdentifierLocation location =
166                                                                                 // (PHPIdentifierLocation)
167                                                                                 // locations[i];
168                                                                                 String openFilename = workspaceLocation
169                                                                                                 + ((String) locations[i]);
170                                                                                 WebUI.getDefault()
171                                                                                                 .openFileInTextEditor(
172                                                                                                                 openFilename);
173                                                                         }
174                                                                 } catch (CoreException e) {
175                                                                         // TODO Auto-generated catch block
176                                                                         e.printStackTrace();
177                                                                 }
178                                                         }
179                                                 }
180
181                                         }
182                                 } catch (Exception e) {
183                                 }
184
185                         }
186                 }
187         }
188
189         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
190                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
191                         fEditor = (PHPEditor) targetEditor;
192                 }
193         }
194
195         private String getPHPIncludeText(IDocument doc, int pos) {
196                 Point word = null;
197                 int start = -1;
198                 int end = -1;
199
200                 try {
201
202                         int position = pos;
203                         char character;
204
205                         while (position >= 0) {
206                                 character = doc.getChar(position);
207                                 if ((character == '\"') || (character == '\'')
208                                                 || (character == '\r') || (character == '\n'))
209                                         break;
210                                 --position;
211                         }
212
213                         start = position;
214
215                         position = pos;
216                         int length = doc.getLength();
217
218                         while (position < length) {
219                                 character = doc.getChar(position);
220                                 if ((character == '\"') || (character == '\'')
221                                                 || (character == '\r') || (character == '\n'))
222                                         break;
223                                 ++position;
224                         }
225
226                         start++;
227                         end = position;
228
229                         if (end > start)
230                                 word = new Point(start, end - start);
231
232                 } catch (BadLocationException x) {
233                 }
234
235                 if (word != null) {
236                         try {
237                                 return doc.get(word.x, word.y);
238                         } catch (BadLocationException e) {
239                         }
240                 }
241                 return "";
242         }
243 }