stack frame bugs fixed
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenDeclarationEditorActon.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 PHPOpenDeclarationEditorActon 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       String word = getPHPIdentifier(doc, pos);
81       //      System.out.println(word);
82       if (word != null && !word.equals("")) {
83         IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
84         List list = indexManager.getLocations(word);
85         if (list != null && list.size() > 0) {
86           String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
87           // TODO show all entries of the list in a dialog box
88           // at the moment always the first entry will be opened
89           if (list.size() > 1) {
90             ListSelectionDialog listSelectionDialog =
91               new ListSelectionDialog(
92                 PHPeclipsePlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell(),
93                 list,
94                 new ListContentProvider(),
95                 new LabelProvider(),
96                 "Select the resources to open.");
97             listSelectionDialog.setTitle("Multiple declarations found");
98             if (listSelectionDialog.open() == Window.OK) {
99               Object[] locations = listSelectionDialog.getResult();
100               if (locations != null) {
101                 try {
102                   for (int i = 0; i < locations.length; i++) {
103                     PHPIdentifierLocation location = (PHPIdentifierLocation) locations[i];
104                     String filename = workspaceLocation + location.getFilename();
105                     //                                  System.out.println(filename);
106                     if (location.getOffset() >= 0) {
107                       PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
108                     } else {
109                       PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
110                     }
111                   }
112                 } catch (CoreException e) {
113                   // TODO Auto-generated catch block
114                   e.printStackTrace();
115                 }
116               }
117             }
118           } else {
119             try {
120               PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
121               String filename = workspaceLocation + location.getFilename();
122               //                                        System.out.println(filename);
123               if (location.getOffset() >= 0) {
124                 PHPeclipsePlugin.getDefault().openFileAndGotoOffset(filename, location.getOffset(), word.length());
125               } else {
126                 PHPeclipsePlugin.getDefault().openFileAndFindString(filename, word);
127               }
128             } catch (CoreException e) {
129               // TODO Auto-generated catch block
130               e.printStackTrace();
131             }
132           }
133         }
134       }
135     }
136   }
137
138   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
139     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
140       fEditor = (PHPEditor) targetEditor;
141     }
142   }
143
144   private String getPHPIdentifier(IDocument doc, int pos) {
145     Point word = PHPWordExtractor.findWord(doc, pos);
146     if (word != null) {
147       try {
148         return doc.get(word.x, word.y);
149       } catch (BadLocationException e) {
150       }
151     }
152     return "";
153   }
154 }