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
1 package net.sourceforge.phpeclipse.wiki.actions.category;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6
7 import net.sourceforge.phpeclipse.wiki.actions.CreateFilesJob;
8 import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction;
9 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Loaded;
10 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
11 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
12 import net.sourceforge.phpeclipse.wiki.preferences.Util;
13
14 import org.eclipse.core.resources.IFile;
15 import org.eclipse.core.runtime.jobs.Job;
16 import org.eclipse.jface.text.IDocument;
17 import org.eclipse.jface.viewers.LabelProvider;
18 import org.eclipse.jface.window.Window;
19 import org.eclipse.swt.widgets.Shell;
20 import org.eclipse.ui.IFileEditorInput;
21 import org.eclipse.ui.dialogs.ListSelectionDialog;
22 import org.eclipse.ui.internal.dialogs.ListContentProvider;
23
24 public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEditorAction {
25
26   class WikiFile implements Comparable {
27     IFile file;
28
29     String wikiTitle;
30
31     public WikiFile(IFile f, String title) {
32       file = f;
33       wikiTitle = title;
34     }
35
36     /*
37      * (non-Javadoc)
38      * 
39      * @see java.lang.Object#toString()
40      */
41     public String toString() {
42       return wikiTitle + " - " + file.getProjectRelativePath().toString();
43     }
44     /* (non-Javadoc)
45      * @see java.lang.Comparable#compareTo(java.lang.Object)
46      */
47     public int compareTo(Object o) {
48       return wikiTitle.compareTo(((WikiFile)o).wikiTitle);
49     }
50   }
51
52   public void openWikiLinkOnSelection() {
53     IDocument doc = getDocument();
54     Shell shell = Util.findShell();
55     if (shell == null) {
56       return;
57     }
58     CategoryDialog dialog = new CategoryDialog(shell);//$NON-NLS-1$
59     dialog.open();
60     String category = dialog.getCategory();
61     
62     // TODO: this doesn#t work at the moment:
63     MediaWikiConnector connector = new MediaWikiConnector();
64       Loaded page = null;
65 //        connector.loadCategory(actionUrl, wikipedia.getCharSet(), "plog4u.org bot");
66       
67     ParseCategory pc = new ParseCategory();
68     pc.parseCategory(page.getContent().getBody());
69     
70     ArrayList startPositionList = pc.getTitleList();
71
72     HashSet wikiNames = new HashSet();
73     ArrayList filesList = new ArrayList();
74     ArrayList wikiList = new ArrayList();
75     String wikiTitle;
76     Integer posInteger;
77     IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
78
79     for (int i = 0; i < startPositionList.size(); i++) {
80       wikiTitle = (String) startPositionList.get(i);
81
82       if (wikiTitle != null && !wikiTitle.equals("")) {
83         if (!wikiNames.contains(wikiTitle)) {
84           IFile file = getWikiFile(currentFile, wikiTitle);
85           if (!file.exists()) {
86             filesList.add(new WikiFile(file, wikiTitle));
87           }
88           wikiNames.add(wikiTitle);
89         }
90       }
91     }
92
93     if (filesList.size() > 0) {
94       Collections.sort(filesList);
95       ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
96           .getActiveWorkbenchWindow().getShell(), filesList, new ListContentProvider(), new LabelProvider(),
97           "Select the links for file creation:");
98       listSelectionDialog.setTitle("Links found in this article");
99       if (listSelectionDialog.open() == Window.OK) {
100         Object[] locations = listSelectionDialog.getResult();
101         if (locations.length > 0) {
102           IFile[] files = new IFile[locations.length];
103           String[] wikiTitles = new String[locations.length];
104           for (int i = 0; i < files.length; i++) {
105             files[i] = ((WikiFile) locations[i]).file;
106             wikiTitles[i] = ((WikiFile) locations[i]).wikiTitle;
107           }
108           
109           Job job = new CreateFilesJob(files, wikiTitles);
110           //        job.setRule(createRule(files));
111           job.setRule(null);
112           job.setUser(true);
113           job.schedule();
114         }
115       }
116
117     }
118   }
119
120 }