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 8f38c48..0a01234 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; @@ -76,7 +78,7 @@ public 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 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 @@ -126,10 +128,10 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { if (wikiLinkStart != (-1) && wikiLinkEnd != (-1) && wikiLinkStart < wikiLinkEnd) { return new String(word.toCharArray(), wikiLinkStart, wikiLinkEnd - wikiLinkStart); } - } catch (BadLocationException e) { + } catch (Exception e) { } - return ""; + return null; } public IDocument getDocument() { @@ -141,49 +143,75 @@ public class OpenWikiLinkEditorAction implements IEditorActionDelegate { IDocument doc = getDocument(); ITextSelection selection = (ITextSelection) editor.getSelectionProvider().getSelection(); int pos = selection.getOffset(); - - String textRegion = getWikiString(editor, doc, pos); - IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); - openWikiFile(ei.getFile(), textRegion, true); + + String wikiTitle = getWikiTitle(editor, doc, pos); + if (wikiTitle != null && !wikiTitle.equals("")) { + IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput(); + openWikiFile(ei.getFile(), wikiTitle, true); + } } - public 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); } - } - } - - protected void openWikiFile(IFile cfile, String word, boolean openEditor) { - 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); } - } + } } protected void createNewFileIfNeeded(IFile file, String word) throws CoreException {