1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
4 //http://www.djini.de/software/wikipedia/index.html
6 //The modified sources are available under the "Common Public License"
7 //with permission from the original author: Daniel Wunsch
9 import java.io.StringWriter;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.List;
15 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
17 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
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 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.app.Velocity;
27 import org.eclipse.core.resources.IFile;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextSelection;
31 import org.eclipse.jface.text.TextSelection;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.ui.IEditorActionDelegate;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IFileEditorInput;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.dialogs.ListSelectionDialog;
40 import org.eclipse.ui.internal.dialogs.ListContentProvider;
41 import org.eclipse.ui.texteditor.AbstractTextEditor;
43 public class DownloadWikipediaAction implements IEditorActionDelegate {
45 private AbstractTextEditor fEditor;
47 private EditorText text;
49 private IWorkbenchWindow window;
51 public void dispose() {
54 public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
56 /* first, we init the runtime engine. Defaults are fine. */
61 if (template == null || template.equals("")) {
62 // fall back to default settings
64 // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
65 template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
68 /* lets make a Context and put data into it */
70 VelocityContext context = new VelocityContext();
72 context.put("config", config);
74 text.setWikiname(wikiname);
75 context.put("text", text);
77 /* lets make our own string to render */
78 StringWriter w = new StringWriter();
79 w = new StringWriter();
80 Velocity.evaluate(context, w, "mystring", template);
83 } catch (Exception e) {
84 // TODO Auto-generated catch block
90 protected Configuration getConfiguration() {
91 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
92 ArrayList configsList = new ArrayList();
93 for (int i = 0; i < allConfigsList.size(); i++) {
94 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
95 if (temp.getType().startsWith(WikiEditorPlugin.PREFIX_LOAD)) {
96 configsList.add(temp);
99 Collections.sort(configsList);
100 Configuration configuration = null;
101 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
102 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
103 "Select the refresh URL.");
104 listSelectionDialog.setTitle("Multiple active configuration found");
105 if (listSelectionDialog.open() == Window.OK) {
106 Object[] locations = listSelectionDialog.getResult();
107 if (locations != null) {
108 for (int i = 0; i < locations.length; i++) {
109 configuration = (Configuration) locations[i];
114 return configuration;
117 public IDocument getDocument() {
118 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
122 private String getWikiFile(IFile file) {
123 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
126 public void init(IWorkbenchWindow window) {
127 this.window = window;
130 void openWikiFile(IFile cfile) {
131 String wikiName = getWikiFile(cfile);
133 if (fEditor != null) {
134 selectWiki(wikiName);
136 } catch (Exception e) {
141 public void openWikiLinkOnSelection() {
142 IDocument doc = getDocument();
143 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
144 int pos = selection.getOffset();
145 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
146 openWikiFile(ei.getFile());
149 public void run(IAction action) {
150 if (fEditor == null) {
151 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
152 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
153 fEditor = (AbstractTextEditor) targetEditor;
156 if (fEditor != null) {
157 openWikiLinkOnSelection();
161 public void selectionChanged(IAction action, ISelection selection) {
162 if (selection.isEmpty()) {
165 if (selection instanceof TextSelection) {
166 action.setEnabled(true);
169 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
170 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
174 private void selectWiki(String wikiName) {
175 Configuration configuration = getConfiguration();
176 if (configuration != null && !configuration.equals("")) {
178 String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
179 IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
181 String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
182 String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
183 if (wikiContent != null) {
184 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
185 doc.set(wikiContent);
187 } catch (Exception e) {
189 WikiEditorPlugin.getDefault().reportError("Exception occured: ",
190 e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
195 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
196 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
197 fEditor = (AbstractTextEditor) targetEditor;
198 text = new EditorText(targetEditor);