1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
3 //http://www.djini.de/software/wikipedia/index.html
5 //The modified sources are available under the "Common Public License"
6 //with permission from the original author: Daniel Wunsch
8 import java.io.StringWriter;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
13 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
14 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
15 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
16 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
17 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
18 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
19 import net.sourceforge.phpeclipse.wiki.preferences.Util;
20 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
22 import org.apache.velocity.VelocityContext;
23 import org.apache.velocity.app.Velocity;
24 import org.eclipse.core.resources.IFile;
25 import org.eclipse.jface.action.IAction;
26 import org.eclipse.jface.text.IDocument;
27 import org.eclipse.jface.text.ITextSelection;
28 import org.eclipse.jface.text.TextSelection;
29 import org.eclipse.jface.viewers.ISelection;
30 import org.eclipse.jface.viewers.LabelProvider;
31 import org.eclipse.jface.window.Window;
32 import org.eclipse.ui.IEditorActionDelegate;
33 import org.eclipse.ui.IEditorPart;
34 import org.eclipse.ui.IFileEditorInput;
35 import org.eclipse.ui.IWorkbenchWindow;
36 import org.eclipse.ui.dialogs.ListSelectionDialog;
37 import org.eclipse.ui.internal.dialogs.ListContentProvider;
38 import org.eclipse.ui.texteditor.AbstractTextEditor;
40 public class DownloadWikipediaAction implements IEditorActionDelegate {
42 private AbstractTextEditor fEditor;
44 private EditorText text;
46 private IWorkbenchWindow window;
50 public void dispose() {
53 public String generateUrl(Configuration config, String template, String wikiname) {
55 /* first, we init the runtime engine. Defaults are fine. */
60 /* lets make a Context and put data into it */
62 VelocityContext context = new VelocityContext();
64 context.put("config", config);
66 text.setWikiname(wikiname);
67 context.put("text", text);
69 /* lets make our own string to render */
70 StringWriter w = new StringWriter();
71 w = new StringWriter();
72 Velocity.evaluate(context, w, "mystring", template);
75 } catch (Exception e) {
76 // TODO Auto-generated catch block
82 protected Configuration getConfiguration(){
83 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
84 ArrayList configsList = new ArrayList();
85 for (int i = 0; i < allConfigsList.size(); i++) {
86 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
87 if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
88 configsList.add(temp);
91 Collections.sort(configsList);
92 Configuration configuration = null;
93 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
94 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
95 "Select the refresh URL.");
96 listSelectionDialog.setTitle("Multiple active configuration found");
97 if (listSelectionDialog.open() == Window.OK) {
98 Object[] locations = listSelectionDialog.getResult();
99 if (locations != null) {
100 for (int i = 0; i < locations.length; i++) {
101 configuration = (Configuration) locations[i];
106 return configuration;
109 public IDocument getDocument() {
110 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
114 private String getWikiFile(IFile file) {
115 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
118 public void init(IWorkbenchWindow window) {
119 this.window = window;
122 void openWikiFile(IFile cfile) {
123 String wikiName = getWikiFile(cfile);
125 if (fEditor != null) {
126 selectWiki(wikiName);
128 } catch (Exception e) {
133 public void openWikiLinkOnSelection() {
134 IDocument doc = getDocument();
135 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
136 int pos = selection.getOffset();
137 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
138 openWikiFile(ei.getFile());
141 public void run(IAction action) {
142 if (fEditor == null) {
143 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
144 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
145 fEditor = (AbstractTextEditor) targetEditor;
148 if (fEditor != null) {
149 openWikiLinkOnSelection();
153 public void selectionChanged(IAction action, ISelection selection) {
154 if (selection.isEmpty()) {
157 if (selection instanceof TextSelection) {
158 action.setEnabled(true);
161 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
162 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
166 private void selectWiki(String wikiName) {
167 Configuration configuration = getConfiguration();
168 if (configuration != null && !configuration.equals("")) {
169 String url = generateUrl(configuration, configuration.getURL(), wikiName);
170 String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
171 if (wikiContent != null) {
172 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
173 doc.set(wikiContent);
178 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
179 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
180 fEditor = (AbstractTextEditor) targetEditor;
181 text = new EditorText(targetEditor);