Changed "Open PHP Declarartion" action in the PHP editor:
[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                     PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
107                   }
108                 } catch (CoreException e) {
109                   // TODO Auto-generated catch block
110                   e.printStackTrace();
111                 }
112               }
113             }
114           } else {
115             try {
116               PHPIdentifierLocation location = (PHPIdentifierLocation) list.get(0);
117               String filename = workspaceLocation + location.getFilename();
118               //                                        System.out.println(filename);
119               PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
120
121             } catch (CoreException e) {
122               // TODO Auto-generated catch block
123               e.printStackTrace();
124             }
125           }
126         }
127       }
128     }
129   }
130
131   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
132     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
133       fEditor = (PHPEditor) targetEditor;
134     }
135   }
136
137   //  public static void openContextHelp(String word) {
138   //    IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
139   //    if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
140   //      String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
141   //      MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
142   //      try {
143   //        Runtime runtime = Runtime.getRuntime();
144   //        String command = form.format(arguments);
145   //
146   //        runtime.exec(command);
147   //      } catch (IOException e) {
148   //      }
149   //    } else {
150   //      IHelp help = WorkbenchHelp.getHelpSupport();
151   //      if (help != null) {
152   //        PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
153   //        WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
154   //      } else {
155   //        //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
156   //      }
157   //    }
158   //  }
159
160   private String getPHPIdentifier(IDocument doc, int pos) {
161     Point word = PHPWordExtractor.findWord(doc, pos);
162     if (word != null) {
163       try {
164         return doc.get(word.x, word.y);
165       } catch (BadLocationException e) {
166       }
167     }
168     return "";
169   }
170 }