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(String name);
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(null);
54 String templateString = generateUrl(config, config.getURL());
55 if (templateString != null && !templateString.equals("")) {
56 ((BrowserView) part).setUrl(templateString);
58 ((BrowserView) part).setUrl(config.getURL());
60 } catch (Exception e) {
65 public void selectionChanged(IAction action, ISelection selection) {
68 public IDocument getDocument() {
69 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
73 public static String getSelectedText(AbstractTextEditor editor, IDocument document, int initialPos) {
76 int line = document.getLineOfOffset(pos);
77 int start = document.getLineOffset(line);
78 int end = start + document.getLineInformation(line).getLength();
81 * The line does not include \n or \r so pos can be > end. Making pos =
82 * end in this case is safe for the purposes of determining the TextRegion
83 * at the cursor position
89 int offsetInLine = pos - start;
90 String word = document.get(start, end - start);
91 int wordlen = word.length();
95 for (int i = offsetInLine; i < wordlen; i++) {
96 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
101 for (int i = offsetInLine; i >= 0; i--) {
102 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
107 if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
108 return new String(word.toCharArray(), textStart, textEnd - textStart);
110 } catch (Exception e) {
116 public String generateUrl(Configuration config, String template) {
117 IDocument doc = getDocument();
118 ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
119 int pos = selection.getOffset();
120 int len = selection.getLength();
124 wikiTitle = doc.get(pos, len);
125 } catch (BadLocationException e) {
129 wikiTitle = getSelectedText(editor, doc, pos);
132 if (wikiTitle != null && !wikiTitle.equals("")) {
133 template = template.replaceAll("\\$text.selection", wikiTitle);
134 wikiTitle = wikiTitle.replaceAll("_", "-");
135 template = template.replaceAll("\\$php.selection", wikiTitle);