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
7 * Contributors: www.phpeclipse.de
8 ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
12 import java.util.List;
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;
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;
41 public class PHPOpenAllIncludesEditorAction extends ActionDelegate implements
42 IEditorActionDelegate {
44 private IWorkbenchWindow fWindow;
46 private PHPEditor fEditor;
48 private IProject fProject;
50 private IncludesScanner fIncludesScanner;
52 public void dispose() {
55 public void init(IWorkbenchWindow window) {
56 this.fWindow = window;
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) {
70 private IWorkbenchPage getActivePage() {
71 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
72 .getWorkbenchWindow();
73 IWorkbenchPage page = workbenchWindow.getActivePage();
77 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
78 if (editorInput == null || editorInput.getFile() == null) {
81 return editorInput.getFile().getParent();
84 private IFile getIncludeFile(IProject project,
85 IFileEditorInput editorInput, String relativeFilename) {
86 IContainer container = getWorkingLocation(editorInput);
87 String fullPath = project.getFullPath().toString();
89 if (relativeFilename.startsWith("../")) {
90 Path path = new Path(relativeFilename);
91 file = container.getFile(path);
94 int index = relativeFilename.lastIndexOf('/');
97 Path path = new Path(relativeFilename);
98 file = project.getFile(path);
104 Path path = new Path(relativeFilename);
105 file = container.getFile(path);
110 public void run(IAction action) {
111 if (fEditor == null) {
112 IEditorPart targetEditor = fWindow.getActivePage()
114 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
115 fEditor = (PHPEditor) targetEditor;
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());
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);
134 if (filename != null && !filename.equals("")) {
136 IFile file = fIncludesScanner.getIncludeFile(filename);
137 fIncludesScanner.addFile(file);
138 } catch (Exception e) {
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()
150 + File.separatorChar;
152 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
153 WebUI.getDefault().getWorkbench()
154 .getActiveWorkbenchWindow().getShell(),
155 list, new ListContentProvider(),
157 "Select the includes to open.");
158 listSelectionDialog.setTitle("Multiple includes found");
159 if (listSelectionDialog.open() == Window.OK) {
160 Object[] locations = listSelectionDialog
162 if (locations != null) {
164 for (int i = 0; i < locations.length; i++) {
165 // PHPIdentifierLocation location =
166 // (PHPIdentifierLocation)
168 String openFilename = workspaceLocation
169 + ((String) locations[i]);
171 .openFileInTextEditor(
174 } catch (CoreException e) {
175 // TODO Auto-generated catch block
182 } catch (Exception e) {
189 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
190 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
191 fEditor = (PHPEditor) targetEditor;
195 private String getPHPIncludeText(IDocument doc, int pos) {
205 while (position >= 0) {
206 character = doc.getChar(position);
207 if ((character == '\"') || (character == '\'')
208 || (character == '\r') || (character == '\n'))
216 int length = doc.getLength();
218 while (position < length) {
219 character = doc.getChar(position);
220 if ((character == '\"') || (character == '\'')
221 || (character == '\r') || (character == '\n'))
230 word = new Point(start, end - start);
232 } catch (BadLocationException x) {
237 return doc.get(word.x, word.y);
238 } catch (BadLocationException e) {