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.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
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.search.internal.ui.util.ListContentProvider;
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;
40 public class PHPOpenAllIncludesEditorAction extends ActionDelegate implements IEditorActionDelegate {
42 private IWorkbenchWindow fWindow;
43 private PHPEditor fEditor;
44 private IProject fProject;
45 private IncludesScanner fIncludesScanner;
46 public void dispose() {
49 public void init(IWorkbenchWindow window) {
50 this.fWindow = window;
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) {
62 private IWorkbenchPage getActivePage() {
63 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite().getWorkbenchWindow();
64 IWorkbenchPage page = workbenchWindow.getActivePage();
67 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
68 if (editorInput == null || editorInput.getFile() == null) {
71 return editorInput.getFile().getParent();
73 private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
74 IContainer container = getWorkingLocation(editorInput);
75 String fullPath = project.getLocation().toString();
77 if (relativeFilename.startsWith("../")) {
78 Path path = new Path(relativeFilename);
79 file = container.getFile(path);
82 int index = relativeFilename.lastIndexOf('/');
85 Path path = new Path(relativeFilename);
86 file = project.getFile(path);
92 Path path = new Path(relativeFilename);
93 file = container.getFile(path);
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;
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());
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);
118 if (filename != null && !filename.equals("")) {
120 IFile file = fIncludesScanner.getIncludeFile(filename);
121 fIncludesScanner.addFile(file);
122 } catch (Exception e) {
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;
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) {
141 for (int i = 0; i < locations.length; i++) {
142 // PHPIdentifierLocation location = (PHPIdentifierLocation)
144 String openFilename = workspaceLocation + ((String) locations[i]);
145 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
147 } catch (CoreException e) {
148 // TODO Auto-generated catch block
155 } catch (Exception e) {
162 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
163 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
164 fEditor = (PHPEditor) targetEditor;
168 private String getPHPIncludeText(IDocument doc, int pos) {
178 while (position >= 0) {
179 character = doc.getChar(position);
180 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
188 int length = doc.getLength();
190 while (position < length) {
191 character = doc.getChar(position);
192 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
201 word = new Point(start, end - start);
203 } catch (BadLocationException x) {
208 return doc.get(word.x, word.y);
209 } catch (BadLocationException e) {