1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
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.Method;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
17 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE;
18 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
19 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
20 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
21 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
22 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
23 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
24 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
25 import net.sourceforge.phpeclipse.wiki.preferences.Util;
26 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.Velocity;
30 import org.eclipse.core.resources.IFile;
31 import org.eclipse.jface.action.IAction;
32 import org.eclipse.jface.text.IDocument;
33 import org.eclipse.jface.text.ITextSelection;
34 import org.eclipse.jface.text.TextSelection;
35 import org.eclipse.jface.viewers.ISelection;
36 import org.eclipse.jface.viewers.LabelProvider;
37 import org.eclipse.jface.window.Window;
38 import org.eclipse.ui.IEditorActionDelegate;
39 import org.eclipse.ui.IEditorPart;
40 import org.eclipse.ui.IFileEditorInput;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.dialogs.ListSelectionDialog;
43 import org.eclipse.ui.internal.dialogs.ListContentProvider;
44 import org.eclipse.ui.texteditor.AbstractTextEditor;
46 public class StoreWikipediaAction implements IEditorActionDelegate {
48 private AbstractTextEditor fEditor;
50 private EditorText text;
52 private IWorkbenchWindow window;
54 public void dispose() {
57 public String generateUrl(Configuration config, String template, String wikiname) {
59 /* first, we init the runtime engine. Defaults are fine. */
64 /* lets make a Context and put data into it */
66 VelocityContext context = new VelocityContext();
68 context.put("config", config);
70 text.setWikiname(wikiname);
71 context.put("text", text);
73 /* lets make our own string to render */
74 StringWriter w = new StringWriter();
75 w = new StringWriter();
76 Velocity.evaluate(context, w, "mystring", template);
79 } catch (Exception e) {
80 // TODO Auto-generated catch block
86 protected Configuration getConfiguration() {
87 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
88 ArrayList configsList = new ArrayList();
89 for (int i = 0; i < allConfigsList.size(); i++) {
90 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
91 if (temp.getType().startsWith(WikiEditorPlugin.PREFIX_STORE)) {
92 configsList.add(temp);
95 Collections.sort(configsList);
96 Configuration configuration = null;
97 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
98 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
99 "Select the refresh URL.");
100 listSelectionDialog.setTitle("Multiple active configuration found");
101 if (listSelectionDialog.open() == Window.OK) {
102 Object[] locations = listSelectionDialog.getResult();
103 if (locations != null) {
104 for (int i = 0; i < locations.length; i++) {
105 configuration = (Configuration) locations[i];
110 return configuration;
113 public IDocument getDocument() {
114 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
118 private String getWikiFile(IFile file) {
119 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
122 public void init(IWorkbenchWindow window) {
123 this.window = window;
126 void openWikiFile(IFile cfile) {
127 String wikiName = getWikiFile(cfile);
129 if (fEditor != null) {
130 selectWiki(wikiName);
132 } catch (Exception e) {
137 public void openWikiLinkOnSelection() {
138 IDocument doc = getDocument();
139 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
140 int pos = selection.getOffset();
141 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
142 openWikiFile(ei.getFile());
145 public void run(IAction action) {
146 if (fEditor == null) {
147 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
148 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
149 fEditor = (AbstractTextEditor) targetEditor;
152 if (fEditor != null) {
153 openWikiLinkOnSelection();
157 public void selectionChanged(IAction action, ISelection selection) {
158 if (selection.isEmpty()) {
161 if (selection instanceof TextSelection) {
162 action.setEnabled(true);
165 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
166 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
170 private void selectWiki(String wikiName) {
171 Configuration configuration = getConfiguration();
172 if (configuration != null && !configuration.equals("")) {
174 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
175 String url = generateUrl(configuration, configuration.getURL(), wikiName);
176 String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_STORE.length());
177 IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
179 MediaWikiConnector connector = new MediaWikiConnector();
180 String actionUrl = configuration.getURL();
181 if (actionUrl == null || actionUrl.equals("")) {
182 // fall back to default settings
183 actionUrl = wikipediaProperties.getActionUrl();
187 Content content = new Content(String.valueOf(d.getTime()), doc.get());
188 boolean success = connector.login(wikipediaProperties, actionUrl, configuration.getUser(), configuration.getPassword(), false);
190 connector.store(wikipediaProperties, actionUrl, wikiName, content, "", false, false);
191 connector.logout(wikipediaProperties, actionUrl);
193 } catch (Exception e) {
195 WikiEditorPlugin.getDefault()
196 .reportError("Exception occured: ", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
201 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
202 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
203 fEditor = (AbstractTextEditor) targetEditor;
204 text = new EditorText(targetEditor);