051cce8134752374459b5aad3eca22ccb01b4e84
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / DownloadWikipediaAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2
3 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
4 //http://www.djini.de/software/wikipedia/index.html
5 //
6 //The modified sources are available under the "Common Public License"
7 //with permission from the original author: Daniel Wunsch
8
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;
14
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;
24
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;
42
43 public class DownloadWikipediaAction implements IEditorActionDelegate {
44
45   private AbstractTextEditor fEditor;
46
47   private EditorText text;
48
49   private IWorkbenchWindow window;
50
51   public void dispose() {
52   }
53
54   public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
55
56     /* first, we init the runtime engine. Defaults are fine. */
57
58     try {
59       Velocity.init();
60
61       if (template == null || template.equals("")) {
62         // fall back to default settings
63         // Example:
64         // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
65         template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
66       }
67
68       /* lets make a Context and put data into it */
69
70       VelocityContext context = new VelocityContext();
71
72       context.put("config", config);
73       text.clear();
74       text.setWikiname(wikiname);
75       context.put("text", text);
76
77       /* lets make our own string to render */
78       StringWriter w = new StringWriter();
79       w = new StringWriter();
80       Velocity.evaluate(context, w, "mystring", template);
81       return w.toString();
82
83     } catch (Exception e) {
84       // TODO Auto-generated catch block
85       e.printStackTrace();
86     }
87     return template;
88   }
89
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);
97       }
98     }
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];
110           break;
111         }
112       }
113     }
114     return configuration;
115   }
116
117   public IDocument getDocument() {
118     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
119     return doc;
120   }
121
122   private String getWikiFile(IFile file) {
123     return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
124   }
125
126   public void init(IWorkbenchWindow window) {
127     this.window = window;
128   }
129
130   void openWikiFile(IFile cfile) {
131     String wikiName = getWikiFile(cfile);
132     try {
133       if (fEditor != null) {
134         selectWiki(wikiName);
135       }
136     } catch (Exception e) {
137     }
138
139   }
140
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());
147   }
148
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;
154       }
155     }
156     if (fEditor != null) {
157       openWikiLinkOnSelection();
158     }
159   }
160
161   public void selectionChanged(IAction action, ISelection selection) {
162     if (selection.isEmpty()) {
163       return;
164     }
165     if (selection instanceof TextSelection) {
166       action.setEnabled(true);
167       return;
168     }
169     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
170       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
171     }
172   }
173
174   private void selectWiki(String wikiName) {
175     Configuration configuration = getConfiguration();
176     if (configuration != null && !configuration.equals("")) {
177       try {
178         String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
179         IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
180
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);
186         }
187       } catch (Exception e) {
188         e.printStackTrace();
189         WikiEditorPlugin.getDefault().reportError("Exception occured: ",
190             e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
191       }
192     }
193   }
194
195   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
196     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
197       fEditor = (AbstractTextEditor) targetEditor;
198       text = new EditorText(targetEditor);
199     }
200   }
201 }