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.sql.WikipediaDB;
 
  16 import net.sourceforge.phpeclipse.wiki.velocity.EditorText;
 
  18 import org.apache.velocity.VelocityContext;
 
  19 import org.apache.velocity.app.Velocity;
 
  20 import org.eclipse.core.resources.IContainer;
 
  21 import org.eclipse.core.resources.IFile;
 
  22 import org.eclipse.core.resources.IFolder;
 
  23 import org.eclipse.core.resources.IResource;
 
  24 import org.eclipse.core.resources.IResourceStatus;
 
  25 import org.eclipse.core.runtime.CoreException;
 
  26 import org.eclipse.core.runtime.IPath;
 
  27 import org.eclipse.core.runtime.IProgressMonitor;
 
  28 import org.eclipse.core.runtime.SubProgressMonitor;
 
  29 import org.eclipse.jface.action.IAction;
 
  30 import org.eclipse.jface.text.IDocument;
 
  31 import org.eclipse.jface.text.ITextSelection;
 
  32 import org.eclipse.jface.text.TextSelection;
 
  33 import org.eclipse.jface.viewers.ISelection;
 
  34 import org.eclipse.jface.viewers.LabelProvider;
 
  35 import org.eclipse.jface.window.Window;
 
  36 import org.eclipse.ui.IEditorActionDelegate;
 
  37 import org.eclipse.ui.IEditorPart;
 
  38 import org.eclipse.ui.IFileEditorInput;
 
  39 import org.eclipse.ui.IWorkbenchWindow;
 
  40 import org.eclipse.ui.dialogs.ListSelectionDialog;
 
  41 import org.eclipse.ui.internal.dialogs.ListContentProvider;
 
  42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
 
  43 import org.eclipse.ui.texteditor.AbstractTextEditor;
 
  45 public class LoadWikipediaSQLAction implements IEditorActionDelegate {
 
  47   private AbstractTextEditor fEditor;
 
  49   private EditorText text;
 
  51   private IWorkbenchWindow window;
 
  53   private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
 
  55       // Create the folder resource in the workspace
 
  56       // Recursive to create any folders which do not exist already
 
  57       if (!folderHandle.exists()) {
 
  58         IContainer parent = folderHandle.getParent();
 
  59         if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
 
  60           createFolder((IFolder) parent, monitor);
 
  62         folderHandle.create(false, true, monitor);
 
  64     } catch (CoreException e) {
 
  65       // If the folder already existed locally, just refresh to get contents
 
  66       if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
 
  67         folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
 
  74    * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
 
  75    * this is the responsibility of <code>createFolder</code>.
 
  78    *          the path of the folder resource to create a handle for
 
  79    * @return the new folder resource handle
 
  82   private IFolder createFolderHandle(IPath folderPath) {
 
  83     return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
 
  86   private void createNewFileIfNeeded(IFile file, String word) throws CoreException {
 
  88       createWikiFile(file, word);
 
  92   private void createWikiFile(IFile file, String word) throws CoreException {
 
  93     IContainer parent = file.getParent();
 
  94     if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
 
  95       createFolder((IFolder) parent, null);
 
  97     String newText = "<!--" + word + "-->";
 
  98     byte[] buffer = newText.getBytes();
 
  99     ByteArrayInputStream source = new ByteArrayInputStream(buffer);
 
 100     file.create(source, true, null);
 
 103   public void dispose() {
 
 106   protected Configuration getConfiguration() {
 
 107     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
 
 108     ArrayList configsList = new ArrayList();
 
 109     for (int i = 0; i < allConfigsList.size(); i++) {
 
 110       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
 
 111       if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
 
 112         configsList.add(temp);
 
 115     Collections.sort(configsList);
 
 116     Configuration configuration = null;
 
 117     ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
 
 118         .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
 
 119         "Select the refresh URL.");
 
 120     listSelectionDialog.setTitle("Multiple active configuration found");
 
 121     if (listSelectionDialog.open() == Window.OK) {
 
 122       Object[] locations = listSelectionDialog.getResult();
 
 123       if (locations != null) {
 
 124         for (int i = 0; i < locations.length; i++) {
 
 125           configuration = (Configuration) locations[i];
 
 130     return configuration;
 
 133   public IDocument getDocument() {
 
 134     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
 
 138   private String getWikiFile(IFile file) {
 
 139     return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
 
 142   public void init(IWorkbenchWindow window) {
 
 143     this.window = window;
 
 146   void openWikiFile(IFile cfile) {
 
 147     String wikiName = getWikiFile(cfile);
 
 149       if (fEditor != null) {
 
 150         selectWiki(wikiName);
 
 152     } catch (Exception e) {
 
 157   public void openWikiLinkOnSelection() {
 
 158     IDocument doc = getDocument();
 
 159     ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
 
 160     int pos = selection.getOffset();
 
 161     IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
 
 162     openWikiFile(ei.getFile());
 
 165   public void run(IAction action) {
 
 166     if (fEditor == null) {
 
 167       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
 
 168       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
 
 169         fEditor = (AbstractTextEditor) targetEditor;
 
 172     if (fEditor != null) {
 
 173       openWikiLinkOnSelection();
 
 177   public void selectionChanged(IAction action, ISelection selection) {
 
 178     if (selection.isEmpty()) {
 
 181     if (selection instanceof TextSelection) {
 
 182       action.setEnabled(true);
 
 185     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
 
 186       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
 
 190   private void selectWiki(String wikiName) {
 
 191     String wikiContent = WikipediaDB.getExactText(wikiName);
 
 192     if (wikiContent != null) {
 
 193       IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
 
 194       doc.set(wikiContent);
 
 198   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
 
 199     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
 
 200       fEditor = (AbstractTextEditor) targetEditor;
 
 201       text = new EditorText(targetEditor);