the PHP_NEW field must be before the field PHPPARSER
[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.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.IWorkbenchWindow;
35 import org.eclipse.ui.actions.ActionDelegate;
36
37 public class PHPOpenDeclarationEditorActon extends ActionDelegate implements IEditorActionDelegate {
38
39   private IWorkbenchWindow fWindow;
40   private PHPEditor fEditor;
41   private IProject fProject;
42
43   public void dispose() {
44   }
45
46   public void init(IWorkbenchWindow window) {
47     this.fWindow = window;
48   }
49
50   public void selectionChanged(IAction action, ISelection selection) {
51     if (!selection.isEmpty()) {
52       if (selection instanceof TextSelection) {
53         action.setEnabled(true);
54       } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
55         //
56       }
57     }
58   }
59
60   public void run(IAction action) {
61     if (fEditor == null) {
62       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
63       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
64         fEditor = (PHPEditor) targetEditor;
65       }
66     }
67     if (fEditor != null) {
68       // determine the current Project from a (file-based) Editor
69       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
70       fProject = f.getProject();
71 //      System.out.println(fProject.toString());
72
73       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
74       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
75       int pos = selection.getOffset();
76       String word = getPHPIdentifier(doc, pos);
77       //      System.out.println(word);
78       if (word!=null && ! word.equals("")) {
79                                 IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
80                                 List list = indexManager.getLocations(word);
81                                 if (list!=null && list.size()>0) {
82                                         String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
83                                         // TODO show all entries of the list in a dialog box
84                                         // at the moment allways the first entry will be opened
85                                         PHPIdentifierLocation location = (PHPIdentifierLocation)list.get(0);
86                                         String filename = workspaceLocation + location.getFilename();
87 //                                      System.out.println(filename);
88                                         try {
89             PHPeclipsePlugin.getDefault().openFileInTextEditor(filename, 0, word);
90           } catch (CoreException e) {
91             // TODO Auto-generated catch block
92             e.printStackTrace();
93           }
94                                 }
95       }
96     }
97   }
98
99   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
100     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
101       fEditor = (PHPEditor) targetEditor;
102     }
103   }
104
105   //  public static void openContextHelp(String word) {
106   //    IPreferenceStore store = PHPeclipsePlugin.getDefault().getPreferenceStore();
107   //    if (store.getBoolean(PHPHelpPlugin.PHP_CHM_ENABLED)) {
108   //      String[] arguments = { store.getString(PHPHelpPlugin.PHP_CHM_FILE), word };
109   //      MessageFormat form = new MessageFormat(store.getString(PHPHelpPlugin.PHP_CHM_COMMAND));
110   //      try {
111   //        Runtime runtime = Runtime.getRuntime();
112   //        String command = form.format(arguments);
113   //
114   //        runtime.exec(command);
115   //      } catch (IOException e) {
116   //      }
117   //    } else {
118   //      IHelp help = WorkbenchHelp.getHelpSupport();
119   //      if (help != null) {
120   //        PHPFunctionHelpResource helpResource = new PHPFunctionHelpResource(word);
121   //        WorkbenchHelp.getHelpSupport().displayHelpResource(helpResource);
122   //      } else {
123   //        //   showMessage(shell, dialogTitle, ActionMessages.getString("Open help not available"), false); //$NON-NLS-1$
124   //      }
125   //    }
126   //  }
127
128   private String getPHPIdentifier(IDocument doc, int pos) {
129     Point word = PHPWordExtractor.findWord(doc, pos);
130     if (word != null) {
131       try {
132         return doc.get(word.x, word.y);
133       } catch (BadLocationException e) {
134       }
135     }
136     return "";
137   }
138 }