1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.ByteArrayInputStream;
4 import java.io.StringWriter;
5 import java.util.ArrayList;
6 import java.util.Collections;
9 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
10 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
11 import net.sourceforge.phpeclipse.wiki.internal.Configuration;
12 import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager;
13 import net.sourceforge.phpeclipse.wiki.internal.IConfiguration;
14 import net.sourceforge.phpeclipse.wiki.preferences.Util;
15 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
17 import org.apache.velocity.VelocityContext;
18 import org.apache.velocity.app.Velocity;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceStatus;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.SubProgressMonitor;
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.internal.ide.IDEWorkbenchPlugin;
42 import org.eclipse.ui.texteditor.AbstractTextEditor;
44 public class DownloadWikipediaAction implements IEditorActionDelegate {
46 private AbstractTextEditor fEditor;
48 private EditorText text;
50 private IWorkbenchWindow window;
52 private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
54 // Create the folder resource in the workspace
55 // Recursive to create any folders which do not exist already
56 if (!folderHandle.exists()) {
57 IContainer parent = folderHandle.getParent();
58 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
59 createFolder((IFolder) parent, monitor);
61 folderHandle.create(false, true, monitor);
63 } catch (CoreException e) {
64 // If the folder already existed locally, just refresh to get contents
65 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
66 folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
73 * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
74 * this is the responsibility of <code>createFolder</code>.
77 * the path of the folder resource to create a handle for
78 * @return the new folder resource handle
81 private IFolder createFolderHandle(IPath folderPath) {
82 return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
85 private void createNewFileIfNeeded(IFile file, String word) throws CoreException {
87 createWikiFile(file, word);
91 private void createWikiFile(IFile file, String word) throws CoreException {
92 IContainer parent = file.getParent();
93 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
94 createFolder((IFolder) parent, null);
96 String newText = "<!--" + word + "-->";
97 byte[] buffer = newText.getBytes();
98 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
99 file.create(source, true, null);
102 public void dispose() {
105 public String generateUrl(Configuration config, String template, String wikiname) {
107 /* first, we init the runtime engine. Defaults are fine. */
112 /* lets make a Context and put data into it */
114 VelocityContext context = new VelocityContext();
116 context.put("config", config);
118 text.setWikiname(wikiname);
119 context.put("text", text);
121 /* lets make our own string to render */
122 StringWriter w = new StringWriter();
123 w = new StringWriter();
124 Velocity.evaluate(context, w, "mystring", template);
127 } catch (Exception e) {
128 // TODO Auto-generated catch block
134 protected Configuration getConfiguration(){
135 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
136 ArrayList configsList = new ArrayList();
137 for (int i = 0; i < allConfigsList.size(); i++) {
138 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
139 if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
140 configsList.add(temp);
143 Collections.sort(configsList);
144 Configuration configuration = null;
145 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
146 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
147 "Select the refresh URL.");
148 listSelectionDialog.setTitle("Multiple active configuration found");
149 if (listSelectionDialog.open() == Window.OK) {
150 Object[] locations = listSelectionDialog.getResult();
151 if (locations != null) {
152 for (int i = 0; i < locations.length; i++) {
153 configuration = (Configuration) locations[i];
158 return configuration;
161 public IDocument getDocument() {
162 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
166 private String getWikiFile(IFile file) {
167 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
170 public void init(IWorkbenchWindow window) {
171 this.window = window;
174 void openWikiFile(IFile cfile) {
175 String wikiName = getWikiFile(cfile);
177 if (fEditor != null) {
178 selectWiki(wikiName);
180 } catch (Exception e) {
185 public void openWikiLinkOnSelection() {
186 IDocument doc = getDocument();
187 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
188 int pos = selection.getOffset();
189 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
190 openWikiFile(ei.getFile());
193 public void run(IAction action) {
194 if (fEditor == null) {
195 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
196 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
197 fEditor = (AbstractTextEditor) targetEditor;
200 if (fEditor != null) {
201 openWikiLinkOnSelection();
205 public void selectionChanged(IAction action, ISelection selection) {
206 if (selection.isEmpty()) {
209 if (selection instanceof TextSelection) {
210 action.setEnabled(true);
213 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
214 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
218 private void selectWiki(String wikiName) {
219 Configuration configuration = getConfiguration();
220 if (configuration != null && !configuration.equals("")) {
221 String url = generateUrl(configuration, configuration.getURL(), wikiName);
222 String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
223 if (wikiContent != null) {
224 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
225 doc.set(wikiContent);
230 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
231 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
232 fEditor = (AbstractTextEditor) targetEditor;
233 text = new EditorText(targetEditor);