Set version 1.1.9
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / AbstractHTTPQueryAction.java
1 package net.sourceforge.phpdt.httpquery;
2
3 import net.sourceforge.phpdt.httpquery.config.Configuration;
4 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
5
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;
18
19 public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
20
21         private AbstractTextEditor editor;
22
23         public AbstractHTTPQueryAction() {
24                 super();
25         }
26
27         abstract protected Configuration getConfiguration(String name);
28
29         public void setActiveEditor(IAction action, IEditorPart targetEditor) {
30                 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
31                         editor = (AbstractTextEditor) targetEditor;
32                 }
33         }
34
35         public void run(IAction action) {
36                 IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
37                 if (editor == null) {
38                         IEditorPart targetEditor = window.getActivePage().getActiveEditor();
39                         if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
40                                 editor = (AbstractTextEditor) targetEditor;
41                         }
42                 }
43
44                 if (window != null) {
45                         IWorkbenchPage page = window.getActivePage();
46                         try {
47                                 IViewPart part = page.findView(BrowserView.ID_BROWSER);
48                                 if (part == null) {
49                                         part = page.showView(BrowserView.ID_BROWSER);
50                                 } else {
51                                         page.bringToTop(part);
52                                 }
53                                 Configuration config = getConfiguration(null);
54                                 String templateString = generateUrl(config, config.getURL());
55                                 if (templateString != null && !templateString.equals("")) {
56                                         ((BrowserView) part).setUrl(templateString);
57                                 } else {
58                                         ((BrowserView) part).setUrl(config.getURL());
59                                 }
60                         } catch (Exception e) {
61                         }
62                 }
63         }
64
65         public void selectionChanged(IAction action, ISelection selection) {
66         }
67
68         public IDocument getDocument() {
69                 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
70                 return doc;
71         }
72
73         public static String getSelectedText(AbstractTextEditor editor, IDocument document, int initialPos) {
74                 try {
75                         int pos = initialPos;
76                         int line = document.getLineOfOffset(pos);
77                         int start = document.getLineOffset(line);
78                         int end = start + document.getLineInformation(line).getLength();
79
80                         /*
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
84                          */
85                         if (pos > end) {
86                                 pos = end;
87                         }
88
89                         int offsetInLine = pos - start;
90                         String word = document.get(start, end - start);
91                         int wordlen = word.length();
92                         int textStart = -1;
93                         int textEnd = -1;
94
95                         for (int i = offsetInLine; i < wordlen; i++) {
96                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
97                                         textEnd = i;
98                                         break;
99                                 }
100                         }
101                         for (int i = offsetInLine; i >= 0; i--) {
102                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
103                                         textStart = i + 1;
104                                         break;
105                                 }
106                         }
107                         if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
108                                 return new String(word.toCharArray(), textStart, textEnd - textStart);
109                         }
110                 } catch (Exception e) {
111
112                 }
113                 return null;
114         }
115
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();
121                 String wikiTitle;
122                 if (len > 0) {
123                         try {
124                                 wikiTitle = doc.get(pos, len);
125                         } catch (BadLocationException e) {
126                                 wikiTitle = null;
127                         }
128                 } else {
129                         wikiTitle = getSelectedText(editor, doc, pos);
130                 }
131
132                 if (wikiTitle != null && !wikiTitle.equals("")) {
133                         template = template.replaceAll("\\$text.selection", wikiTitle);
134                         wikiTitle = wikiTitle.replaceAll("_", "-");
135                         template = template.replaceAll("\\$php.selection", wikiTitle);
136                         return template;
137                 }
138                 return null;
139         }
140 }