X-Git-Url: http://git.phpeclipse.com diff --git a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java index d8b4537..c09b190 100644 --- a/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java +++ b/archive/net.sourceforge.phpeclipse.wiki/src/net/sourceforge/phpeclipse/wiki/actions/OpenWikiLinkEditorAction.java @@ -5,6 +5,8 @@ import java.io.ByteArrayInputStream; import net.sourceforge.phpeclipse.wiki.editor.WikiEditor; import net.sourceforge.phpeclipse.wiki.editor.WikiEditorPlugin; import net.sourceforge.phpeclipse.wiki.preferences.Util; +import net.sourceforge.phpeclipse.wiki.xml.Page; +import net.sourceforge.phpeclipse.wiki.xml.XStreamManager; import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; @@ -32,11 +34,11 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; import org.eclipse.ui.texteditor.AbstractTextEditor; -public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { +public class OpenWikiLinkEditorAction implements IEditorActionDelegate { - private IWorkbenchWindow window; + protected IWorkbenchWindow window; - private AbstractTextEditor editor; + protected AbstractTextEditor editor; public void dispose() { } @@ -76,7 +78,7 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { } } - public static String getWikiString(AbstractTextEditor editor, IDocument document, int initialPos) { + public static String getWikiTitle(AbstractTextEditor editor, IDocument document, int initialPos) { try { int pos = initialPos; int line = document.getLineOfOffset(pos); @@ -116,7 +118,7 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { wikiLinkStart = i + 1; break; } - if (word.charAt(i) == '|') { // links wih different description + if (word.charAt(i) == '|') { // links with different description wikiLinkEnd = i; } if (word.charAt(i) == '#') { // for links with anchors @@ -141,58 +143,82 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { IDocument doc = getDocument(); ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); int pos = selection.getOffset(); - - String textRegion = getWikiString(editor, doc, pos); + + String wikiTitle = getWikiTitle(editor, doc, pos); IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); - openWikiFile(ei.getFile(), textRegion); + openWikiFile(ei.getFile(), wikiTitle, true); } - public static void openWikiUrl(IProject project, String word) { - if (word != null && !word.equals("")) { - IFile cfile = project.getFile("dummy.wp"); - IFile file = getWikiFile(cfile, word); + // public void openWikiUrl(IProject project, String wikiTitle) { + // if (wikiTitle != null && !wikiTitle.equals("")) { + // IFile cfile = project.getFile("dummy.wp"); + // IFile file = getWikiFile(cfile, wikiTitle); + // try { + // createNewFileIfNeeded(file, wikiTitle); + // + // IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); + // } 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); + // } + // } + // } + + protected void openWikiFile(IFile cfile, String wikiTitle, boolean openEditor) { + if (wikiTitle != null && !wikiTitle.equals("")) { + IFile file = getWikiFile(cfile, wikiTitle); try { - createNewFileIfNeeded(file, word); - // if (WikiEditorPlugin.getDefault().getPreferenceStore().getBoolean(WikiConstants.REUSE_EDITOR)) { - // saveIfNeeded(); - // getActivePage().reuseEditor(reusableEditor, new FileEditorInput(file)); - // } else { + createNewFileIfNeeded(file, wikiTitle); + + Page page = new Page("", wikiTitle, ""); + page.createXMLFile(file, false); +// createXMLFile(wikiTitle, file, false); + IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); - // 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); } - } - } - - public static 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 { - IDE.openEditor(WikiEditorPlugin.getDefault().getActivePage(), file, true); - // 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); + } + } + + /** + * @param wikiTitle + * @param file + * @throws CoreException + */ + private void createXMLFile(String wikiTitle, IFile file, boolean modify) throws CoreException { + String srcBasePath = Util.getWikiTextsPath(file); + String binBasePath = Util.getProjectsWikiOutputPath(file.getProject(), WikiEditorPlugin.HTML_OUTPUT_PATH); + + String filename = Util.getXMLFileName(file, binBasePath, srcBasePath); + IPath path = new Path(filename); + IFile xmlFile = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); + IContainer parent = xmlFile.getParent(); + if (parent instanceof IFolder && (!((IFolder) parent).exists())) { + Util.createFolder((IFolder) parent, null); + } + Page page = new Page("", wikiTitle, ""); + byte[] buffer = XStreamManager.toXML(page).getBytes(); + ByteArrayInputStream source = new ByteArrayInputStream(buffer); + if (!xmlFile.exists()) { + // only if file doesn't exists + xmlFile.create(source, true, null); + } else { + if (modify) { + xmlFile.setContents(source, true, true, null); } - } + } } - private static void createNewFileIfNeeded(IFile file, String word) throws CoreException { + protected void createNewFileIfNeeded(IFile file, String word) throws CoreException { if (!file.exists()) { - createWikiFile(file, word); + createWikiFile(file); } } - private static IFile getWikiFile(IFile file, String word) { + protected 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); @@ -234,12 +260,12 @@ public final class OpenWikiLinkEditorAction implements IEditorActionDelegate { } } - private static void createWikiFile(IFile file, String word) throws CoreException { + private void createWikiFile(IFile file) throws CoreException { IContainer parent = file.getParent(); if (parent instanceof IFolder && (!((IFolder) parent).exists())) { createFolder((IFolder) parent, null); } - String newText = ""; + String newText = WikiEditorPlugin.AUTOMATICALLY_CREATED; byte[] buffer = newText.getBytes(); ByteArrayInputStream source = new ByteArrayInputStream(buffer); file.create(source, true, null);