package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
-import java.io.ByteArrayInputStream;
+//Parts of this sources are copied and modified from the jEdit Wikipedia plugin:
+//http://www.djini.de/software/wikipedia/index.html
+//
+//The modified sources are available under the "Common Public License"
+//with permission from the original author: Daniel Wunsch
+
import java.io.StringWriter;
+import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.config.IWikipedia;
+import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector;
import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
import net.sourceforge.phpeclipse.wiki.internal.Configuration;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
-import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
-import org.eclipse.core.resources.IFolder;
-import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.IResourceStatus;
-import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
-import org.eclipse.core.runtime.IProgressMonitor;
-import org.eclipse.core.runtime.SubProgressMonitor;
+import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.dialogs.ListSelectionDialog;
import org.eclipse.ui.internal.dialogs.ListContentProvider;
-import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
import org.eclipse.ui.texteditor.AbstractTextEditor;
public class DownloadWikipediaAction implements IEditorActionDelegate {
private IWorkbenchWindow window;
- private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
- try {
- // Create the folder resource in the workspace
- // Recursive to create any folders which do not exist already
- if (!folderHandle.exists()) {
- IContainer parent = folderHandle.getParent();
- if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
- createFolder((IFolder) parent, monitor);
- }
- folderHandle.create(false, true, monitor);
- }
- } catch (CoreException e) {
- // If the folder already existed locally, just refresh to get contents
- if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
- folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
- else
- throw e;
- }
- }
-
- /**
- * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
- * this is the responsibility of <code>createFolder</code>.
- *
- * @param folderPath
- * the path of the folder resource to create a handle for
- * @return the new folder resource handle
- * @see #createFolder
- */
- private IFolder createFolderHandle(IPath folderPath) {
- return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
- }
-
- private void createNewFileIfNeeded(IFile file, String word) throws CoreException {
- if (!file.exists()) {
- createWikiFile(file, word);
- }
- }
-
- private void createWikiFile(IFile file, String word) throws CoreException {
- IContainer parent = file.getParent();
- if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
- createFolder((IFolder) parent, null);
- }
- String newText = "<!--" + word + "-->";
- byte[] buffer = newText.getBytes();
- ByteArrayInputStream source = new ByteArrayInputStream(buffer);
- file.create(source, true, null);
- }
-
public void dispose() {
}
- public String generateUrl(Configuration config, String template, String wikiname) {
+ public String generateUrl(IWikipedia wikipediaProperties, Configuration config, String template, String wikiname) {
/* first, we init the runtime engine. Defaults are fine. */
try {
Velocity.init();
+ if (template == null || template.equals("")) {
+ // fall back to default settings
+ // Example:
+ // http://en.wikipedia.org/w/index.php?title=$text.wikiname&action=raw
+ template = wikipediaProperties.getActionUrl() + "?title=$text.wikiname&action=raw";
+ }
+
/* lets make a Context and put data into it */
VelocityContext context = new VelocityContext();
return template;
}
- protected Configuration getConfiguration(){
+ protected Configuration getConfigurationPrefix(String prefix) {
List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
ArrayList configsList = new ArrayList();
for (int i = 0; i < allConfigsList.size(); i++) {
IConfiguration temp = (IConfiguration) allConfigsList.get(i);
- if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
+ if (temp.getType().startsWith(prefix)) {
configsList.add(temp);
}
}
}
return configuration;
}
+ protected Configuration getConfiguration( ) {
+ return getConfigurationPrefix(WikiEditorPlugin.PREFIX_LOAD);
+ }
public IDocument getDocument() {
IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
return doc;
}
- private String getWikiFile(IFile file) {
- return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
+ private static String getWikiFile(IFile file) {
+ return Util.getFileWikiName(file);
}
public void init(IWorkbenchWindow window) {
String wikiName = getWikiFile(cfile);
try {
if (fEditor != null) {
- selectWiki(wikiName);
+ selectWiki(cfile, wikiName);
}
} catch (Exception e) {
}
action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
}
}
-
- private void selectWiki(String wikiName) {
+
+ private void selectWiki(IFile cfile, String wikiName) {
Configuration configuration = getConfiguration();
if (configuration != null && !configuration.equals("")) {
- String url = generateUrl(configuration, configuration.getURL(), wikiName);
- String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
- if (wikiContent != null) {
- IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
- doc.set(wikiContent);
+ try {
+ String wikiLocale = configuration.getType().substring(WikiEditorPlugin.PREFIX_LOAD.length());
+ IWikipedia wikipediaProperties = WikiEditorPlugin.getWikiInstance(wikiLocale);
+
+ // String url = generateUrl(wikipediaProperties, configuration, configuration.getURL(), wikiName);
+ // MediaWikiConnector mc = new MediaWikiConnector();
+ // String wikiContent = mc.getWikiRawText(wikiName, url);
+ // if (wikiContent != null) {
+ // IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
+ // doc.set(wikiContent);
+ // }
+ IFile[] files = new IFile[1];
+ files[0] = cfile;
+
+ Job job = new RefreshJob(wikipediaProperties, files, configuration.getURL());
+ job.setRule(null);
+ job.setUser(true);
+ job.setPriority(Job.SHORT);
+ job.schedule();
+ } catch (Exception e) {
+ e.printStackTrace();
+ WikiEditorPlugin.getDefault().reportError("Exception occured: ",
+ e.getMessage() + "\nSee stacktrace in /.metadata/.log file.");
}
}
}