1) Added missing strings for italic, underline and strike through.
[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.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.List;
8
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;
23
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;
33
34 public final class CreateFilesFromCategoryEditorAction extends OpenWikiLinkEditorAction {
35
36   class WikiFile implements Comparable {
37     IFile file;
38
39     String wikiTitle;
40
41     public WikiFile(IFile f, String title) {
42       file = f;
43       wikiTitle = title;
44     }
45
46     /*
47      * (non-Javadoc)
48      * 
49      * @see java.lang.Object#toString()
50      */
51     public String toString() {
52       return wikiTitle + " - " + file.getProjectRelativePath().toString();
53     }
54
55     /*
56      * (non-Javadoc)
57      * 
58      * @see java.lang.Comparable#compareTo(java.lang.Object)
59      */
60     public int compareTo(Object o) {
61       return wikiTitle.compareTo(((WikiFile) o).wikiTitle);
62     }
63   }
64
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);
72       }
73     }
74     if (configsList.size() == 1) {
75       return (Configuration) configsList.get(0);
76     }
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];
88           break;
89         }
90       }
91     }
92     return configuration;
93   }
94
95   protected Configuration getConfiguration() {
96     return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
97   }
98
99   public void openWikiLinkOnSelection() {
100     ProblemConsole console = new ProblemConsole();
101     IDocument doc = getDocument();
102     Shell shell = Util.findShell();
103     if (shell == null) {
104       return;
105     }
106     CategoryDialog dialog = new CategoryDialog(shell);//$NON-NLS-1$
107     dialog.open();
108     String category = dialog.getCategory();
109     if (category == null || category.length() == 0) {
110       return;
111     }
112     category = category.replaceAll(" ", "_");
113     Configuration configuration = getConfiguration();
114     String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
115     String htmlContent = null;
116     try {
117       IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
118
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());
136     }
137     if (htmlContent == null) {
138       return;
139     }
140     ParseCategory pc = new ParseCategory();
141     pc.parseCategory(htmlContent);
142
143     ArrayList startPositionList = pc.getTitleList();
144
145     HashSet wikiNames = new HashSet();
146     ArrayList filesList = new ArrayList();
147     ArrayList wikiList = new ArrayList();
148     String wikiTitle;
149     Integer posInteger;
150     IFile currentFile = ((IFileEditorInput) editor.getEditorInput()).getFile();
151
152     for (int i = 0; i < startPositionList.size(); i++) {
153       wikiTitle = (String) startPositionList.get(i);
154
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));
160           }
161           wikiNames.add(wikiTitle);
162         }
163       }
164     }
165
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;
180           }
181
182           Job job = new CreateFilesJob(files, wikiTitles);
183           //        job.setRule(createRule(files));
184           job.setRule(null);
185           job.setUser(true);
186           job.schedule();
187         }
188       }
189
190     }
191   }
192
193 }