--- /dev/null
+package net.sourceforge.phpeclipse.wiki.actions.httpquery;
+
+import java.net.URL;
+import java.text.BreakIterator;
+
+import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
+
+import org.eclipse.jface.action.IAction;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.ITextSelection;
+import org.eclipse.jface.viewers.ISelection;
+import org.eclipse.ui.IEditorActionDelegate;
+import org.eclipse.ui.IEditorPart;
+import org.eclipse.ui.IViewPart;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.texteditor.IDocumentProvider;
+import org.eclipse.ui.texteditor.ITextEditor;
+
+public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
+
+ private IEditorPart targetEditor;
+
+ public AbstractHTTPQueryAction() {
+ super();
+ }
+
+ public void setActiveEditor(IAction action, IEditorPart targetEditor) {
+ this.targetEditor = targetEditor;
+ }
+
+ abstract protected String getUrl(String selection);
+
+ public void run(IAction action) {
+ String selection = findSelectedText();
+ if (selection != null && selection.trim().length() > 0) {
+ URL url;
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window != null) {
+ IWorkbenchPage page = window.getActivePage();
+ try {
+ IViewPart part = page.findView(BrowserView.ID_BROWSER);
+ if (part == null) {
+ part = page.showView(BrowserView.ID_BROWSER);
+ } else {
+ page.bringToTop(part);
+ }
+ String urlStr = getUrl(selection);
+ if (urlStr != null && !urlStr.equals("")) {
+ ((BrowserView) part).setUrl(urlStr);
+ }
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ public void selectionChanged(IAction action, ISelection selection) {
+ }
+
+ protected String findSelectedText() {
+ String selectedText = null;
+ ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
+
+ selectedText = textSelection.getText();
+ if (selectedText == null || selectedText.trim().length() == 0) {
+ selectedText = findWord(textSelection);
+ }
+ return selectedText;
+ }
+
+ private String findWord(ITextSelection textSelection) {
+ IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
+ IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
+ int caretPosition = textSelection.getOffset();
+ try {
+ IRegion line = document.getLineInformation(document.getLineOfOffset(caretPosition));
+ String currentLine = document.get(line.getOffset(), line.getLength());
+ int positionInLine = caretPosition - line.getOffset();
+ return findWordAt(positionInLine, currentLine);
+ } catch (Exception e) {
+ }
+ return null;
+ }
+
+ private String findWordAt(int pos, String source) {
+ BreakIterator boundary = BreakIterator.getWordInstance();
+ boundary.setText(source);
+ int end = boundary.following(pos);
+ int start = boundary.previous();
+ return source.substring(start, end);
+ }
+
+}
\ No newline at end of file