1) Added missing strings for italic, underline and strike through.
[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
31                                 && (targetEditor instanceof AbstractTextEditor)) {
32                         editor = (AbstractTextEditor) targetEditor;
33                 }
34         }
35
36         public void run(IAction action) {
37                 IWorkbenchWindow window = PlatformUI.getWorkbench()
38                                 .getActiveWorkbenchWindow();
39                 if (editor == null) {
40                         IEditorPart targetEditor = window.getActivePage().getActiveEditor();
41                         if (targetEditor != null
42                                         && (targetEditor instanceof AbstractTextEditor)) {
43                                 editor = (AbstractTextEditor) targetEditor;
44                         }
45                 }
46
47                 if (window != null) {
48                         IWorkbenchPage page = window.getActivePage();
49                         try {
50                                 IViewPart part = page.findView(BrowserView.ID_BROWSER);
51                                 if (part == null) {
52                                         part = page.showView(BrowserView.ID_BROWSER);
53                                 } else {
54                                         page.bringToTop(part);
55                                 }
56                                 Configuration config = getConfiguration(null);
57                                 String templateString = generateUrl(config, config.getURL());
58                                 if (templateString != null && !templateString.equals("")) {
59                                         ((BrowserView) part).setUrl(templateString);
60                                 } else {
61                                         ((BrowserView) part).setUrl(config.getURL());
62                                 }
63                         } catch (Exception e) {
64                         }
65                 }
66         }
67
68         public void selectionChanged(IAction action, ISelection selection) {
69         }
70
71         public IDocument getDocument() {
72                 IDocument doc = editor.getDocumentProvider().getDocument(
73                                 editor.getEditorInput());
74                 return doc;
75         }
76
77         public static String getSelectedText(AbstractTextEditor editor,
78                         IDocument document, int initialPos) {
79                 try {
80                         int pos = initialPos;
81                         int line = document.getLineOfOffset(pos);
82                         int start = document.getLineOffset(line);
83                         int end = start + document.getLineInformation(line).getLength();
84
85                         /*
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
89                          */
90                         if (pos > end) {
91                                 pos = end;
92                         }
93
94                         int offsetInLine = pos - start;
95                         String word = document.get(start, end - start);
96                         int wordlen = word.length();
97                         int textStart = -1;
98                         int textEnd = -1;
99
100                         for (int i = offsetInLine; i < wordlen; i++) {
101                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
102                                         textEnd = i;
103                                         break;
104                                 }
105                         }
106                         for (int i = offsetInLine; i >= 0; i--) {
107                                 if (!Character.isJavaIdentifierPart(word.charAt(i))) {
108                                         textStart = i + 1;
109                                         break;
110                                 }
111                         }
112                         if (textStart != (-1) && textEnd != (-1) && textStart < textEnd) {
113                                 return new String(word.toCharArray(), textStart, textEnd
114                                                 - textStart);
115                         }
116                 } catch (Exception e) {
117
118                 }
119                 return null;
120         }
121
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();
128                 String wikiTitle;
129                 if (len > 0) {
130                         try {
131                                 wikiTitle = doc.get(pos, len);
132                         } catch (BadLocationException e) {
133                                 wikiTitle = null;
134                         }
135                 } else {
136                         wikiTitle = getSelectedText(editor, doc, pos);
137                 }
138
139                 if (wikiTitle != null && !wikiTitle.equals("")) {
140                         template = template.replaceAll("\\$text.selection", wikiTitle);
141                         wikiTitle = wikiTitle.replaceAll("_", "-");
142                         template = template.replaceAll("\\$php.selection", wikiTitle);
143                         return template;
144                 }
145                 return null;
146         }
147 }