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: Klaus Hartlage - www.eclipseproject.de
8 ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
11 import java.util.List;
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.text.TextSelection;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.LabelProvider;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.ui.IEditorActionDelegate;
31 import org.eclipse.ui.IEditorPart;
32 import org.eclipse.ui.IFileEditorInput;
33 import org.eclipse.ui.IWorkbenchPage;
34 import org.eclipse.ui.IWorkbenchWindow;
35 import org.eclipse.ui.actions.ActionDelegate;
36 import org.eclipse.ui.dialogs.ListSelectionDialog;
37 import org.eclipse.ui.internal.dialogs.ListContentProvider;
39 public class PHPOpenAllIncludesEditorAction extends ActionDelegate implements IEditorActionDelegate {
41 private IWorkbenchWindow fWindow;
42 private PHPEditor fEditor;
43 private IProject fProject;
44 private IncludesScanner fLineCreator;
45 public void dispose() {
48 public void init(IWorkbenchWindow window) {
49 this.fWindow = window;
52 public void selectionChanged(IAction action, ISelection selection) {
53 if (!selection.isEmpty()) {
54 if (selection instanceof TextSelection) {
55 action.setEnabled(true);
56 } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
61 private IWorkbenchPage getActivePage() {
62 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite().getWorkbenchWindow();
63 IWorkbenchPage page = workbenchWindow.getActivePage();
66 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
67 if (editorInput == null || editorInput.getFile() == null) {
70 return editorInput.getFile().getParent();
72 private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
73 IContainer container = getWorkingLocation(editorInput);
74 String fullPath = project.getLocation().toString();
76 if (relativeFilename.startsWith("../")) {
77 Path path = new Path(relativeFilename);
78 file = container.getFile(path);
81 int index = relativeFilename.lastIndexOf('/');
84 Path path = new Path(relativeFilename);
85 file = project.getFile(path);
91 Path path = new Path(relativeFilename);
92 file = container.getFile(path);
97 public void run(IAction action) {
98 if (fEditor == null) {
99 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
100 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
101 fEditor = (PHPEditor) targetEditor;
104 if (fEditor != null) {
105 // determine the current Project from a (file-based) Editor
106 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
107 fProject = f.getProject();
108 // System.out.println(fProject.toString());
110 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
111 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
112 fLineCreator = new IncludesScanner(this, fProject, (IFileEditorInput) fEditor.getEditorInput());
113 int pos = selection.getOffset();
114 // System.out.println(selection.getText());
115 String filename = getPHPIncludeText(doc, pos);
117 if (filename != null && !filename.equals("")) {
119 IFile file = fLineCreator.getIncludeFile(filename);
120 fLineCreator.addFile(file);
121 } catch (Exception e) {
127 List list = fLineCreator.getList();
128 if (list != null && list.size() > 0) {
129 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
131 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
132 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
133 "Select the includes to open.");
134 listSelectionDialog.setTitle("Multiple includes found");
135 if (listSelectionDialog.open() == Window.OK) {
136 Object[] locations = listSelectionDialog.getResult();
137 if (locations != null) {
139 for (int i = 0; i < locations.length; i++) {
140 // PHPIdentifierLocation location = (PHPIdentifierLocation)
142 String openFilename = workspaceLocation + ((String) locations[i]);
143 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
145 } catch (CoreException e) {
146 // TODO Auto-generated catch block
153 } catch (Exception e) {
160 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
161 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
162 fEditor = (PHPEditor) targetEditor;
166 private String getPHPIncludeText(IDocument doc, int pos) {
176 while (position >= 0) {
177 character = doc.getChar(position);
178 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
186 int length = doc.getLength();
188 while (position < length) {
189 character = doc.getChar(position);
190 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
199 word = new Point(start, end - start);
201 } catch (BadLocationException x) {
206 return doc.get(word.x, word.y);
207 } catch (BadLocationException e) {