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
7 * Contributors: Klaus Hartlage - www.eclipseproject.de
8 ******************************************************************************/
9 package net.sourceforge.phpeclipse.actions;
11 import net.sourceforge.phpdt.internal.compiler.parser.Scanner;
12 import net.sourceforge.phpeclipse.phpeditor.PHPEditor;
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;
33 public class PHPOpenIncludeEditorAction extends ActionDelegate
35 IEditorActionDelegate {
37 private IWorkbenchWindow fWindow;
38 private PHPEditor fEditor;
39 private IProject fProject;
41 public void dispose() {
44 public void init(IWorkbenchWindow window) {
45 this.fWindow = window;
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) {
58 private IWorkbenchPage getActivePage() {
59 IWorkbenchWindow workbenchWindow = fEditor.getEditorSite()
60 .getWorkbenchWindow();
61 IWorkbenchPage page = workbenchWindow.getActivePage();
64 public IContainer getWorkingLocation(IFileEditorInput editorInput) {
65 if (editorInput == null || editorInput.getFile() == null) {
68 return editorInput.getFile().getParent();
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);
78 public void run(IAction action) {
79 if (fEditor == null) {
80 IEditorPart targetEditor = fWindow.getActivePage()
82 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
83 fEditor = (PHPEditor) targetEditor;
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());
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("")) {
102 IFile file = getWikiFile(fProject, (IFileEditorInput) fEditor
103 .getEditorInput(), relativeFilename);
105 // createNewFileIfNeeded(file);
107 // (WikiPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR))
110 // getActivePage().reuseEditor(reusableEditor, new
111 // FileEditorInput(file));
113 IDE.openEditor(getActivePage(), file, true);
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),
126 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
127 if (targetEditor != null && (targetEditor instanceof PHPEditor)) {
128 fEditor = (PHPEditor) targetEditor;
132 private String getPHPIncludeText(IDocument doc, int pos) {
142 while (position >= 0) {
143 character = doc.getChar(position);
144 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
152 int length = doc.getLength();
154 while (position < length) {
155 character = doc.getChar(position);
156 if ((character=='\"')||(character == '\'')|| (character == '\r')|| (character == '\n'))
165 word = new Point(start, end - start);
167 } catch (BadLocationException x) {
172 return doc.get(word.x, word.y);
173 } catch (BadLocationException e) {