package net.sourceforge.phpeclipse.wiki.actions.mediawiki; //Parts of this sources are copied and modified from the jEdit Wikipedia plugin: //http://www.djini.de/software/wikipedia/index.html // //The modified sources are available under the "Common Public License" //with permission from the original author: Daniel Wunsch import java.io.ByteArrayInputStream; import net.sourceforge.phpeclipse.wiki.actions.mediawiki.connect.MediaWikiConnector; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.preferences.Util; 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.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.SubProgressMonitor; import org.eclipse.jface.action.IAction; import org.eclipse.jface.text.BadLocationException; 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.ui.IEditorActionDelegate; import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IFileEditorInput; import org.eclipse.ui.IWorkbenchWindow; import org.eclipse.ui.ide.IDE; import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; import org.eclipse.ui.texteditor.AbstractTextEditor; public final class DownloadWikiLinkEditorAction implements IEditorActionDelegate { private IWorkbenchWindow window; private AbstractTextEditor fEditor; public void dispose() { } public void init(IWorkbenchWindow window) { this.window = window; } 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)); } } 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 setActiveEditor(IAction action, IEditorPart targetEditor) { if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) { fEditor = (AbstractTextEditor) targetEditor; } } public static String getWikiString(AbstractTextEditor editor, IDocument document, int initialPos) { try { int pos = initialPos; int line = document.getLineOfOffset(pos); int start = document.getLineOffset(line); int end = start + document.getLineInformation(line).getLength(); /* * The line does not include \n or \r so pos can be > end. Making pos = end in this case is safe for the purposes of * determining the TextRegion at the cursor position */ if (pos > end) { pos = end; } int offsetInLine = pos - start; String word = document.get(start, end - start); int wordlen = word.length(); int wikiLinkStart = -1; int wikiLinkEnd = -1; for (int i = offsetInLine; i < wordlen; i++) { if (word.charAt(i) == ']' && i < wordlen - 1 && word.charAt(i + 1) == ']') { wikiLinkEnd = i; break; } if (word.charAt(i) == '|') { wikiLinkEnd = i; break; } if (word.charAt(i) == '#') { wikiLinkEnd = i; break; } } for (int i = offsetInLine; i >= 0; i--) { if (word.charAt(i) == '[' && i > 0 && word.charAt(i - 1) == '[') { wikiLinkStart = i + 1; break; } if (word.charAt(i) == '|') { // links wih different description wikiLinkEnd = i; } if (word.charAt(i) == '#') { // for links with anchors wikiLinkEnd = i; } } if (wikiLinkStart != (-1) && wikiLinkEnd != (-1) && wikiLinkStart < wikiLinkEnd) { return new String(word.toCharArray(), wikiLinkStart, wikiLinkEnd - wikiLinkStart); } } catch (BadLocationException e) { } return ""; } public IDocument getDocument() { IDocument doc = fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput()); return doc; } public void openWikiLinkOnSelection() { IDocument doc = getDocument(); ITextSelection selection = (ITextSelection) fEditor.getSelectionProvider().getSelection(); int pos = selection.getOffset(); String textRegion = getWikiString(fEditor, doc, pos); IFileEditorInput ei = (IFileEditorInput) fEditor.getEditorInput(); openWikiFile(ei.getFile(), textRegion); } void openWikiFile(IFile cfile, String word) { if (word != null && !word.equals("")) { IFile file = getWikiFile(cfile, word); try { createNewFileIfNeeded(file, word); // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) { // saveIfNeeded(); // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file)); // } else { IEditorPart part = IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); if (part != null && (part instanceof AbstractTextEditor)) { AbstractTextEditor newEditor = (AbstractTextEditor) part; word = Util.titleToDB(word); MediaWikiConnector mc = new MediaWikiConnector(); String wikiText = mc.getWikiRawText(word, "http://en.wikibooks.org/w/wiki.phtml"); if (wikiText!=null) { IDocument doc = newEditor.getDocumentProvider().getDocument(newEditor.getEditorInput()); doc.set(wikiText); } } // redrawText(); // } } catch (Exception e) { // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE), // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e); } } } private void createNewFileIfNeeded(IFile file, String word) throws CoreException { if (!file.exists()) { createWikiFile(file, word); } } private IFile getWikiFile(IFile file, String word) { String wikiFileName = Util.getWikiFileName(word, file, WikiEditorPlugin.HTML_OUTPUT_PATH); IPath path = new Path(wikiFileName); return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); } /** * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource; * this is the responsibility of createFolder. * * @param folderPath * the path of the folder resource to create a handle for * @return the new folder resource handle * @see #createFolder */ private IFolder createFolderHandle(IPath folderPath) { return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath); } private void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException { try { // Create the folder resource in the workspace // Recursive to create any folders which do not exist already if (!folderHandle.exists()) { IContainer parent = folderHandle.getParent(); if (parent instanceof IFolder && (!((IFolder) parent).exists())) { createFolder((IFolder) parent, monitor); } // if (linkTargetPath != null) // folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor); // else folderHandle.create(false, true, monitor); } } catch (CoreException e) { // If the folder already existed locally, just refresh to get contents if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED) folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500)); else throw e; } } private void createWikiFile(IFile file, String word) throws CoreException { IContainer parent = file.getParent(); if (parent instanceof IFolder && (!((IFolder) parent).exists())) { createFolder((IFolder) parent, null); } String newText = ""; byte[] buffer = newText.getBytes(); ByteArrayInputStream source = new ByteArrayInputStream(buffer); file.create(source, true, null); } }