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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
15 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
16 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.WikipediaDE;
17 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.Content;
18 import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
19 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
20 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
21 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
22 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
23 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
24 import net.sourceforge.phpeclipse.wiki.preferences.Util;
25 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.eclipse.core.resources.IFile;
30 import org.eclipse.jface.action.IAction;
31 import org.eclipse.jface.text.IDocument;
32 import org.eclipse.jface.text.ITextSelection;
33 import org.eclipse.jface.text.TextSelection;
34 import org.eclipse.jface.viewers.ISelection;
35 import org.eclipse.jface.viewers.LabelProvider;
36 import org.eclipse.jface.window.Window;
37 import org.eclipse.ui.IEditorActionDelegate;
38 import org.eclipse.ui.IEditorPart;
39 import org.eclipse.ui.IFileEditorInput;
40 import org.eclipse.ui.IWorkbenchWindow;
41 import org.eclipse.ui.dialogs.ListSelectionDialog;
42 import org.eclipse.ui.internal.dialogs.ListContentProvider;
43 import org.eclipse.ui.texteditor.AbstractTextEditor;
45 public class StoreWikipediaAction implements IEditorActionDelegate {
47 private AbstractTextEditor fEditor;
49 private EditorText text;
51 private IWorkbenchWindow window;
53 public void dispose() {
56 public String generateUrl(Configuration config, String template, String wikiname) {
58 /* first, we init the runtime engine. Defaults are fine. */
63 /* lets make a Context and put data into it */
65 VelocityContext context = new VelocityContext();
67 context.put("config", config);
69 text.setWikiname(wikiname);
70 context.put("text", text);
72 /* lets make our own string to render */
73 StringWriter w = new StringWriter();
74 w = new StringWriter();
75 Velocity.evaluate(context, w, "mystring", template);
78 } catch (Exception e) {
79 // TODO Auto-generated catch block
85 protected Configuration getConfiguration() {
86 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
87 ArrayList configsList = new ArrayList();
88 for (int i = 0; i < allConfigsList.size(); i++) {
89 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
90 if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_SET_TEXT)) {
91 configsList.add(temp);
94 Collections.sort(configsList);
95 Configuration configuration = null;
96 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
97 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
98 "Select the refresh URL.");
99 listSelectionDialog.setTitle("Multiple active configuration found");
100 if (listSelectionDialog.open() == Window.OK) {
101 Object[] locations = listSelectionDialog.getResult();
102 if (locations != null) {
103 for (int i = 0; i < locations.length; i++) {
104 configuration = (Configuration) locations[i];
109 return configuration;
112 public IDocument getDocument() {
113 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
117 private String getWikiFile(IFile file) {
118 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
121 public void init(IWorkbenchWindow window) {
122 this.window = window;
125 void openWikiFile(IFile cfile) {
126 String wikiName = getWikiFile(cfile);
128 if (fEditor != null) {
129 selectWiki(wikiName);
131 } catch (Exception e) {
136 public void openWikiLinkOnSelection() {
137 IDocument doc = getDocument();
138 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
139 int pos = selection.getOffset();
140 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
141 openWikiFile(ei.getFile());
144 public void run(IAction action) {
145 if (fEditor == null) {
146 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
147 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
148 fEditor = (AbstractTextEditor) targetEditor;
151 if (fEditor != null) {
152 openWikiLinkOnSelection();
156 public void selectionChanged(IAction action, ISelection selection) {
157 if (selection.isEmpty()) {
160 if (selection instanceof TextSelection) {
161 action.setEnabled(true);
164 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
165 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
169 private void selectWiki(String wikiName) {
170 Configuration configuration = getConfiguration();
171 if (configuration != null && !configuration.equals("")) {
173 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
174 String url = generateUrl(configuration, configuration.getURL(), wikiName);
175 IWikipedia w = WikipediaDE.getInstance();
176 MediaWikiConnector connector = new MediaWikiConnector();
179 Content content = new Content(String.valueOf(d.getTime()), doc.get());
180 boolean success = connector.login(w, configuration.getURL(), configuration.getUser(), configuration.getPassword(), false);
182 connector.store(w, configuration.getURL(), wikiName, content, "", false, false);
183 connector.logout(w, configuration.getURL());
185 } catch (Exception e) {
187 WikiEditorPlugin.getDefault()
188 .reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
193 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
194 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
195 fEditor = (AbstractTextEditor) targetEditor;
196 text = new EditorText(targetEditor);