package net.sourceforge.phpeclipse.wiki.actions.mediawiki; import java.io.ByteArrayInputStream; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collections; import java.util.List; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.internal.Configuration; import net.sourceforge.phpeclipse.wiki.internal.ConfigurationManager; import net.sourceforge.phpeclipse.wiki.internal.IConfiguration; import net.sourceforge.phpeclipse.wiki.preferences.Util; import net.sourceforge.phpeclipse.wiki.sql.WikipediaDB; import net.sourceforge.phpeclipse.wiki.velocity.EditorText; 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.jface.action.IAction; import org.eclipse.jface.text.IDocument; import org.eclipse.jface.text.ITextSelection; import org.eclipse.jface.text.TextSelection; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.jface.window.Window; import org.eclipse.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; 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 LoadWikipediaSQLAction implements IEditorActionDelegate { private AbstractTextEditor fEditor; private EditorText text; private IWorkbenchWindow window; public void dispose() { } protected Configuration getConfiguration() { 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_SQL)) { configsList.add(temp); } } Collections.sort(configsList); Configuration configuration = null; ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench() .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(), "Select the refresh URL."); listSelectionDialog.setTitle("Multiple active configuration found"); if (listSelectionDialog.open() == Window.OK) { Object[] locations = listSelectionDialog.getResult(); if (locations != null) { for (int i = 0; i < locations.length; i++) { configuration = (Configuration) locations[i]; break; } } } return configuration; } public IDocument getDocument() { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); return doc; } private String getWikiFile(IFile file) { return Util.getURLWikiName(file); } public void init(IWorkbenchWindow window) { this.window = window; } void openWikiFile(IFile cfile) { String wikiName = getWikiFile(cfile); try { if (fEditor != null) { selectWiki(wikiName); } } catch (Exception e) { } } public void openWikiLinkOnSelection() { IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); openWikiFile(ei.getFile()); } public void run(IAction action) { if (fEditor == null) { IEditorPart targetEditor = window.getActivePage().getActiveEditor(); if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; } } if (fEditor != null) { openWikiLinkOnSelection(); } } public void selectionChanged(IAction action, ISelection selection) { if (selection.isEmpty()) { return; } if (selection instanceof TextSelection) { action.setEnabled(true); return; } if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) { action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class)); } } private void selectWiki(String wikiName) { String wikiContent = WikipediaDB.getExactText(wikiName); if (wikiContent != null) { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); doc.set(wikiContent); } } public void setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; text = new EditorText(targetEditor); } } }