Open PHP "include" relative to the projects root inside the editor (right mouse click)
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationEditorAction.java
1 /**********************************************************************
2 Copyright (c) 2000, 2002 IBM Corp. and others.
3 All rights reserved. This program and the accompanying materials
4 are made available under the terms of the Common Public License v1.0
5 which accompanies this distribution, and is available at
6 http://www.eclipse.org/legal/cpl-v10.html
7
8 Contributors:
9     Klaus Hartlage - www.eclipseproject.de
10 **********************************************************************/
11 package net.sourceforge.phpeclipse.actions;
12
13 import java.util.List;
14
15 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
16 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
17 import net.sourceforge.phpeclipse.builder.PHPIdentifierLocation;
18 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
19 import net.sourceforge.phpeclipse.phpeditor.php.PHPWordExtractor;
20
21 import org.eclipse.core.resources.IFile;
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.jface.action.IAction;
25 import org.eclipse.jface.text.BadLocationException;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.TextSelection;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.swt.graphics.Point;
33 import org.eclipse.ui.IEditorActionDelegate;
34 import org.eclipse.ui.IEditorPart;
35 import org.eclipse.ui.IFileEditorInput;
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;
40
41 public class PHPOpenDeclarationEditorAction extends ActionDelegate implements IEditorActionDelegate {
42
43   private IWorkbenchWindow fWindow;
44   private PHPEditor fEditor;
45   private IProject fProject;
46
47   public void dispose() {
48   }
49
50   public void init(IWorkbenchWindow window) {
51     this.fWindow = window;
52   }
53
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) {
59         //
60       }
61     }
62   }
63
64   public void run(IAction action) {
65     if (fEditor == null) {
66       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
67       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
68         fEditor = (PHPEditor) targetEditor;
69       }
70     }
71     if (fEditor != null) {
72       // determine the current Project from a (file-based) Editor
73       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
74       fProject = f.getProject();
75       //      System.out.println(fProject.toString());
76
77       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
78       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
79       int pos = selection.getOffset();
80     //  System.out.println(selection.getText());
81       String word = getPHPIdentifier(doc, pos);
82       //      System.out.println(word);
83       if (word != null && !word.equals("")) {
84         IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
85         List list = indexManager.getLocations(word);
86         if (list != null && list.size() > 0) {
87           String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
88           // TODO show all entries of the list in a dialog box
89           // at the moment always the first entry will be opened
90           if (list.size() > 1) {
91             ListSelectionDialog listSelectionDialog =
92               new ListSelectionDialog(
93                 PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
94                 list,
95                 new ListContentProvider(),
96                 new LabelProvider(),
97                 "Select the resources to open.");
98             listSelectionDialog.setTitle("Multiple declarations found");
99             if (listSelectionDialog.open() == Window.OK) {
100               Object[] locations = listSelectionDialog.getResult();
101               if (locations != null) {
102                 try {
103                   for (int i = 0; i < locations.length; i++) {
104                     PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
105                     String filename = workspaceLocation + location.getFilename();
106                     //                                  System.out.println(filename);
107                     if (location.getOffset() >= 0) {
108                       PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
109                     } else {
110                       PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
111                     }
112                   }
113                 } catch (CoreException e) {
114                   // TODO Auto-generated catch block
115                   e.printStackTrace();
116                 }
117               }
118             }
119           } else {
120             try {
121               PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
122               String filename = workspaceLocation + location.getFilename();
123               //                                        System.out.println(filename);
124               if (location.getOffset() >= 0) {
125                 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
126               } else {
127                 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
128               }
129             } catch (CoreException e) {
130               // TODO Auto-generated catch block
131               e.printStackTrace();
132             }
133           }
134         }
135       }
136     }
137   }
138
139   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
140     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
141       fEditor = (PHPEditor) targetEditor;
142     }
143   }
144
145   private String getPHPIdentifier(IDocument doc, int pos) {
146     Point word = PHPWordExtractor.findWord(doc, pos);
147     if (word != null) {
148       try {
149         return doc.get(word.x, word.y);
150       } catch (BadLocationException e) {
151       }
152     }
153     return "";
154   }
155 }