Upload/Download of multiple files now possible
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / DownloadWikipediaAction.java
index cdbff14..26b6fcc 100644 (file)
@@ -1,4 +1,5 @@
 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
+
 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
 //http://www.djini.de/software/wikipedia/index.html
 //
@@ -6,10 +7,12 @@ package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
 //with permission from the original author: Daniel Wunsch
 
 import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
@@ -22,6 +25,7 @@ import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
 import org.apache.velocity.VelocityContext;
 import org.apache.velocity.app.Velocity;
 import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.text.IDocument;
 import org.eclipse.jface.text.ITextSelection;
@@ -45,18 +49,23 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
 
   private IWorkbenchWindow window;
 
-  
-
   public void dispose() {
   }
 
-  public String generateUrl(Configuration config, String template, String wikiname) {
+  public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
 
     /* first, we init the runtime engine. Defaults are fine. */
 
     try {
       Velocity.init();
 
+      if (template == null || template.equals("")) {
+        // fall back to default settings
+        // Example:
+        // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
+        template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
+      }
+
       /* lets make a Context and put data into it */
 
       VelocityContext context = new VelocityContext();
@@ -79,12 +88,12 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
     return template;
   }
 
-  protected Configuration getConfiguration(){
+  protected Configuration getConfigurationPrefix(String prefix) {
     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
     ArrayList configsList = new ArrayList();
     for (int i = 0; i < allConfigsList.size(); i++) {
       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
-      if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
+      if (temp.getType().startsWith(prefix)) {
         configsList.add(temp);
       }
     }
@@ -105,14 +114,17 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
     }
     return configuration;
   }
+  protected Configuration getConfiguration( ) {
+    return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
+  }
 
   public IDocument getDocument() {
     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
     return doc;
   }
 
-  private String getWikiFile(IFile file) {
-    return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
+  private static String getWikiFile(IFile file) {
+    return Util.getFileWikiName(file);
   }
 
   public void init(IWorkbenchWindow window) {
@@ -123,7 +135,7 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
     String wikiName = getWikiFile(cfile);
     try {
       if (fEditor != null) {
-        selectWiki(wikiName);
+        selectWiki(cfile, wikiName);
       }
     } catch (Exception e) {
     }
@@ -162,15 +174,33 @@ public class DownloadWikipediaAction implements IEditorActionDelegate {
       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
     }
   }
-  
-  private void selectWiki(String wikiName) {
+
+  private void selectWiki(IFile cfile, String wikiName) {
     Configuration configuration = getConfiguration();
     if (configuration != null && !configuration.equals("")) {
-      String url = generateUrl(configuration, configuration.getURL(), wikiName);
-      String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
-      if (wikiContent != null) {
-        IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
-        doc.set(wikiContent);
+      try {
+        String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
+        IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
+
+        //        String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
+        //        MediaWikiConnector mc = new MediaWikiConnector();
+        //        String wikiContent = mc.getWikiRawText(wikiName, url);
+        //        if (wikiContent != null) {
+        //          IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
+        //          doc.set(wikiContent);
+        //        }
+        IFile[] files = new IFile[1];
+        files[0] = cfile;
+
+        Job job = new RefreshJob(wikipediaProperties, files, configuration.getURL());
+        job.setRule(null);
+        job.setUser(true);
+        job.setPriority(Job.SHORT);
+        job.schedule();
+      } catch (Exception e) {
+        e.printStackTrace();
+        WikiEditorPlugin.getDefault().reportError("Exception occured: ",
+            e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
       }
     }
   }