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;
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;
40 public class PHPOpenAllIncludesEditorAction extends ActionDelegate implements
41 IEditorActionDelegate {
43 private IWorkbenchWindow fWindow;
45 private PHPEditor fEditor;
47 private IProject fProject;
49 private IncludesScanner fIncludesScanner;
51 public void dispose() {
54 public void init(IWorkbenchWindow window) {
55 this.fWindow = window;
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) {
69 private IWorkbenchPage getActivePage() {
70 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
71 .getWorkbenchWindow();
72 IWorkbenchPage page = workbenchWindow.getActivePage();
76 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
77 if (editorInput == null || editorInput.getFile() == null) {
80 return editorInput.getFile().getParent();
83 private IFile getIncludeFile(IProject project,
84 IFileEditorInput editorInput, String relativeFilename) {
85 IContainer container = getWorkingLocation(editorInput);
86 String fullPath = project.getFullPath().toString();
88 if (relativeFilename.startsWith("../")) {
89 Path path = new Path(relativeFilename);
90 file = container.getFile(path);
93 int index = relativeFilename.lastIndexOf('/');
96 Path path = new Path(relativeFilename);
97 file = project.getFile(path);
103 Path path = new Path(relativeFilename);
104 file = container.getFile(path);
109 public void run(IAction action) {
110 if (fEditor == null) {
111 IEditorPart targetEditor = fWindow.getActivePage()
113 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
114 fEditor = (PHPEditor) targetEditor;
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());
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);
133 if (filename != null && !filename.equals("")) {
135 IFile file = fIncludesScanner.getIncludeFile(filename);
136 fIncludesScanner.addFile(file);
137 } catch (Exception e) {
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.getFullPath()
149 + File.separatorChar;
151 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(
152 PHPeclipsePlugin.getDefault().getWorkbench()
153 .getActiveWorkbenchWindow().getShell(),
154 list, new ListContentProvider(),
156 "Select the includes to open.");
157 listSelectionDialog.setTitle("Multiple includes found");
158 if (listSelectionDialog.open() == Window.OK) {
159 Object[] locations = listSelectionDialog
161 if (locations != null) {
163 for (int i = 0; i < locations.length; i++) {
164 // PHPIdentifierLocation location =
165 // (PHPIdentifierLocation)
167 String openFilename = workspaceLocation
168 + ((String) locations[i]);
169 PHPeclipsePlugin.getDefault()
170 .openFileInTextEditor(
173 } catch (CoreException e) {
174 // TODO Auto-generated catch block
181 } catch (Exception e) {
188 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
189 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
190 fEditor = (PHPEditor) targetEditor;
194 private String getPHPIncludeText(IDocument doc, int pos) {
204 while (position >= 0) {
205 character = doc.getChar(position);
206 if ((character == '\"') || (character == '\'')
207 || (character == '\r') || (character == '\n'))
215 int length = doc.getLength();
217 while (position < length) {
218 character = doc.getChar(position);
219 if ((character == '\"') || (character == '\'')
220 || (character == '\r') || (character == '\n'))
229 word = new Point(start, end - start);
231 } catch (BadLocationException x) {
236 return doc.get(word.x, word.y);
237 } catch (BadLocationException e) {