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