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.core.runtime.jobs.Job;
29 import org.eclipse.jface.action.IAction;
30 import org.eclipse.jface.text.IDocument;
31 import org.eclipse.jface.text.ITextSelection;
32 import org.eclipse.jface.text.TextSelection;
33 import org.eclipse.jface.viewers.ISelection;
34 import org.eclipse.jface.viewers.LabelProvider;
35 import org.eclipse.jface.window.Window;
36 import org.eclipse.ui.IEditorActionDelegate;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IFileEditorInput;
39 import org.eclipse.ui.IWorkbenchWindow;
40 import org.eclipse.ui.dialogs.ListSelectionDialog;
41 import org.eclipse.ui.internal.dialogs.ListContentProvider;
42 import org.eclipse.ui.texteditor.AbstractTextEditor;
44 public class DownloadWikipediaAction implements IEditorActionDelegate {
46 private AbstractTextEditor fEditor;
48 private EditorText text;
50 private IWorkbenchWindow window;
52 public void dispose() {
55 public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
57 /* first, we init the runtime engine. Defaults are fine. */
62 if (template == null || template.equals("")) {
63 // fall back to default settings
65 // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
66 template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
69 /* lets make a Context and put data into it */
71 VelocityContext context = new VelocityContext();
73 context.put("config", config);
75 text.setWikiname(wikiname);
76 context.put("text", text);
78 /* lets make our own string to render */
79 StringWriter w = new StringWriter();
80 w = new StringWriter();
81 Velocity.evaluate(context, w, "mystring", template);
84 } catch (Exception e) {
85 // TODO Auto-generated catch block
91 protected Configuration getConfigurationPrefix(String prefix) {
92 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
93 ArrayList configsList = new ArrayList();
94 for (int i = 0; i < allConfigsList.size(); i++) {
95 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
96 if (temp.getType().startsWith(prefix)) {
97 configsList.add(temp);
100 Collections.sort(configsList);
101 Configuration configuration = null;
102 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
103 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
104 "Select the refresh URL.");
105 listSelectionDialog.setTitle("Multiple active configuration found");
106 if (listSelectionDialog.open() == Window.OK) {
107 Object[] locations = listSelectionDialog.getResult();
108 if (locations != null) {
109 for (int i = 0; i < locations.length; i++) {
110 configuration = (Configuration) locations[i];
115 return configuration;
117 protected Configuration getConfiguration( ) {
118 return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
121 public IDocument getDocument() {
122 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
126 private static String getWikiFile(IFile file) {
127 return Util.getFileWikiName(file);
130 public void init(IWorkbenchWindow window) {
131 this.window = window;
134 void openWikiFile(IFile cfile) {
135 String wikiName = getWikiFile(cfile);
137 if (fEditor != null) {
138 selectWiki(cfile, wikiName);
140 } catch (Exception e) {
145 public void openWikiLinkOnSelection() {
146 IDocument doc = getDocument();
147 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
148 int pos = selection.getOffset();
149 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
150 openWikiFile(ei.getFile());
153 public void run(IAction action) {
154 if (fEditor == null) {
155 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
156 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
157 fEditor = (AbstractTextEditor) targetEditor;
160 if (fEditor != null) {
161 openWikiLinkOnSelection();
165 public void selectionChanged(IAction action, ISelection selection) {
166 if (selection.isEmpty()) {
169 if (selection instanceof TextSelection) {
170 action.setEnabled(true);
173 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
174 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
178 private void selectWiki(IFile cfile, String wikiName) {
179 Configuration configuration = getConfiguration();
180 if (configuration != null && !configuration.equals("")) {
182 String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
183 IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
185 // String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
186 // MediaWikiConnector mc = new MediaWikiConnector();
187 // String wikiContent = mc.getWikiRawText(wikiName, url);
188 // if (wikiContent != null) {
189 // IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
190 // doc.set(wikiContent);
192 IFile[] files = new IFile[1];
195 Job job = new RefreshJob(wikipediaProperties, files, configuration.getURL());
198 job.setPriority(Job.SHORT);
200 } catch (Exception e) {
202 WikiEditorPlugin.getDefault().reportError("Exception occured: ",
203 e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
208 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
209 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
210 fEditor = (AbstractTextEditor) targetEditor;
211 text = new EditorText(targetEditor);