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; 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); } } 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) { return; } CategoryDialog dialog = new CategoryDialog(shell);//$NON-NLS-1$ dialog.open(); String category = dialog.getCategory(); 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(htmlContent); 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(); } } } } }