6d364863ec0d528b8c1c8d0a07df841b33073a86
[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();
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();
54                                 String templateString = generateUrl(config, config.getURL());
55                                 if (templateString != null && !templateString.equals("")) {
56                                         ((BrowserView) part).setUrl(templateString);
57                                 }
58                         } catch (Exception e) {
59                         }
60                 }
61         }
62
63         public void selectionChanged(IAction action, ISelection selection) {
64         }
65
66         public IDocument getDocument() {
67                 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
68                 return doc;
69         }
70
71         public static String getSelectedText(AbstractTextEditor editor, IDocument document, int initialPos) {
72                 try {
73                         int pos = initialPos;
74                         int line = document.getLineOfOffset(pos);
75                         int start = document.getLineOffset(line);
76                         int end = start + document.getLineInformation(line).getLength();
77
78                         /*
79                          * The line does not include \n or \r so pos can be > end. Making pos =
80                          * end in this case is safe for the purposes of determining the TextRegion
81                          * at the cursor position
82                          */
83                         if (pos > end) {
84                                 pos = end;
85                         }
86
87                         int offsetInLine = pos - start;
88                         String word = document.get(start, end - start);
89                         int wordlen = word.length();
90                         int textStart = -1;
91                         int textEnd = -1;
92
93                         for (int i = offsetInLine; i < wordlen; i++) {
94                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
95                                         textEnd = i;
96                                         break;
97                                 }
98                         }
99                         for (int i = offsetInLine; i >= 0; i--) {
100                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
101                                         textStart = i + 1;
102                                         break;
103                                 }
104                         }
105                         if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
106                                 return new String(word.toCharArray(), textStart, textEnd - textStart);
107                         }
108                 } catch (Exception e) {
109
110                 }
111                 return null;
112         }
113
114         public String generateUrl(Configuration config, String template) {
115                 IDocument doc = getDocument();
116                 ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
117                 int pos = selection.getOffset();
118                 int len = selection.getLength();
119                 String wikiTitle;
120                 if (len > 0) {
121                         try {
122                                 wikiTitle = doc.get(pos, len);
123                         } catch (BadLocationException e) {
124                                 wikiTitle = null;
125                         }
126                 } else {
127                         wikiTitle = getSelectedText(editor, doc, pos);
128                 }
129
130                 if (wikiTitle != null && !wikiTitle.equals("")) {
131                         template = template.replaceAll("\\$text.selection", wikiTitle);
132                         wikiTitle = wikiTitle.replaceAll("_", "-");
133                         template = template.replaceAll("\\$php.selection", wikiTitle);
134                         return template;
135                 }
136                 return null;
137         }
138 }