Implemeted action for uploading Wikipedia articles (thanks to D.Wunsch)
[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 //Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
3 //http://www.djini.de/software/wikipedia/index.html
4 //
5 //The modified sources are available under the "Common Public License"
6 //with permission from the original author: Daniel Wunsch
7
8 import java.io.StringWriter;
9 import java.util.ArrayList;
10 import java.util.Collections;
11 import java.util.List;
12
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;
21
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;
39
40 public class DownloadWikipediaAction implements IEditorActionDelegate {
41
42   private AbstractTextEditor fEditor;
43
44   private EditorText text;
45
46   private IWorkbenchWindow window;
47
48   
49
50   public void dispose() {
51   }
52
53   public String generateUrl(Configuration config, String template, String wikiname) {
54
55     /* first, we init the runtime engine. Defaults are fine. */
56
57     try {
58       Velocity.init();
59
60       /* lets make a Context and put data into it */
61
62       VelocityContext context = new VelocityContext();
63
64       context.put("config", config);
65       text.clear();
66       text.setWikiname(wikiname);
67       context.put("text", text);
68
69       /* lets make our own string to render */
70       StringWriter w = new StringWriter();
71       w = new StringWriter();
72       Velocity.evaluate(context, w, "mystring", template);
73       return w.toString();
74
75     } catch (Exception e) {
76       // TODO Auto-generated catch block
77       e.printStackTrace();
78     }
79     return template;
80   }
81
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);
89       }
90     }
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];
102           break;
103         }
104       }
105     }
106     return configuration;
107   }
108
109   public IDocument getDocument() {
110     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
111     return doc;
112   }
113
114   private String getWikiFile(IFile file) {
115     return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
116   }
117
118   public void init(IWorkbenchWindow window) {
119     this.window = window;
120   }
121
122   void openWikiFile(IFile cfile) {
123     String wikiName = getWikiFile(cfile);
124     try {
125       if (fEditor != null) {
126         selectWiki(wikiName);
127       }
128     } catch (Exception e) {
129     }
130
131   }
132
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());
139   }
140
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;
146       }
147     }
148     if (fEditor != null) {
149       openWikiLinkOnSelection();
150     }
151   }
152
153   public void selectionChanged(IAction action, ISelection selection) {
154     if (selection.isEmpty()) {
155       return;
156     }
157     if (selection instanceof TextSelection) {
158       action.setEnabled(true);
159       return;
160     }
161     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
162       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
163     }
164   }
165   
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);
174       }
175     }
176   }
177
178   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
179     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
180       fEditor = (AbstractTextEditor) targetEditor;
181       text = new EditorText(targetEditor);
182     }
183   }
184 }