1 package net.sourceforge.phpeclipse.wiki.actions;
3 import java.io.ByteArrayInputStream;
5 import net.sourceforge.phpeclipse.wiki.editor.WikiEditor;
6 import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin;
7 import net.sourceforge.phpeclipse.wiki.preferences.Util;
9 import org.eclipse.core.resources.IContainer;
10 import org.eclipse.core.resources.IFile;
11 import org.eclipse.core.resources.IFolder;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IResourceStatus;
15 import org.eclipse.core.resources.ResourcesPlugin;
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.Path;
20 import org.eclipse.core.runtime.SubProgressMonitor;
21 import org.eclipse.jface.action.IAction;
22 import org.eclipse.jface.text.BadLocationException;
23 import org.eclipse.jface.text.IDocument;
24 import org.eclipse.jface.text.ITextSelection;
25 import org.eclipse.jface.text.TextSelection;
26 import org.eclipse.jface.viewers.ISelection;
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.ide.IDE;
32 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
33 import org.eclipse.ui.texteditor.AbstractTextEditor;
35 public final class OpenWikiLinkEditorAction implements IEditorActionDelegate {
37 private IWorkbenchWindow window;
39 private AbstractTextEditor editor;
41 public void dispose() {
44 public void init(IWorkbenchWindow window) {
48 public void selectionChanged(IAction action, ISelection selection) {
49 if (selection.isEmpty()) {
52 if (selection instanceof TextSelection) {
53 action.setEnabled(true);
56 if (window.getActivePage() != null && window.getActivePage().getActivePart() != null) {
57 action.setEnabled(window.getActivePage().getActivePart().getClass().equals(WikiEditor.class));
61 public void run(IAction action) {
63 IEditorPart targetEditor = window.getActivePage().getActiveEditor();
64 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
65 editor = (AbstractTextEditor) targetEditor;
69 openWikiLinkOnSelection();
73 public void setActiveEditor(IAction action, IEditorPart targetEditor) {
74 if (targetEditor != null && (targetEditor instanceof AbstractTextEditor)) {
75 editor = (AbstractTextEditor) targetEditor;
79 public static String getWikiString(AbstractTextEditor editor, IDocument document, int initialPos) {
82 int line = document.getLineOfOffset(pos);
83 int start = document.getLineOffset(line);
84 int end = start + document.getLineInformation(line).getLength();
87 * 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
88 * determining the TextRegion at the cursor position
94 int offsetInLine = pos - start;
95 String word = document.get(start, end - start);
96 int wordlen = word.length();
97 int wikiLinkStart = -1;
100 for (int i = offsetInLine; i < wordlen; i++) {
101 if (word.charAt(i) == ']' && i < wordlen - 1 && word.charAt(i + 1) == ']') {
105 if (word.charAt(i) == '|') {
109 if (word.charAt(i) == '#') {
114 for (int i = offsetInLine; i >= 0; i--) {
115 if (word.charAt(i) == '[' && i > 0 && word.charAt(i - 1) == '[') {
116 wikiLinkStart = i + 1;
119 if (word.charAt(i) == '|') { // links wih different description
122 if (word.charAt(i) == '#') { // for links with anchors
126 if (wikiLinkStart != (-1) && wikiLinkEnd != (-1) && wikiLinkStart < wikiLinkEnd) {
127 return new String(word.toCharArray(), wikiLinkStart, wikiLinkEnd - wikiLinkStart);
129 } catch (BadLocationException e) {
135 public IDocument getDocument() {
136 IDocument doc = editor.getDocumentProvider().getDocument(editor.getEditorInput());
140 public void openWikiLinkOnSelection() {
141 IDocument doc = getDocument();
142 ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection();
143 int pos = selection.getOffset();
145 String textRegion = getWikiString(editor, doc, pos);
146 IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput();
147 openWikiFile(ei.getFile(), textRegion);
150 public static void openWikiUrl(IProject project, String word) {
151 if (word != null && !word.equals("")) {
152 IFile cfile = project.getFile("dummy.wp");
153 IFile file = getWikiFile(cfile, word);
155 createNewFileIfNeeded(file, word);
156 // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) {
158 // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file));
160 IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true);
163 } catch (Exception e) {
164 // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE),
165 // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e);
170 public static void openWikiFile(IFile cfile, String word) {
171 if (word != null && !word.equals("")) {
172 IFile file = getWikiFile(cfile, word);
174 createNewFileIfNeeded(file, word);
175 // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) {
177 // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file));
179 IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true);
182 } catch (Exception e) {
183 // WikiEditorPlugin.getDefault().logAndReport(WikiEditorPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TITLE),
184 // WikiPlugin.getResourceString(WikiConstants.RESOURCE_WIKI_ERROR_DIALOGUE_OPEN_WIKI_FILE_TEXT), e);
189 private static void createNewFileIfNeeded(IFile file, String word) throws CoreException {
190 if (!file.exists()) {
191 createWikiFile(file, word);
195 private static IFile getWikiFile(IFile file, String word) {
196 String wikiFileName = Util.getWikiFileName(word, file, WikiEditorPlugin.HTML_OUTPUT_PATH);
197 IPath path = new Path(wikiFileName);
198 return ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
202 * Creates a folder resource handle for the folder with the given workspace path. This method does not create the folder resource;
203 * this is the responsibility of <code>createFolder</code>.
206 * the path of the folder resource to create a handle for
207 * @return the new folder resource handle
210 private static IFolder createFolderHandle(IPath folderPath) {
211 return IDEWorkbenchPlugin.getPluginWorkspace().getRoot().getFolder(folderPath);
214 private static void createFolder(IFolder folderHandle, IProgressMonitor monitor) throws CoreException {
216 // Create the folder resource in the workspace
217 // Recursive to create any folders which do not exist already
218 if (!folderHandle.exists()) {
219 IContainer parent = folderHandle.getParent();
220 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
221 createFolder((IFolder) parent, monitor);
223 // if (linkTargetPath != null)
224 // folderHandle.createLink(linkTargetPath, IResource.ALLOW_MISSING_LOCAL, monitor);
226 folderHandle.create(false, true, monitor);
228 } catch (CoreException e) {
229 // If the folder already existed locally, just refresh to get contents
230 if (e.getStatus().getCode() == IResourceStatus.PATH_OCCUPIED)
231 folderHandle.refreshLocal(IResource.DEPTH_INFINITE, new SubProgressMonitor(monitor, 500));
237 private static void createWikiFile(IFile file, String word) throws CoreException {
238 IContainer parent = file.getParent();
239 if (parent instanceof IFolder && (!((IFolder) parent).exists())) {
240 createFolder((IFolder) parent, null);
242 String newText = "<!--" + word + "-->";
243 byte[] buffer = newText.getBytes();
244 ByteArrayInputStream source = new ByteArrayInputStream(buffer);
245 file.create(source, true, null);