Added xstream handiling for Wikipedia upload/download
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / LoadWikipediaSQLAction.java
1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.StringWriter;
5 import java.util.ArrayList;
6 import java.util.Collections;
7 import java.util.List;
8
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;
17
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;
44
45 public class LoadWikipediaSQLAction implements IEditorActionDelegate {
46
47   private AbstractTextEditor fEditor;
48
49   private EditorText text;
50
51   private IWorkbenchWindow window;
52
53   public void dispose() {
54   }
55
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);
63       }
64     }
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];
76           break;
77         }
78       }
79     }
80     return configuration;
81   }
82
83   public IDocument getDocument() {
84     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
85     return doc;
86   }
87
88   private String getWikiFile(IFile file) {
89     return Util.getURLWikiName(file);
90   }
91
92   public void init(IWorkbenchWindow window) {
93     this.window = window;
94   }
95
96   void openWikiFile(IFile cfile) {
97     String wikiName = getWikiFile(cfile);
98     try {
99       if (fEditor != null) {
100         selectWiki(wikiName);
101       }
102     } catch (Exception e) {
103     }
104
105   }
106
107   public void openWikiLinkOnSelection() {
108     IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
109     openWikiFile(ei.getFile());
110   }
111
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;
117       }
118     }
119     if (fEditor != null) {
120       openWikiLinkOnSelection();
121     }
122   }
123
124   public void selectionChanged(IAction action, ISelection selection) {
125     if (selection.isEmpty()) {
126       return;
127     }
128     if (selection instanceof TextSelection) {
129       action.setEnabled(true);
130       return;
131     }
132     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
133       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
134     }
135   }
136
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);
142     }
143   }
144
145   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
146     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
147       fEditor = (AbstractTextEditor) targetEditor;
148       text = new EditorText(targetEditor);
149     }
150   }
151 }