3d3ad4ffefa09bbc081c6973263db44570b91fd1
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / post / StoreWikipediaAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki.post;
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.util.ArrayList;
11 import java.util.Collections;
12 import java.util.Date;
13 import java.util.List;
14
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;
26
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;
44
45 public class StoreWikipediaAction implements IEditorActionDelegate {
46
47   private AbstractTextEditor fEditor;
48
49   private EditorText text;
50
51   private IWorkbenchWindow window;
52
53   public void dispose() {
54   }
55
56   public String generateUrl(Configuration config, String template, String wikiname) {
57
58     /* first, we init the runtime engine. Defaults are fine. */
59
60     try {
61       Velocity.init();
62
63       /* lets make a Context and put data into it */
64
65       VelocityContext context = new VelocityContext();
66
67       context.put("config", config);
68       text.clear();
69       text.setWikiname(wikiname);
70       context.put("text", text);
71
72       /* lets make our own string to render */
73       StringWriter w = new StringWriter();
74       w = new StringWriter();
75       Velocity.evaluate(context, w, "mystring", template);
76       return w.toString();
77
78     } catch (Exception e) {
79       // TODO Auto-generated catch block
80       e.printStackTrace();
81     }
82     return template;
83   }
84
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);
92       }
93     }
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];
105           break;
106         }
107       }
108     }
109     return configuration;
110   }
111
112   public IDocument getDocument() {
113     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
114     return doc;
115   }
116
117   private String getWikiFile(IFile file) {
118     return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
119   }
120
121   public void init(IWorkbenchWindow window) {
122     this.window = window;
123   }
124
125   void openWikiFile(IFile cfile) {
126     String wikiName = getWikiFile(cfile);
127     try {
128       if (fEditor != null) {
129         selectWiki(wikiName);
130       }
131     } catch (Exception e) {
132     }
133
134   }
135
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());
142   }
143
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;
149       }
150     }
151     if (fEditor != null) {
152       openWikiLinkOnSelection();
153     }
154   }
155
156   public void selectionChanged(IAction action, ISelection selection) {
157     if (selection.isEmpty()) {
158       return;
159     }
160     if (selection instanceof TextSelection) {
161       action.setEnabled(true);
162       return;
163     }
164     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
165       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
166     }
167   }
168
169   private void selectWiki(String wikiName) {
170     Configuration configuration = getConfiguration();
171     if (configuration != null && !configuration.equals("")) {
172       try {
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();
177         Date d = new Date();
178
179         Content content = new Content(String.valueOf(d.getTime()), doc.get());
180         boolean success = connector.login(w, configuration.getURL(), configuration.getUser(), configuration.getPassword(), false);
181         if (success) {
182           connector.store(w, configuration.getURL(), wikiName, content, "", false, false);
183           connector.logout(w, configuration.getURL());
184         }
185       } catch (Exception e) {
186         e.printStackTrace();
187         WikiEditorPlugin.getDefault()
188             .reportError("Exception occured", e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
189       }
190     }
191   }
192
193   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
194     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
195       fEditor = (AbstractTextEditor) targetEditor;
196       text = new EditorText(targetEditor);
197     }
198   }
199 }