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.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia; 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.core.runtime.jobs.Job; 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(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) { /* first, we init the runtime engine. Defaults are fine. */ try { Velocity.init(); if (template == null || template.equals("")) { // fall back to default settings // Example: // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw"; } /* 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 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); } } 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 IDocument getDocument() { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); return doc; } private static String getWikiFile(IFile file) { return Util.getURLWikiName(file); } public void init(IWorkbenchWindow window) { this.window = window; } void openWikiFile(IFile cfile) { String wikiName = getWikiFile(cfile); try { if (fEditor != null) { selectWiki(cfile, 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(IFile cfile, String wikiName) { Configuration configuration = getConfiguration(); if (configuration != null && !configuration.equals("")) { try { String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length()); IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale); // String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName); // MediaWikiConnector mc = new MediaWikiConnector(); // String wikiContent = mc.getWikiRawText(wikiName, url); // if (wikiContent != null) { // IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); // doc.set(wikiContent); // } IFile[] files = new IFile[1]; files[0] = cfile; Job job = new RefreshJob(wikipediaProperties, files, configuration.getURL()); job.setRule(null); job.setUser(true); job.setPriority(Job.SHORT); job.schedule(); } catch (Exception e) { e.printStackTrace(); WikiEditorPlugin.getDefault().reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file."); } } } public void setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; text = new EditorText(targetEditor); } } }