version 1.1.2b
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / actions / mediawiki / DownloadWikipediaAction.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.velocity.EditorText;
16
17 import org.apache.velocity.VelocityContext;
18 import org.apache.velocity.app.Velocity;
19 import org.eclipse.core.resources.IContainer;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.IResourceStatus;
24 import org.eclipse.core.runtime.CoreException;
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.core.runtime.SubProgressMonitor;
28 import org.eclipse.jface.action.IAction;
29 import org.eclipse.jface.text.IDocument;
30 import org.eclipse.jface.text.ITextSelection;
31 import org.eclipse.jface.text.TextSelection;
32 import org.eclipse.jface.viewers.ISelection;
33 import org.eclipse.jface.viewers.LabelProvider;
34 import org.eclipse.jface.window.Window;
35 import org.eclipse.ui.IEditorActionDelegate;
36 import org.eclipse.ui.IEditorPart;
37 import org.eclipse.ui.IFileEditorInput;
38 import org.eclipse.ui.IWorkbenchWindow;
39 import org.eclipse.ui.dialogs.ListSelectionDialog;
40 import org.eclipse.ui.internal.dialogs.ListContentProvider;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
42 import org.eclipse.ui.texteditor.AbstractTextEditor;
43
44 public class DownloadWikipediaAction implements IEditorActionDelegate {
45
46   private AbstractTextEditor fEditor;
47
48   private EditorText text;
49
50   private IWorkbenchWindow window;
51
52   private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
53     try {
54       // Create the folder resource in the workspace
55       // Recursive to create any folders which do not exist already
56       if (!folderHandle.exists()) {
57         IContainer parent = folderHandle.getParent();
58         if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
59           createFolder((IFolder) parent, monitor);
60         }
61         folderHandle.create(false, true, monitor);
62       }
63     } catch (CoreException e) {
64       // If the folder already existed locally, just refresh to get contents
65       if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
66         folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
67       else
68         throw e;
69     }
70   }
71
72   /**
73    * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
74    * this is the responsibility of <code>createFolder</code>.
75    * 
76    * @param folderPath
77    *          the path of the folder resource to create a handle for
78    * @return the new folder resource handle
79    * @see #createFolder
80    */
81   private IFolder createFolderHandle(IPath folderPath) {
82     return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
83   }
84
85   private void createNewFileIfNeeded(IFile file, String word) throws CoreException {
86     if (!file.exists()) {
87       createWikiFile(file, word);
88     }
89   }
90
91   private void createWikiFile(IFile file, String word) throws CoreException {
92     IContainer parent = file.getParent();
93     if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
94       createFolder((IFolder) parent, null);
95     }
96     String newText = "<!--" + word + "-->";
97     byte[] buffer = newText.getBytes();
98     ByteArrayInputStream source = new ByteArrayInputStream(buffer);
99     file.create(source, true, null);
100   }
101
102   public void dispose() {
103   }
104
105   public String generateUrl(Configuration config, String template, String wikiname) {
106
107     /* first, we init the runtime engine. Defaults are fine. */
108
109     try {
110       Velocity.init();
111
112       /* lets make a Context and put data into it */
113
114       VelocityContext context = new VelocityContext();
115
116       context.put("config", config);
117       text.clear();
118       text.setWikiname(wikiname);
119       context.put("text", text);
120
121       /* lets make our own string to render */
122       StringWriter w = new StringWriter();
123       w = new StringWriter();
124       Velocity.evaluate(context, w, "mystring", template);
125       return w.toString();
126
127     } catch (Exception e) {
128       // TODO Auto-generated catch block
129       e.printStackTrace();
130     }
131     return template;
132   }
133
134   protected Configuration getConfiguration(){
135     List allConfigsList = ConfigurationManager.getInstance().getConfigurations();
136     ArrayList configsList = new ArrayList();
137     for (int i = 0; i < allConfigsList.size(); i++) {
138       IConfiguration temp = (IConfiguration) allConfigsList.get(i);
139       if (temp.getType().equals(WikiEditorPlugin.WIKIPEDIA_GET_TEXT)) {
140         configsList.add(temp);
141       }
142     }
143     Collections.sort(configsList);
144     Configuration configuration = null;
145     ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
146         .getActiveWorkbenchWindow().getShell(), configsList, new ListContentProvider(), new LabelProvider(),
147         "Select the refresh URL.");
148     listSelectionDialog.setTitle("Multiple active configuration found");
149     if (listSelectionDialog.open() == Window.OK) {
150       Object[] locations = listSelectionDialog.getResult();
151       if (locations != null) {
152         for (int i = 0; i < locations.length; i++) {
153           configuration = (Configuration) locations[i];
154           break;
155         }
156       }
157     }
158     return configuration;
159   }
160
161   public IDocument getDocument() {
162     IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
163     return doc;
164   }
165
166   private String getWikiFile(IFile file) {
167     return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
168   }
169
170   public void init(IWorkbenchWindow window) {
171     this.window = window;
172   }
173
174   void openWikiFile(IFile cfile) {
175     String wikiName = getWikiFile(cfile);
176     try {
177       if (fEditor != null) {
178         selectWiki(wikiName);
179       }
180     } catch (Exception e) {
181     }
182
183   }
184
185   public void openWikiLinkOnSelection() {
186     IDocument doc = getDocument();
187     ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
188     int pos = selection.getOffset();
189     IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
190     openWikiFile(ei.getFile());
191   }
192
193   public void run(IAction action) {
194     if (fEditor == null) {
195       IEditorPart targetEditor = window.getActivePage().getActiveEditor();
196       if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
197         fEditor = (AbstractTextEditor) targetEditor;
198       }
199     }
200     if (fEditor != null) {
201       openWikiLinkOnSelection();
202     }
203   }
204
205   public void selectionChanged(IAction action, ISelection selection) {
206     if (selection.isEmpty()) {
207       return;
208     }
209     if (selection instanceof TextSelection) {
210       action.setEnabled(true);
211       return;
212     }
213     if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
214       action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
215     }
216   }
217   
218   private void selectWiki(String wikiName) {
219     Configuration configuration = getConfiguration();
220     if (configuration != null && !configuration.equals("")) {
221       String url = generateUrl(configuration, configuration.getURL(), wikiName);
222       String wikiContent = MediaWikiConnector.getWikiRawText(wikiName, url);
223       if (wikiContent != null) {
224         IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
225         doc.set(wikiContent);
226       }
227     }
228   }
229
230   public void setActiveEditor(IAction action, IEditorPart targetEditor) {
231     if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
232       fEditor = (AbstractTextEditor) targetEditor;
233       text = new EditorText(targetEditor);
234     }
235   }
236 }