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;
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() {
}
}
}
- 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);
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
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() {
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);
+
+ String wikiTitle = getWikiTitle(editor, doc, pos);
+ if (wikiTitle != null && !wikiTitle.equals("")) {
+ IFileEditorInput ei = (IFileEditorInput) editor.getEditorInput();
+ 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);
}
}
- 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 = "<!--" + word + "-->";
+ String newText = WikiEditorPlugin.AUTOMATICALLY_CREATED;
byte[] buffer = newText.getBytes();
ByteArrayInputStream source = new ByteArrayInputStream(buffer);
file.create(source, true, null);