package net.sourceforge.phpeclipse.wiki.actions.mediawiki; //Parts of this sources are copied and modified from the jEdit Wikipedia plugin: //http://www.djini.de/software/wikipedia/index.html // //The modified sources are available under the "Common Public License" //with permission from the original author: Daniel Wunsch import java.io.StringWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; 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 net.sourceforge.phpeclipse.wiki.velocity.EditorText; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import org.eclipse.core.resources.IFile; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.dialogs.ListSelectionDialog; import org.eclipse.ui.internal.dialogs.ListContentProvider; import org.eclipse.ui.texteditor.AbstractTextEditor; public class DownloadWikipediaAction implements IEditorActionDelegate { private AbstractTextEditor fEditor; private EditorText text; private IWorkbenchWindow window; public void dispose() { } public String generateUrl(Configuration config, String template, String wikiname) { /* first, we init the runtime engine. Defaults are fine. */ try { Velocity.init(); /* lets make a Context and put data into it */ VelocityContext context = new VelocityContext(); context.put("config", config); text.clear(); text.setWikiname(wikiname); context.put("text", text); /* lets make our own string to render */ StringWriter w = new StringWriter(); w = new StringWriter(); Velocity.evaluate(context, w, "mystring", template); return w.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return template; } protected Configuration getConfiguration(){ 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().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) { configsList.add(temp); } } 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; } public IDocument getDocument() { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); return doc; } private String getWikiFile(IFile file) { return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH); } public void init(IWorkbenchWindow window) { this.window = window; } void openWikiFile(IFile cfile) { String wikiName = getWikiFile(cfile); try { if (fEditor != null) { selectWiki(wikiName); } } catch (Exception e) { } } public void openWikiLinkOnSelection() { IDocument doc = getDocument(); ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); int pos = selection.getOffset(); IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); openWikiFile(ei.getFile()); } public void run(IAction action) { if (fEditor == null) { IEditorPart targetEditor = window.getActivePage().getActiveEditor(); if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; } } if (fEditor != null) { openWikiLinkOnSelection(); } } public void selectionChanged(IAction action, ISelection selection) { if (selection.isEmpty()) { return; } if (selection instanceof TextSelection) { action.setEnabled(true); return; } if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) { action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class)); } } private void selectWiki(String wikiName) { Configuration configuration = getConfiguration(); if (configuration != null && !configuration.equals("")) { String url = generateUrl(configuration, configuration.getURL(), wikiName); String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url); if (wikiContent != null) { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); doc.set(wikiContent); } } } public void setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; text = new EditorText(targetEditor); } } }