1 package net.sourceforge.phpeclipse.wiki.actions.category;
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashSet;
9 import net.sourceforge.phpeclipse.wiki.actions.CreateFilesJob;
10 import net.sourceforge.phpeclipse.wiki.actions.OpenWikiLinkEditorAction;
11 import net.sourceforge.phpeclipse.wiki.actions.ProblemConsole;
12 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Loaded;
14 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
15 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.MethodException;
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.PageNotEditableException;
17 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.exceptions.UnexpectedAnswerException;
18 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
19 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
20 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
21 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
22 import net.sourceforge.phpeclipse.wiki.preferences.Util;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.core.runtime.jobs.Job;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.viewers.LabelProvider;
28 import org.eclipse.jface.window.Window;
29 import org.eclipse.swt.widgets.Shell;
30 import org.eclipse.ui.IFileEditorInput;
31 import org.eclipse.ui.dialogs.ListSelectionDialog;
32 import org.eclipse.ui.internal.dialogs.ListContentProvider;
34 public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEditorAction {
36 class WikiFile implements Comparable {
41 public WikiFile(IFile f, String title) {
49 * @see java.lang.Object#toString()
51 public String toString() {
52 return wikiTitle + " - " + file.getProjectRelativePath().toString();
58 * @see java.lang.Comparable#compareTo(java.lang.Object)
60 public int compareTo(Object o) {
61 return wikiTitle.compareTo(((WikiFile) o).wikiTitle);
65 protected Configuration getConfigurationPrefix(String prefix) {
66 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
67 ArrayList configsList = new ArrayList();
68 for (int i = 0; i < allConfigsList.size(); i++) {
69 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
70 if (temp.getType().startsWith(prefix)) {
71 configsList.add(temp);
74 if (configsList.size() == 1) {
75 return (Configuration) configsList.get(0);
77 Collections.sort(configsList);
78 Configuration configuration = null;
79 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
80 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
81 "Select the refresh URL.");
82 listSelectionDialog.setTitle("Multiple active configuration found");
83 if (listSelectionDialog.open() == Window.OK) {
84 Object[] locations = listSelectionDialog.getResult();
85 if (locations != null) {
86 for (int i = 0; i < locations.length; i++) {
87 configuration = (Configuration) locations[i];
95 protected Configuration getConfiguration() {
96 return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
99 public void openWikiLinkOnSelection() {
100 ProblemConsole console = new ProblemConsole();
101 IDocument doc = getDocument();
102 Shell shell = Util.findShell();
106 CategoryDialog dialog = new CategoryDialog(shell);//$NON-NLS-1$
108 String category = dialog.getCategory();
109 if (category == null || category.length() == 0) {
112 category = category.replaceAll(" ", "_");
113 Configuration configuration = getConfiguration();
114 String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
115 String htmlContent = null;
117 IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
119 MediaWikiConnector connector = new MediaWikiConnector();
120 String actionUrl = wikipediaProperties.getActionUrl() + "/Category:" + category;
121 htmlContent = connector.loadHTMLPage(actionUrl, wikipediaProperties.getCharSet());
122 } catch (UnexpectedAnswerException e) {
123 console.println("UnexpectedAnswerException: " + e.getMessage());
124 } catch (MethodException e) {
125 console.println("HTTP-MethodException: " + e.getMessage());
126 } catch (PageNotEditableException e) {
127 console.println("PageNotEditableException: " + e.getMessage());
128 } catch (NoSuchMethodException e) {
129 console.println("NoSuchMethodException: " + e.getMessage());
130 } catch (IllegalAccessException e) {
131 console.println("IllegalAccessException: " + e.getMessage());
132 } catch (ClassNotFoundException e) {
133 console.println("ClassNotFoundException: " + e.getMessage());
134 } catch (InvocationTargetException e) {
135 console.println("InvocationTargetException: " + e.getMessage());
137 if (htmlContent == null) {
140 ParseCategory pc = new ParseCategory();
141 pc.parseCategory(htmlContent);
143 ArrayList startPositionList = pc.getTitleList();
145 HashSet wikiNames = new HashSet();
146 ArrayList filesList = new ArrayList();
147 ArrayList wikiList = new ArrayList();
150 IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
152 for (int i = 0; i < startPositionList.size(); i++) {
153 wikiTitle = (String) startPositionList.get(i);
155 if (wikiTitle != null && !wikiTitle.equals("")) {
156 if (!wikiNames.contains(wikiTitle)) {
157 IFile file = getWikiFile(currentFile, wikiTitle);
158 if (!file.exists()) {
159 filesList.add(new WikiFile(file, wikiTitle));
161 wikiNames.add(wikiTitle);
166 if (filesList.size() > 0) {
167 Collections.sort(filesList);
168 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
169 .getActiveWorkbenchWindow().getShell(), filesList, new ListContentProvider(), new LabelProvider(),
170 "Select the links for file creation:");
171 listSelectionDialog.setTitle("Links found in this article");
172 if (listSelectionDialog.open() == Window.OK) {
173 Object[] locations = listSelectionDialog.getResult();
174 if (locations.length > 0) {
175 IFile[] files = new IFile[locations.length];
176 String[] wikiTitles = new String[locations.length];
177 for (int i = 0; i < files.length; i++) {
178 files[i] = ((WikiFile) locations[i]).file;
179 wikiTitles[i] = ((WikiFile) locations[i]).wikiTitle;
182 Job job = new CreateFilesJob(files, wikiTitles);
183 // job.setRule(createRule(files));