"Open SQL Table View" actiun (right mouse click in editor on table fragment-string)
[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.phpeclipse.PHPeclipsePlugin;
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 getIncludeFile(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 = getIncludeFile(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                                         PHPeclipsePlugin.getDefault().openFileInTextEditor(file.getLocation().toString());
115                                         //                              redrawText();
116                                         //                      }
117                                 } catch (Exception e) {
118                                         //                      WikiPlugin.getDefault().logAndReport(WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE),
119                                         // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT),
120                                         // e);
121                                 }
122
123                         }
124                 }
125         }
126
127         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
128                 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
129                         fEditor = (PHPEditor) targetEditor;
130                 }
131         }
132
133         private String getPHPIncludeText(IDocument doc, int pos) {
134                 Point word = null;
135                 int start = -1;
136                 int end = -1;
137
138                 try {
139
140                         int position = pos;
141                         char character;
142
143                         while (position >= 0) {
144                                 character = doc.getChar(position);
145                                 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
146                                         break;
147                                 --position;
148                         }
149
150                         start = position;
151
152                         position = pos;
153                         int length = doc.getLength();
154
155                         while (position < length) {
156                                 character = doc.getChar(position);
157                                 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
158                                         break;
159                                 ++position;
160                         }
161
162                         start++;
163                         end = position;
164
165                         if (end > start)
166                                 word = new Point(start, end - start);
167
168                 } catch (BadLocationException x) {
169                 }
170
171                 if (word != null) {
172                         try {
173                                 return doc.get(word.x, word.y);
174                         } catch (BadLocationException e) {
175                         }
176                 }
177                 return "";
178         }
179 }