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
index db83d2c..1aaad74 100644 (file)
@@ -1,14 +1,16 @@
 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.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.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;
@@ -16,28 +18,27 @@ import org.eclipse.ui.IViewPart;
 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 IConfiguration getUrl();
 
   public void run(IAction action) {
-    String selection = findSelectedText();
-    if (selection == null || selection.trim().length() == 0) {
-      selection = "";
-    }
+//    String selection = findSelectedText();
+//    if (selection == null || selection.trim().length() == 0) {
+//      selection = "";
+//    }
     URL url;
     IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
     if (window != null) {
@@ -49,9 +50,10 @@ public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
         } else {
           page.bringToTop(part);
         }
-        String urlStr = getUrl(selection);
-        if (urlStr != null && !urlStr.equals("")) {
-          ((BrowserView) part).setUrl(urlStr);
+        IConfiguration config = getUrl();
+        String templateString = generateUrl(config.getURL());
+        if (templateString != null && !templateString.equals("")) {
+          ((BrowserView) part).setUrl(templateString);
         }
       } catch (Exception e) {
       }
@@ -61,37 +63,35 @@ public abstract class AbstractHTTPQueryAction implements IEditorActionDelegate {
   public void selectionChanged(IAction action, ISelection selection) {
   }
 
-  protected String findSelectedText() {
-    String selectedText = null;
-    ITextSelection textSelection = (ITextSelection) targetEditor.getEditorSite().getSelectionProvider().getSelection();
+  public String generateUrl(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();
+      
+      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 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