1 package net.sourceforge.phpdt.httpquery;
3 import net.sourceforge.phpdt.httpquery.config.Configuration;
4 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
6 import org.eclipse.jface.action.IAction;
7 import org.eclipse.jface.text.BadLocationException;
8 import org.eclipse.jface.text.IDocument;
9 import org.eclipse.jface.text.ITextSelection;
10 import org.eclipse.jface.viewers.ISelection;
11 import org.eclipse.ui.IEditorActionDelegate;
12 import org.eclipse.ui.IEditorPart;
13 import org.eclipse.ui.IViewPart;
14 import org.eclipse.ui.IWorkbenchPage;
15 import org.eclipse.ui.IWorkbenchWindow;
16 import org.eclipse.ui.PlatformUI;
17 import org.eclipse.ui.texteditor.AbstractTextEditor;
19 public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
21 private AbstractTextEditor editor;
23 public AbstractHTTPQueryAction() {
27 abstract protected Configuration getConfiguration();
29 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
30 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
31 editor = (AbstractTextEditor) targetEditor;
35 public void run(IAction action) {
36 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
38 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
39 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
40 editor = (AbstractTextEditor) targetEditor;
45 IWorkbenchPage page = window.getActivePage();
47 IViewPart part = page.findView(BrowserView.ID_BROWSER);
49 part = page.showView(BrowserView.ID_BROWSER);
51 page.bringToTop(part);
53 Configuration config = getConfiguration();
54 String templateString = generateUrl(config, config.getURL());
55 if (templateString != null && !templateString.equals("")) {
56 ((BrowserView) part).setUrl(templateString);
58 } catch (Exception e) {
63 public void selectionChanged(IAction action, ISelection selection) {
66 public IDocument getDocument() {
67 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
71 public static String getSelectedText(AbstractTextEditor editor, IDocument document, int initialPos) {
74 int line = document.getLineOfOffset(pos);
75 int start = document.getLineOffset(line);
76 int end = start + document.getLineInformation(line).getLength();
79 * The line does not include \n or \r so pos can be > end. Making pos =
80 * end in this case is safe for the purposes of determining the TextRegion
81 * at the cursor position
87 int offsetInLine = pos - start;
88 String word = document.get(start, end - start);
89 int wordlen = word.length();
93 for (int i = offsetInLine; i < wordlen; i++) {
94 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
99 for (int i = offsetInLine; i >= 0; i--) {
100 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
105 if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
106 return new String(word.toCharArray(), textStart, textEnd - textStart);
108 } catch (Exception e) {
114 public String generateUrl(Configuration config, String template) {
115 IDocument doc = getDocument();
116 ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
117 int pos = selection.getOffset();
118 int len = selection.getLength();
122 wikiTitle = doc.get(pos, len);
123 } catch (BadLocationException e) {
127 wikiTitle = getSelectedText(editor, doc, pos);
130 if (wikiTitle != null && !wikiTitle.equals("")) {
131 template = template.replaceAll("\\$text.selection", wikiTitle);
132 wikiTitle = wikiTitle.replaceAll("_", "-");
133 template = template.replaceAll("\\$php.selection", wikiTitle);