get category
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / category / CreateFilesFromCategoryEditorAction.java
index fccc4e7..7c56834 100644 (file)
@@ -1,14 +1,24 @@
 package net.sourceforge.phpeclipse.wiki.actions.category;
 
+import java.lang.reflect.InvocationTargetException;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashSet;
+import java.util.List;
 
 import net.sourceforge.phpeclipse.wiki.actions.CreateFilesJob;
 import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction;
+import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Loaded;
 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.internal.Configuration;
+import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
+import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
 import net.sourceforge.phpeclipse.wiki.preferences.Util;
 
 import org.eclipse.core.resources.IFile;
@@ -41,15 +51,53 @@ public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEdito
     public String toString() {
       return wikiTitle + " - " + file.getProjectRelativePath().toString();
     }
-    /* (non-Javadoc)
+
+    /*
+     * (non-Javadoc)
+     * 
      * @see java.lang.Comparable#compareTo(java.lang.Object)
      */
     public int compareTo(Object o) {
-      return wikiTitle.compareTo(((WikiFile)o).wikiTitle);
+      return wikiTitle.compareTo(((WikiFile) o).wikiTitle);
+    }
+  }
+
+  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().startsWith(prefix)) {
+        configsList.add(temp);
+      }
+    }
+    if (configsList.size() == 1) {
+      return (Configuration) configsList.get(0);
+    }
+    Collections.sort(configsList);
+    Configuration configuration = null;
+    ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
+        .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
+        "Select the refresh URL.");
+    listSelectionDialog.setTitle("Multiple active configuration found");
+    if (listSelectionDialog.open() == Window.OK) {
+      Object[] locations = listSelectionDialog.getResult();
+      if (locations != null) {
+        for (int i = 0; i < locations.length; i++) {
+          configuration = (Configuration) locations[i];
+          break;
+        }
+      }
     }
+    return configuration;
+  }
+
+  protected Configuration getConfiguration() {
+    return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
   }
 
   public void openWikiLinkOnSelection() {
+    ProblemConsole console = new ProblemConsole();
     IDocument doc = getDocument();
     Shell shell = Util.findShell();
     if (shell == null) {
@@ -58,15 +106,40 @@ public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEdito
     CategoryDialog dialog = new CategoryDialog(shell);//$NON-NLS-1$
     dialog.open();
     String category = dialog.getCategory();
-    
-    // TODO: this doesn#t work at the moment:
-    MediaWikiConnector connector = new MediaWikiConnector();
-      Loaded page = null;
-//        connector.loadCategory(actionUrl, wikipedia.getCharSet(), "plog4u.org bot");
-      
+    if (category == null || category.length() == 0) {
+      return;
+    }
+    category = category.replaceAll(" ", "_");
+    Configuration configuration = getConfiguration();
+    String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
+    String htmlContent = null;
+    try {
+      IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
+
+      MediaWikiConnector connector = new MediaWikiConnector();
+      String actionUrl = wikipediaProperties.getActionUrl() + "/Category:" + category;
+      htmlContent = connector.loadHTMLPage(actionUrl, wikipediaProperties.getCharSet());
+    } catch (UnexpectedAnswerException e) {
+      console.println("UnexpectedAnswerException: " + e.getMessage());
+    } catch (MethodException e) {
+      console.println("HTTP-MethodException: " + e.getMessage());
+    } catch (PageNotEditableException e) {
+      console.println("PageNotEditableException: " + e.getMessage());
+    } catch (NoSuchMethodException e) {
+      console.println("NoSuchMethodException: " + e.getMessage());
+    } catch (IllegalAccessException e) {
+      console.println("IllegalAccessException: " + e.getMessage());
+    } catch (ClassNotFoundException e) {
+      console.println("ClassNotFoundException: " + e.getMessage());
+    } catch (InvocationTargetException e) {
+      console.println("InvocationTargetException: " + e.getMessage());
+    }
+    if (htmlContent == null) {
+      return;
+    }
     ParseCategory pc = new ParseCategory();
-    pc.parseCategory(page.getContent().getBody());
-    
+    pc.parseCategory(htmlContent);
+
     ArrayList startPositionList = pc.getTitleList();
 
     HashSet wikiNames = new HashSet();
@@ -105,7 +178,7 @@ public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEdito
             files[i] = ((WikiFile) locations[i]).file;
             wikiTitles[i] = ((WikiFile) locations[i]).wikiTitle;
           }
-          
+
           Job job = new CreateFilesJob(files, wikiTitles);
           //        job.setRule(createRule(files));
           job.setRule(null);