improved "Open Include"
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / actions / PHPOpenIncludeEditorAction.java
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
6  * 
7  * Contributors: Klaus Hartlage - www.eclipseproject.de
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.util.List;
12
13 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
14 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
15 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
16
17 import org.eclipse.core.resources.IContainer;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jface.action.IAction;
23 import org.eclipse.jface.text.BadLocationException;
24 import org.eclipse.jface.text.IDocument;
25 import org.eclipse.jface.text.ITextSelection;
26 import org.eclipse.jface.text.TextSelection;
27 import org.eclipse.jface.viewers.ISelection;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.window.Window;
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.IWorkbenchPage;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.actions.ActionDelegate;
37 import org.eclipse.ui.dialogs.ListSelectionDialog;
38 import org.eclipse.ui.internal.dialogs.ListContentProvider;
39
40 public class PHPOpenIncludeEditorAction extends ActionDelegate implements IEditorActionDelegate {
41
42   private IWorkbenchWindow fWindow;
43   private PHPEditor fEditor;
44   private IProject fProject;
45
46   public void dispose() {
47   }
48
49   public void init(IWorkbenchWindow window) {
50     this.fWindow = window;
51   }
52
53   public void selectionChanged(IAction action, ISelection selection) {
54     if (!selection.isEmpty()) {
55       if (selection instanceof TextSelection) {
56         action.setEnabled(true);
57       } else if (fWindow.getActivePage() != null && fWindow.getActivePage().getActivePart() != null) {
58         //
59       }
60     }
61   }
62   private IWorkbenchPage getActivePage() {
63     IWorkbenchWindow workbenchWindow = fEditor.getEditorSite().getWorkbenchWindow();
64     IWorkbenchPage page = workbenchWindow.getActivePage();
65     return page;
66   }
67   public IContainer getWorkingLocation(IFileEditorInput editorInput) {
68     if (editorInput == null || editorInput.getFile() == null) {
69       return null;
70     }
71     return editorInput.getFile().getParent();
72   }
73   private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
74     //          IContainer container = getWorkingLocation(editorInput);
75     //          String fullPath = project.getLocation().toString();
76     Path path = new Path(relativeFilename);
77     IFile file = project.getFile(path);
78     return file;
79   }
80
81   public void run(IAction action) {
82     if (fEditor == null) {
83       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
84       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
85         fEditor = (PHPEditor) targetEditor;
86       }
87     }
88     if (fEditor != null) {
89       // determine the current Project from a (file-based) Editor
90       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
91       fProject = f.getProject();
92       //      System.out.println(fProject.toString());
93
94       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
95       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
96       int pos = selection.getOffset();
97       //  System.out.println(selection.getText());
98       String filename = getPHPIncludeText(doc, pos);
99       //      System.out.println(word);
100       if (filename != null && !filename.equals("")) {
101         try {
102           IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
103           if (file != null && file.exists()) {
104             PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
105             return;
106           }
107         } catch (Exception e) {
108 // ignore
109         }
110
111         try {
112
113           IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
114           //            filename = StringUtil.replaceRegExChars(filename);
115           List list = indexManager.getFileList(filename);
116           if (list != null && list.size() > 0) {
117             String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
118
119             ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
120                 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
121                 "Select the includes to open.");
122             listSelectionDialog.setTitle("Multiple includes found");
123             if (listSelectionDialog.open() == Window.OK) {
124               Object[] locations = listSelectionDialog.getResult();
125               if (locations != null) {
126                 try {
127                   for (int i = 0; i < locations.length; i++) {
128                     //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
129                     // locations[i];
130                     String openFilename = workspaceLocation + ((String) locations[i]);
131                     PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
132                   }
133                 } catch (CoreException e) {
134                   // TODO Auto-generated catch block
135                   e.printStackTrace();
136                 }
137               }
138             }
139
140           }
141         } catch (Exception e) {
142         }
143
144       }
145     }
146   }
147
148   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
149     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
150       fEditor = (PHPEditor) targetEditor;
151     }
152   }
153
154   private String getPHPIncludeText(IDocument doc, int pos) {
155     Point word = null;
156     int start = -1;
157     int end = -1;
158
159     try {
160
161       int position = pos;
162       char character;
163
164       while (position >= 0) {
165         character = doc.getChar(position);
166         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
167           break;
168         --position;
169       }
170
171       start = position;
172
173       position = pos;
174       int length = doc.getLength();
175
176       while (position < length) {
177         character = doc.getChar(position);
178         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
179           break;
180         ++position;
181       }
182
183       start++;
184       end = position;
185
186       if (end > start)
187         word = new Point(start, end - start);
188
189     } catch (BadLocationException x) {
190     }
191
192     if (word != null) {
193       try {
194         return doc.get(word.x, word.y);
195       } catch (BadLocationException e) {
196       }
197     }
198     return "";
199   }
200 }