package net.sourceforge.phpeclipse.wiki.actions.httpquery;
+import java.io.StringWriter;
import java.net.URL;
-import java.text.BreakIterator;
import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
+import net.sourceforge.phpeclipse.wiki.internal.Configuration;
+import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy;
+import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
+import org.apache.velocity.VelocityContext;
+import org.apache.velocity.app.Velocity;
import org.eclipse.jface.action.IAction;
-import org.eclipse.jface.text.IDocument;
-import org.eclipse.jface.text.IRegion;
-import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorActionDelegate;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
-import org.eclipse.ui.texteditor.IDocumentProvider;
-import org.eclipse.ui.texteditor.ITextEditor;
public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
private IEditorPart targetEditor;
-
+ private EditorText text;
public AbstractHTTPQueryAction() {
super();
}
public void setActiveEditor(IAction action, IEditorPart targetEditor) {
this.targetEditor = targetEditor;
+ text = new EditorText(targetEditor);
}
- abstract protected String getUrl(String selection);
+ abstract protected Configuration getConfiguration();
public void run(IAction action) {
- String selection = findSelectedText();
- if (selection != null && selection.trim().length() > 0) {
- URL url;
- IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
- if (window != null) {
- IWorkbenchPage page = window.getActivePage();
- try {
- IViewPart part = page.findView(BrowserView.ID_BROWSER);
- if (part == null) {
- part = page.showView(BrowserView.ID_BROWSER);
- } else {
- page.bringToTop(part);
- }
- String urlStr = getUrl(selection);
- if (urlStr != null && !urlStr.equals("")) {
- ((BrowserView) part).setUrl(urlStr);
- }
- } catch (Exception e) {
+ URL url;
+ IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
+ if (window != null) {
+ IWorkbenchPage page = window.getActivePage();
+ try {
+ IViewPart part = page.findView(BrowserView.ID_BROWSER);
+ if (part == null) {
+ part = page.showView(BrowserView.ID_BROWSER);
+ } else {
+ page.bringToTop(part);
+ }
+ Configuration config = getConfiguration();
+ String templateString = generateUrl(config, config.getURL());
+ if (templateString != null && !templateString.equals("")) {
+ ((BrowserView) part).setUrl(templateString);
}
+ } catch (Exception e) {
}
}
}
public void selectionChanged(IAction action, ISelection selection) {
}
- protected String findSelectedText() {
- String selectedText = null;
- ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
+ public String generateUrl(Configuration config, String template) {
- selectedText = textSelection.getText();
- if (selectedText == null || selectedText.trim().length() == 0) {
- selectedText = findWord(textSelection);
- }
- return selectedText;
- }
+ /* first, we init the runtime engine. Defaults are fine. */
- private String findWord(ITextSelection textSelection) {
- IDocumentProvider documentProvider = ((ITextEditor) targetEditor).getDocumentProvider();
- IDocument document = documentProvider.getDocument(targetEditor.getEditorInput());
- int caretPosition = textSelection.getOffset();
try {
- IRegion line = document.getLineInformation(document.getLineOfOffset(caretPosition));
- String currentLine = document.get(line.getOffset(), line.getLength());
- int positionInLine = caretPosition - line.getOffset();
- return findWordAt(positionInLine, currentLine);
+ Velocity.init();
+
+ /* lets make a Context and put data into it */
+
+ VelocityContext context = new VelocityContext();
+
+ context.put("config", config);
+ text.clear();
+ context.put("text", text);
+
+ /* lets make our own string to render */
+ StringWriter w = new StringWriter();
+ w = new StringWriter();
+ Velocity.evaluate(context, w, "mystring", template);
+ return w.toString();
+
} catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
}
- return null;
+ return template;
}
-
- private String findWordAt(int pos, String source) {
- BreakIterator boundary = BreakIterator.getWordInstance();
- boundary.setText(source);
- int end = boundary.following(pos);
- int start = boundary.previous();
- return source.substring(start, end);
- }
-
}
\ No newline at end of file