1 package net.sourceforge.phpeclipse.wiki.actions.mediawiki;
3 import java.io.ByteArrayInputStream;
4 import java.util.ArrayList;
5 import java.util.Collections;
7 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
8 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
9 import net.sourceforge.phpeclipse.wiki.preferences.Util;
11 import org.eclipse.core.resources.IContainer;
12 import org.eclipse.core.resources.IFile;
13 import org.eclipse.core.resources.IFolder;
14 import org.eclipse.core.resources.IResource;
15 import org.eclipse.core.resources.IResourceStatus;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.core.runtime.SubProgressMonitor;
20 import org.eclipse.jface.action.IAction;
21 import org.eclipse.jface.text.IDocument;
22 import org.eclipse.jface.text.ITextSelection;
23 import org.eclipse.jface.text.TextSelection;
24 import org.eclipse.jface.viewers.ISelection;
25 import org.eclipse.jface.viewers.LabelProvider;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.ui.IEditorActionDelegate;
28 import org.eclipse.ui.IEditorPart;
29 import org.eclipse.ui.IFileEditorInput;
30 import org.eclipse.ui.IWorkbenchWindow;
31 import org.eclipse.ui.dialogs.ListSelectionDialog;
32 import org.eclipse.ui.internal.dialogs.ListContentProvider;
33 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
34 import org.eclipse.ui.texteditor.AbstractTextEditor;
36 public final class RefreshWikiTextEditorAction implements IEditorActionDelegate {
38 private IWorkbenchWindow window;
40 private AbstractTextEditor fEditor;
42 public void dispose() {
45 public void init(IWorkbenchWindow window) {
49 public void selectionChanged(IAction action, ISelection selection) {
50 if (selection.isEmpty()) {
53 if (selection instanceof TextSelection) {
54 action.setEnabled(true);
57 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
58 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
62 public void run(IAction action) {
63 if (fEditor == null) {
64 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
65 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
66 fEditor = (AbstractTextEditor) targetEditor;
69 if (fEditor != null) {
70 openWikiLinkOnSelection();
74 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
75 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
76 fEditor = (AbstractTextEditor) targetEditor;
80 public IDocument getDocument() {
81 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
85 public void openWikiLinkOnSelection() {
86 IDocument doc = getDocument();
87 ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection();
88 int pos = selection.getOffset();
89 // String textRegion = getWikiString(fEditor, doc, pos);
90 IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput();
91 openWikiFile(ei.getFile());
94 void openWikiFile(IFile cfile) {
95 String wikiName = getWikiFile(cfile);
97 if (fEditor != null) {
100 } catch (Exception e) {
105 private void selectWiki(String wikiName) {
106 String exampleWikiURL = "http://en.wikibooks.org/w/wiki.phtml";
107 String selectedWikiURL = null;
108 ArrayList locationsList = new ArrayList();
109 locationsList.add(exampleWikiURL);
111 Collections.sort(locationsList);
113 ListSelectionDialog listSelectionDialog = new ListSelectionDialog(WikiEditorPlugin.getDefault().getWorkbench()
114 .getActiveWorkbenchWindow().getShell(), locationsList, new ListContentProvider(), new LabelProvider(),
115 "Select the refresh URL.");
116 listSelectionDialog.setTitle("Multiple active configuration found");
117 if (listSelectionDialog.open() == Window.OK) {
118 Object[] locations = listSelectionDialog.getResult();
119 if (locations != null) {
120 for (int i = 0; i < locations.length; i++) {
121 selectedWikiURL = (String) locations[i];
127 if (selectedWikiURL != null && !selectedWikiURL.equals("")) {
128 String wikiContent = MediaWikiConnector.getWikiText(wikiName, selectedWikiURL);
129 if (wikiContent != null) {
130 IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
131 doc.set(wikiContent);
136 private void createNewFileIfNeeded(IFile file, String word) throws CoreException {
137 if (!file.exists()) {
138 createWikiFile(file, word);
142 private String getWikiFile(IFile file) {
143 return Util.getFileWikiName(file, WikiEditorPlugin.HTML_OUTPUT_PATH);
144 // IPath path = new Path(wikiFileName);
145 // return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
149 * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
150 * this is the responsibility of <code>createFolder</code>.
153 * the path of the folder resource to create a handle for
154 * @return the new folder resource handle
157 private IFolder createFolderHandle(IPath folderPath) {
158 return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
161 private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
163 // Create the folder resource in the workspace
164 // Recursive to create any folders which do not exist already
165 if (!folderHandle.exists()) {
166 IContainer parent = folderHandle.getParent();
167 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
168 createFolder((IFolder) parent, monitor);
170 // if (linkTargetPath != null)
171 // folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
173 folderHandle.create(false, true, monitor);
175 } catch (CoreException e) {
176 // If the folder already existed locally, just refresh to get contents
177 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
178 folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
184 private void createWikiFile(IFile file, String word) throws CoreException {
185 IContainer parent = file.getParent();
186 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
187 createFolder((IFolder) parent, null);
189 String newText = "<!--" + word + "-->";
190 byte[] buffer = newText.getBytes();
191 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
192 file.create(source, true, null);