Enable word wrapping with preference key editor.wrap.words (false by default)
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / category / CreateFilesFromCategoryEditorAction.java
diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/category/CreateFilesFromCategoryEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/category/CreateFilesFromCategoryEditorAction.java
new file mode 100644 (file)
index 0000000..fccc4e7
--- /dev/null
@@ -0,0 +1,120 @@
+package net.sourceforge.phpeclipse.wiki.actions.category;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+
+import net.sourceforge.phpeclipse.wiki.actions.CreateFilesJob;
+import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Loaded;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
+import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
+import net.sourceforge.phpeclipse.wiki.preferences.Util;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.runtime.jobs.Job;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.viewers.LabelProvider;
+import org.eclipse.jface.window.Window;
+import org.eclipse.swt.widgets.Shell;
+import org.eclipse.ui.IFileEditorInput;
+import org.eclipse.ui.dialogs.ListSelectionDialog;
+import org.eclipse.ui.internal.dialogs.ListContentProvider;
+
+public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEditorAction {
+
+  class WikiFile implements Comparable {
+    IFile file;
+
+    String wikiTitle;
+
+    public WikiFile(IFile f, String title) {
+      file = f;
+      wikiTitle = title;
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see java.lang.Object#toString()
+     */
+    public String toString() {
+      return wikiTitle + " - " + file.getProjectRelativePath().toString();
+    }
+    /* (non-Javadoc)
+     * @see java.lang.Comparable#compareTo(java.lang.Object)
+     */
+    public int compareTo(Object o) {
+      return wikiTitle.compareTo(((WikiFile)o).wikiTitle);
+    }
+  }
+
+  public void openWikiLinkOnSelection() {
+    IDocument doc = getDocument();
+    Shell shell = Util.findShell();
+    if (shell == null) {
+      return;
+    }
+    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");
+      
+    ParseCategory pc = new ParseCategory();
+    pc.parseCategory(page.getContent().getBody());
+    
+    ArrayList startPositionList = pc.getTitleList();
+
+    HashSet wikiNames = new HashSet();
+    ArrayList filesList = new ArrayList();
+    ArrayList wikiList = new ArrayList();
+    String wikiTitle;
+    Integer posInteger;
+    IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
+
+    for (int i = 0; i < startPositionList.size(); i++) {
+      wikiTitle = (String) startPositionList.get(i);
+
+      if (wikiTitle != null && !wikiTitle.equals("")) {
+        if (!wikiNames.contains(wikiTitle)) {
+          IFile file = getWikiFile(currentFile, wikiTitle);
+          if (!file.exists()) {
+            filesList.add(new WikiFile(file, wikiTitle));
+          }
+          wikiNames.add(wikiTitle);
+        }
+      }
+    }
+
+    if (filesList.size() > 0) {
+      Collections.sort(filesList);
+      ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
+          .getActiveWorkbenchWindow().getShell(), filesList, new ListContentProvider(), new LabelProvider(),
+          "Select the links for file creation:");
+      listSelectionDialog.setTitle("Links found in this article");
+      if (listSelectionDialog.open() == Window.OK) {
+        Object[] locations = listSelectionDialog.getResult();
+        if (locations.length > 0) {
+          IFile[] files = new IFile[locations.length];
+          String[] wikiTitles = new String[locations.length];
+          for (int i = 0; i < files.length; i++) {
+            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);
+          job.setUser(true);
+          job.schedule();
+        }
+      }
+
+    }
+  }
+
+}
\ No newline at end of file