Moved Google and Koders Search to the wiki plugin
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / httpquery / AbstractHTTPQueryAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.httpquery;
2
3 import java.io.StringWriter;
4 import java.net.URL;
5
6 import net.sourceforge.phpeclipse.webbrowser.views.BrowserView;
7 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationWorkingCopy;
8 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
9 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
10
11 import org.apache.velocity.VelocityContext;
12 import org.apache.velocity.app.Velocity;
13 import org.eclipse.jface.action.IAction;
14 import org.eclipse.jface.viewers.ISelection;
15 import org.eclipse.ui.IEditorActionDelegate;
16 import org.eclipse.ui.IEditorPart;
17 import org.eclipse.ui.IViewPart;
18 import org.eclipse.ui.IWorkbenchPage;
19 import org.eclipse.ui.IWorkbenchWindow;
20 import org.eclipse.ui.PlatformUI;
21
22 public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
23
24   private IEditorPart targetEditor;
25   private EditorText text;
26   public AbstractHTTPQueryAction() {
27     super();
28   }
29
30   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
31     this.targetEditor = targetEditor;
32     text = new EditorText(targetEditor);
33   }
34
35   abstract protected IConfiguration getUrl();
36
37   public void run(IAction action) {
38 //    String selection = findSelectedText();
39 //    if (selection == null || selection.trim().length() == 0) {
40 //      selection = "";
41 //    }
42     URL url;
43     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
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         IConfiguration config = getUrl();
54         String templateString = generateUrl(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 String generateUrl(String template) {
67
68     /* first, we init the runtime engine. Defaults are fine. */
69
70     try {
71       Velocity.init();
72
73       /* lets make a Context and put data into it */
74
75       VelocityContext context = new VelocityContext();
76       
77       ConfigurationWorkingCopy config = new ConfigurationWorkingCopy();
78       config.setName("test");
79       config.setPassword("pw");
80       
81       context.put("config", config);
82       text.clear();
83       context.put("text", text);  
84
85       /* lets make our own string to render */
86       StringWriter w = new StringWriter();
87       w = new StringWriter();
88       Velocity.evaluate(context, w, "mystring", template);
89       return w.toString();
90
91     } catch (Exception e) {
92       // TODO Auto-generated catch block
93       e.printStackTrace();
94     }
95     return template;
96   }
97 }