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();
76 Path path = new Path(relativeFilename);
77 IFile file = project.getFile(path);
81 public void run(IAction action) {
82 if (fEditor == null) {
83 IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
84 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
85 fEditor = (PHPEditor) targetEditor;
88 if (fEditor != null) {
89 // determine the current Project from a (file-based) Editor
90 IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
91 fProject = f.getProject();
92 // System.out.println(fProject.toString());
94 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
95 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
96 int pos = selection.getOffset();
97 // System.out.println(selection.getText());
98 String filename = getPHPIncludeText(doc, pos);
99 // System.out.println(word);
100 if (filename != null && !filename.equals("")) {
102 IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
103 if (file != null && file.exists()) {
104 PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
107 } catch (Exception e) {
113 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
114 // filename = StringUtil.replaceRegExChars(filename);
115 List list = indexManager.getFileList(filename);
116 if (list != null && list.size() > 0) {
117 String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
119 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
120 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
121 "Select the includes to open.");
122 listSelectionDialog.setTitle("Multiple includes found");
123 if (listSelectionDialog.open() == Window.OK) {
124 Object[] locations = listSelectionDialog.getResult();
125 if (locations != null) {
127 for (int i = 0; i < locations.length; i++) {
128 // PHPIdentifierLocation location = (PHPIdentifierLocation)
130 String openFilename = workspaceLocation + ((String) locations[i]);
131 PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
133 } catch (CoreException e) {
134 // TODO Auto-generated catch block
141 } catch (Exception e) {
148 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
149 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
150 fEditor = (PHPEditor) targetEditor;
154 private String getPHPIncludeText(IDocument doc, int pos) {
164 while (position >= 0) {
165 character = doc.getChar(position);
166 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
174 int length = doc.getLength();
176 while (position < length) {
177 character = doc.getChar(position);
178 if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
187 word = new Point(start, end - start);
189 } catch (BadLocationException x) {
194 return doc.get(word.x, word.y);
195 } catch (BadLocationException e) {