1dcf6ebd6cb82d7f3b0c1a3ef2ec165a05d8c9b5
[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 net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
13
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.action.IAction;
19 import org.eclipse.jface.text.BadLocationException;
20 import org.eclipse.jface.text.IDocument;
21 import org.eclipse.jface.text.ITextSelection;
22 import org.eclipse.jface.text.TextSelection;
23 import org.eclipse.jface.viewers.ISelection;
24 import org.eclipse.swt.graphics.Point;
25 import org.eclipse.ui.IEditorActionDelegate;
26 import org.eclipse.ui.IEditorPart;
27 import org.eclipse.ui.IFileEditorInput;
28 import org.eclipse.ui.IWorkbenchPage;
29 import org.eclipse.ui.IWorkbenchWindow;
30 import org.eclipse.ui.actions.ActionDelegate;
31 import org.eclipse.ui.ide.IDE;
32
33 public class PHPOpenIncludeEditorAction extends ActionDelegate
34                 implements
35                         IEditorActionDelegate {
36
37         private IWorkbenchWindow fWindow;
38         private PHPEditor fEditor;
39         private IProject fProject;
40
41         public void dispose() {
42         }
43
44         public void init(IWorkbenchWindow window) {
45                 this.fWindow = window;
46         }
47
48         public void selectionChanged(IAction action, ISelection selection) {
49                 if (!selection.isEmpty()) {
50                         if (selection instanceof TextSelection) {
51                                 action.setEnabled(true);
52                         } else if (fWindow.getActivePage() != null
53                                         && fWindow.getActivePage().getActivePart() != null) {
54                                 //
55                         }
56                 }
57         }
58         private IWorkbenchPage getActivePage() {
59                 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
60                                 .getWorkbenchWindow();
61                 IWorkbenchPage page = workbenchWindow.getActivePage();
62                 return page;
63         }
64         public IContainer getWorkingLocation(IFileEditorInput editorInput) {
65                 if (editorInput == null || editorInput.getFile() == null) {
66                         return null;
67                 }
68                 return editorInput.getFile().getParent();
69         }
70         private IFile getWikiFile(IProject project, IFileEditorInput editorInput, String relativeFilename) {
71 //              IContainer container = getWorkingLocation(editorInput);
72 //              String fullPath  = project.getLocation().toString();
73                 Path path = new Path(relativeFilename);
74                 IFile file = project.getFile(path);
75                 return file;
76         }
77
78         public void run(IAction action) {
79                 if (fEditor == null) {
80                         IEditorPart targetEditor = fWindow.getActivePage()
81                                         .getActiveEditor();
82                         if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
83                                 fEditor = (PHPEditor) targetEditor;
84                         }
85                 }
86                 if (fEditor != null) {
87                         // determine the current Project from a (file-based) Editor
88                         IFile f = ((IFileEditorInput) fEditor.getEditorInput()).getFile();
89                         fProject = f.getProject();
90                         //      System.out.println(fProject.toString());
91
92                         ITextSelection selection = (ITextSelection) fEditor
93                                         .getSelectionProvider().getSelection();
94                         IDocument doc = fEditor.getDocumentProvider().getDocument(
95                                         fEditor.getEditorInput());
96                         int pos = selection.getOffset();
97                         //  System.out.println(selection.getText());
98                         String relativeFilename = getPHPIncludeText(doc, pos);
99                         //      System.out.println(word);
100                         if (relativeFilename != null && !relativeFilename.equals("")) {
101
102                                 IFile file = getWikiFile(fProject, (IFileEditorInput) fEditor
103                                                 .getEditorInput(), relativeFilename);
104                                 try {
105                                         //                      createNewFileIfNeeded(file);
106                                         //                      if
107                                         // (WikiPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR))
108                                         // {
109                                         //                              saveIfNeeded();
110                                         //                              getActivePage().reuseEditor(reusableEditor, new
111                                         // FileEditorInput(file));
112                                         //                      } else {
113                                         IDE.openEditor(getActivePage(), file, true);
114                                         //                              redrawText();
115                                         //                      }
116                                 } catch (Exception e) {
117                                         //                      WikiPlugin.getDefault().logAndReport(WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE),
118                                         // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT),
119                                         // e);
120                                 }
121
122                         }
123                 }
124         }
125
126         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
127                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
128                         fEditor = (PHPEditor) targetEditor;
129                 }
130         }
131
132         private String getPHPIncludeText(IDocument doc, int pos) {
133                 Point word = null;
134                 int start = -1;
135                 int end = -1;
136
137                 try {
138
139                         int position = pos;
140                         char character;
141
142                         while (position >= 0) {
143                                 character = doc.getChar(position);
144                                 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
145                                         break;
146                                 --position;
147                         }
148
149                         start = position;
150
151                         position = pos;
152                         int length = doc.getLength();
153
154                         while (position < length) {
155                                 character = doc.getChar(position);
156                                 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
157                                         break;
158                                 ++position;
159                         }
160
161                         start++;
162                         end = position;
163
164                         if (end > start)
165                                 word = new Point(start, end - start);
166
167                 } catch (BadLocationException x) {
168                 }
169
170                 if (word != null) {
171                         try {
172                                 return doc.get(word.x, word.y);
173                         } catch (BadLocationException e) {
174                         }
175                 }
176                 return "";
177         }
178 }