made message: "Include filename doesn't exist in project" configurable
[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: www.phpeclipse.de
8  ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
10
11 import java.io.File;
12 import java.util.List;
13
14 import net.sourceforge.phpeclipse.PHPeclipsePlugin;
15 import net.sourceforge.phpeclipse.builder.IdentifierIndexManager;
16 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
17
18 import org.eclipse.core.resources.IContainer;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.core.runtime.Path;
23 import org.eclipse.jface.action.IAction;
24 import org.eclipse.jface.text.BadLocationException;
25 import org.eclipse.jface.text.IDocument;
26 import org.eclipse.jface.text.ITextSelection;
27 import org.eclipse.jface.text.TextSelection;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.LabelProvider;
30 import org.eclipse.jface.window.Window;
31 import org.eclipse.swt.graphics.Point;
32 import org.eclipse.ui.IEditorActionDelegate;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IWorkbenchPage;
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 PHPOpenIncludeEditorAction 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   private IWorkbenchPage getActivePage() {
64     IWorkbenchWindow workbenchWindow = fEditor.getEditorSite().getWorkbenchWindow();
65     IWorkbenchPage page = workbenchWindow.getActivePage();
66     return page;
67   }
68   public IContainer getWorkingLocation(IFileEditorInput editorInput) {
69     if (editorInput == null || editorInput.getFile() == null) {
70       return null;
71     }
72     return editorInput.getFile().getParent();
73   }
74   private IFile getIncludeFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
75     IContainer container = getWorkingLocation(editorInput);
76     String fullPath = project.getLocation().toString();
77     IFile file = null;
78     if (relativeFilename.startsWith("../")) {
79       Path path = new Path(relativeFilename);
80           file = container.getFile(path);
81           return file;
82     }
83     int index = relativeFilename.lastIndexOf('/');
84     
85     if (index >= 0) {
86       Path path = new Path(relativeFilename);
87       file = project.getFile(path);
88       if (file.exists()) {
89             return file;
90           }
91     } 
92     
93         Path path = new Path(relativeFilename);
94         file = container.getFile(path);
95     
96     return file;
97   }
98
99   public void run(IAction action) {
100     if (fEditor == null) {
101       IEditorPart targetEditor = fWindow.getActivePage().getActiveEditor();
102       if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
103         fEditor = (PHPEditor) targetEditor;
104       }
105     }
106     if (fEditor != null) {
107       // determine the current Project from a (file-based) Editor
108       IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
109       fProject = f.getProject();
110       //      System.out.println(fProject.toString());
111
112       ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
113       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
114       int pos = selection.getOffset();
115       //  System.out.println(selection.getText());
116       String filename = getPHPIncludeText(doc, pos);
117       //      System.out.println(word);
118       if (filename != null && !filename.equals("")) {
119         try {
120           IFile file = getIncludeFile(fProject, (IFileEditorInput) fEditor.getEditorInput(), filename);
121           if (file != null && file.exists()) {
122             PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
123             return;
124           }
125         } catch (Exception e) {
126           // ignore
127         }
128
129         try {
130
131           IdentifierIndexManager indexManager = PHPeclipsePlugin.getDefault().getIndexManager(fProject);
132           //            filename = StringUtil.replaceRegExChars(filename);
133           List list = indexManager.getFileList(filename);
134           if (list != null && list.size() > 0) { 
135             //String workspaceLocation = PHPeclipsePlugin.getWorkspace().getRoot().getLocation().toString();
136             String workspaceLocation = fProject.getLocation().toString()+File.separatorChar;
137             
138             ListSelectionDialog listSelectionDialog = new ListSelectionDialog(PHPeclipsePlugin.getDefault().getWorkbench()
139                 .getActiveWorkbenchWindow().getShell(), list, new ListContentProvider(), new LabelProvider(),
140                 "Select the includes to open.");
141             listSelectionDialog.setTitle("Multiple includes found");
142             if (listSelectionDialog.open() == Window.OK) {
143               Object[] locations = listSelectionDialog.getResult();
144               if (locations != null) {
145                 try {
146                   for (int i = 0; i < locations.length; i++) {
147                     //                    PHPIdentifierLocation location = (PHPIdentifierLocation)
148                     // locations[i];
149                     String openFilename = workspaceLocation + ((String) locations[i]);
150                     PHPeclipsePlugin.getDefault().openFileInTextEditor(openFilename);
151                   }
152                 } catch (CoreException e) {
153                   // TODO Auto-generated catch block
154                   e.printStackTrace();
155                 }
156               }
157             }
158
159           }
160         } catch (Exception e) {
161         }
162
163       }
164     }
165   }
166
167   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
168     if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
169       fEditor = (PHPEditor) targetEditor;
170     }
171   }
172
173   private String getPHPIncludeText(IDocument doc, int pos) {
174     Point word = null;
175     int start = -1;
176     int end = -1;
177
178     try {
179
180       int position = pos;
181       char character;
182
183       while (position >= 0) {
184         character = doc.getChar(position);
185         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
186           break;
187         --position;
188       }
189
190       start = position;
191
192       position = pos;
193       int length = doc.getLength();
194
195       while (position < length) {
196         character = doc.getChar(position);
197         if ((character == '\"') || (character == '\'') || (character == '\r') || (character == '\n'))
198           break;
199         ++position;
200       }
201
202       start++;
203       end = position;
204
205       if (end > start)
206         word = new Point(start, end - start);
207
208     } catch (BadLocationException x) {
209     }
210
211     if (word != null) {
212       try {
213         return doc.get(word.x, word.y);
214       } catch (BadLocationException e) {
215       }
216     }
217     return "";
218   }
219 }