improved settings dialogs/improved HTTP Query action
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / httpquery / AbstractHTTPQueryAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.httpquery;
2
3 import java.net.URL;
4 import java.text.BreakIterator;
5
6 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
7
8 import org.eclipse.jface.action.IAction;
9 import org.eclipse.jface.text.IDocument;
10 import org.eclipse.jface.text.IRegion;
11 import org.eclipse.jface.text.ITextSelection;
12 import org.eclipse.jface.viewers.ISelection;
13 import org.eclipse.ui.IEditorActionDelegate;
14 import org.eclipse.ui.IEditorPart;
15 import org.eclipse.ui.IViewPart;
16 import org.eclipse.ui.IWorkbenchPage;
17 import org.eclipse.ui.IWorkbenchWindow;
18 import org.eclipse.ui.PlatformUI;
19 import org.eclipse.ui.texteditor.IDocumentProvider;
20 import org.eclipse.ui.texteditor.ITextEditor;
21
22 public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
23
24   private IEditorPart targetEditor;
25
26   public AbstractHTTPQueryAction() {
27     super();
28   }
29
30   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
31     this.targetEditor = targetEditor;
32   }
33
34   abstract protected String getUrl(String selection);
35
36   public void run(IAction action) {
37     String selection = findSelectedText();
38     if (selection == null || selection.trim().length() == 0) {
39       selection = "";
40     }
41     URL url;
42     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
43     if (window != null) {
44       IWorkbenchPage page = window.getActivePage();
45       try {
46         IViewPart part = page.findView(BrowserView.ID_BROWSER);
47         if (part == null) {
48           part = page.showView(BrowserView.ID_BROWSER);
49         } else {
50           page.bringToTop(part);
51         }
52         String urlStr = getUrl(selection);
53         if (urlStr != null && !urlStr.equals("")) {
54           ((BrowserView) part).setUrl(urlStr);
55         }
56       } catch (Exception e) {
57       }
58     }
59   }
60
61   public void selectionChanged(IAction action, ISelection selection) {
62   }
63
64   protected String findSelectedText() {
65     String selectedText = null;
66     ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
67
68     selectedText = textSelection.getText();
69     if (selectedText == null || selectedText.trim().length() == 0) {
70       selectedText = findWord(textSelection);
71     }
72     return selectedText;
73   }
74
75   private String findWord(ITextSelection textSelection) {
76     IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
77     IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
78     int caretPosition = textSelection.getOffset();
79     try {
80       IRegion line = document.getLineInformation(document.getLineOfOffset(caretPosition));
81       String currentLine = document.get(line.getOffset(), line.getLength());
82       int positionInLine = caretPosition - line.getOffset();
83       return findWordAt(positionInLine, currentLine);
84     } catch (Exception e) {
85     }
86     return null;
87   }
88
89   private String findWordAt(int pos, String source) {
90     BreakIterator boundary = BreakIterator.getWordInstance();
91     boundary.setText(source);
92     int end = boundary.following(pos);
93     int start = boundary.previous();
94     return source.substring(start, end);
95   }
96
97 }