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