package net.sourceforge.phpeclipse.wiki.actions.httpquery; import java.io.StringWriter; import java.net.URL; import net.sourceforge.phpeclipse.webbrowser.views.BrowserView; import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy; import net.sourceforge.phpeclipse.wiki.internal.IConfiguration; 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.viewers.ISelection; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.PlatformUI; 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 IConfiguration getUrl(); public void run(IAction action) { // String selection = findSelectedText(); // if (selection == null || selection.trim().length() == 0) { // selection = ""; // } 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); } IConfiguration config = getUrl(); String templateString = generateUrl(config.getURL()); if (templateString != null && !templateString.equals("")) { ((BrowserView) part).setUrl(templateString); } } catch (Exception e) { } } } public void selectionChanged(IAction action, ISelection selection) { } public String generateUrl(String template) { /* first, we init the runtime engine. Defaults are fine. */ try { Velocity.init(); /* lets make a Context and put data into it */ VelocityContext context = new VelocityContext(); ConfigurationWorkingCopy config = new ConfigurationWorkingCopy(); config.setName("test"); config.setPassword("pw"); 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 template; } }