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
 
  31                                 && (targetEditor instanceof AbstractTextEditor)) {
 
  32                         editor = (AbstractTextEditor) targetEditor;
 
  36         public void run(IAction action) {
 
  37                 IWorkbenchWindow window = PlatformUI.getWorkbench()
 
  38                                 .getActiveWorkbenchWindow();
 
  40                         IEditorPart targetEditor = window.getActivePage().getActiveEditor();
 
  41                         if (targetEditor != null
 
  42                                         && (targetEditor instanceof AbstractTextEditor)) {
 
  43                                 editor = (AbstractTextEditor) targetEditor;
 
  48                         IWorkbenchPage page = window.getActivePage();
 
  50                                 IViewPart part = page.findView(BrowserView.ID_BROWSER);
 
  52                                         part = page.showView(BrowserView.ID_BROWSER);
 
  54                                         page.bringToTop(part);
 
  56                                 Configuration config = getConfiguration(null);
 
  57                                 String templateString = generateUrl(config, config.getURL());
 
  58                                 if (templateString != null && !templateString.equals("")) {
 
  59                                         ((BrowserView) part).setUrl(templateString);
 
  61                                         ((BrowserView) part).setUrl(config.getURL());
 
  63                         } catch (Exception e) {
 
  68         public void selectionChanged(IAction action, ISelection selection) {
 
  71         public IDocument getDocument() {
 
  72                 IDocument doc = editor.getDocumentProvider().getDocument(
 
  73                                 editor.getEditorInput());
 
  77         public static String getSelectedText(AbstractTextEditor editor,
 
  78                         IDocument document, int initialPos) {
 
  81                         int line = document.getLineOfOffset(pos);
 
  82                         int start = document.getLineOffset(line);
 
  83                         int end = start + document.getLineInformation(line).getLength();
 
  86                          * The line does not include \n or \r so pos can be > end. Making
 
  87                          * pos = end in this case is safe for the purposes of determining
 
  88                          * the TextRegion at the cursor position
 
  94                         int offsetInLine = pos - start;
 
  95                         String word = document.get(start, end - start);
 
  96                         int wordlen = word.length();
 
 100                         for (int i = offsetInLine; i < wordlen; i++) {
 
 101                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
 
 106                         for (int i = offsetInLine; i >= 0; i--) {
 
 107                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
 
 112                         if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
 
 113                                 return new String(word.toCharArray(), textStart, textEnd
 
 116                 } catch (Exception e) {
 
 122         public String generateUrl(Configuration config, String template) {
 
 123                 IDocument doc = getDocument();
 
 124                 ITextSelection selection = (ITextSelection) editor
 
 125                                 .getSelectionProvider().getSelection();
 
 126                 int pos = selection.getOffset();
 
 127                 int len = selection.getLength();
 
 131                                 wikiTitle = doc.get(pos, len);
 
 132                         } catch (BadLocationException e) {
 
 136                         wikiTitle = getSelectedText(editor, doc, pos);
 
 139                 if (wikiTitle != null && !wikiTitle.equals("")) {
 
 140                         template = template.replaceAll("\\$text.selection", wikiTitle);
 
 141                         wikiTitle = wikiTitle.replaceAll("_", "-");
 
 142                         template = template.replaceAll("\\$php.selection", wikiTitle);