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.builder.IdentifierIndexManager;
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.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;
40 public class PHPOpenIncludeEditorAction extends ActionDelegate implements IEditorActionDelegate {
42 private IWorkbenchWindow fWindow;
43 private PHPEditor fEditor;
44 private IProject fProject;
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 int pos = selection.getOffset();
114 // System.out.println(selection.getText());
115 String filename = getPHPIncludeText(doc, pos);
116 // System.out.println(word);
117 if (filename != null && !filename.equals("")) {
119 IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
120 if (file != null && file.exists()) {
121 PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
124 } catch (Exception e) {
130 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
131 // filename = StringUtil.replaceRegExChars(filename);
132 List list = indexManager.getFileList(filename);
133 if (list != null && list.size() > 0) {
134 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
136 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
137 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
138 "Select the includes to open.");
139 listSelectionDialog.setTitle("Multiple includes found");
140 if (listSelectionDialog.open() == Window.OK) {
141 Object[] locations = listSelectionDialog.getResult();
142 if (locations != null) {
144 for (int i = 0; i < locations.length; i++) {
145 // PHPIdentifierLocation location = (PHPIdentifierLocation)
147 String openFilename = workspaceLocation + ((String) locations[i]);
148 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
150 } catch (CoreException e) {
151 // TODO Auto-generated catch block
158 } catch (Exception e) {
165 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
166 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
167 fEditor = (PHPEditor) targetEditor;
171 private String getPHPIncludeText(IDocument doc, int pos) {
181 while (position >= 0) {
182 character = doc.getChar(position);
183 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
191 int length = doc.getLength();
193 while (position < length) {
194 character = doc.getChar(position);
195 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
204 word = new Point(start, end - start);
206 } catch (BadLocationException x) {
211 return doc.get(word.x, word.y);
212 } catch (BadLocationException e) {