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;
 
  12 import java.util.List;
 
  14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
 
  15 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
 
  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;
 
  39 import org.eclipse.ui.internal.dialogs.ListContentProvider;
 
  41 public class PHPOpenIncludeEditorAction extends ActionDelegate implements IEditorActionDelegate {
 
  43   private IWorkbenchWindow fWindow;
 
  44   private PHPEditor fEditor;
 
  45   private IProject fProject;
 
  47   public void dispose() {
 
  50   public void init(IWorkbenchWindow window) {
 
  51     this.fWindow = window;
 
  54   public void selectionChanged(IAction action, ISelection selection) {
 
  55     if (!selection.isEmpty()) {
 
  56       if (selection instanceof TextSelection) {
 
  57         action.setEnabled(true);
 
  58       } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
 
  63   private IWorkbenchPage getActivePage() {
 
  64     IWorkbenchWindow workbenchWindow = fEditor.getEditorSite().getWorkbenchWindow();
 
  65     IWorkbenchPage page = workbenchWindow.getActivePage();
 
  68   public IContainer getWorkingLocation(IFileEditorInput editorInput) {
 
  69     if (editorInput == null || editorInput.getFile() == null) {
 
  72     return editorInput.getFile().getParent();
 
  74   private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
 
  75     IContainer container = getWorkingLocation(editorInput);
 
  76     String fullPath = project.getLocation().toString();
 
  78     if (relativeFilename.startsWith("../")) {
 
  79       Path path = new Path(relativeFilename);
 
  80           file = container.getFile(path);
 
  83     int index = relativeFilename.lastIndexOf('/');
 
  86       Path path = new Path(relativeFilename);
 
  87       file = project.getFile(path);
 
  93         Path path = new Path(relativeFilename);
 
  94         file = container.getFile(path);
 
  99   public void run(IAction action) {
 
 100     if (fEditor == null) {
 
 101       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
 
 102       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
 
 103         fEditor = (PHPEditor) targetEditor;
 
 106     if (fEditor != null) {
 
 107       // determine the current Project from a (file-based) Editor
 
 108       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
 
 109       fProject = f.getProject();
 
 110       //      System.out.println(fProject.toString());
 
 112       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
 
 113       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
 
 114       int pos = selection.getOffset();
 
 115       //  System.out.println(selection.getText());
 
 116       String filename = getPHPIncludeText(doc, pos);
 
 117       //      System.out.println(word);
 
 118       if (filename != null && !filename.equals("")) {
 
 120           IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
 
 121           if (file != null && file.exists()) {
 
 122             PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
 
 125         } catch (Exception e) {
 
 131           IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
 
 132           //            filename = StringUtil.replaceRegExChars(filename);
 
 133           List list = indexManager.getFileList(filename);
 
 134           if (list != null && list.size() > 0) { 
 
 135             //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
 
 136             String workspaceLocation = fProject.getLocation().toString()+File.separatorChar;
 
 138             ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
 
 139                 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
 
 140                 "Select the includes to open.");
 
 141             listSelectionDialog.setTitle("Multiple includes found");
 
 142             if (listSelectionDialog.open() == Window.OK) {
 
 143               Object[] locations = listSelectionDialog.getResult();
 
 144               if (locations != null) {
 
 146                   for (int i = 0; i < locations.length; i++) {
 
 147                     //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
 
 149                     String openFilename = workspaceLocation + ((String) locations[i]);
 
 150                     PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
 
 152                 } catch (CoreException e) {
 
 153                   // TODO Auto-generated catch block
 
 160         } catch (Exception e) {
 
 167   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
 
 168     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
 
 169       fEditor = (PHPEditor) targetEditor;
 
 173   private String getPHPIncludeText(IDocument doc, int pos) {
 
 183       while (position >= 0) {
 
 184         character = doc.getChar(position);
 
 185         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
 
 193       int length = doc.getLength();
 
 195       while (position < length) {
 
 196         character = doc.getChar(position);
 
 197         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
 
 206         word = new Point(start, end - start);
 
 208     } catch (BadLocationException x) {
 
 213         return doc.get(word.x, word.y);
 
 214       } catch (BadLocationException e) {