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