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 public void dispose() {
56 protected Configuration getConfiguration() {
57 List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
58 ArrayList configsList = new ArrayList();
59 for (int i = 0; i < allConfigsList.size(); i++) {
60 IConfiguration temp = (IConfiguration) allConfigsList.get(i);
61 if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_SQL)) {
62 configsList.add(temp);
65 Collections.sort(configsList);
66 Configuration configuration = null;
67 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
68 .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
69 "Select the refresh URL.");
70 listSelectionDialog.setTitle("Multiple active configuration found");
71 if (listSelectionDialog.open() == Window.OK) {
72 Object[] locations = listSelectionDialog.getResult();
73 if (locations != null) {
74 for (int i = 0; i < locations.length; i++) {
75 configuration = (Configuration) locations[i];
83 public IDocument getDocument() {
84 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
88 private String getWikiFile(IFile file) {
89 return Util.getFileWikiName(file);
92 public void init(IWorkbenchWindow window) {
96 void openWikiFile(IFile cfile) {
97 String wikiName = getWikiFile(cfile);
99 if (fEditor != null) {
100 selectWiki(wikiName);
102 } catch (Exception e) {
107 public void openWikiLinkOnSelection() {
108 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
109 openWikiFile(ei.getFile());
112 public void run(IAction action) {
113 if (fEditor == null) {
114 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
115 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
116 fEditor = (AbstractTextEditor) targetEditor;
119 if (fEditor != null) {
120 openWikiLinkOnSelection();
124 public void selectionChanged(IAction action, ISelection selection) {
125 if (selection.isEmpty()) {
128 if (selection instanceof TextSelection) {
129 action.setEnabled(true);
132 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
133 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
137 private void selectWiki(String wikiName) {
138 String wikiContent = WikipediaDB.getExactText(wikiName);
139 if (wikiContent != null) {
140 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
141 doc.set(wikiContent);
145 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
146 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
147 fEditor = (AbstractTextEditor) targetEditor;
148 text = new EditorText(targetEditor);