From f4c6893c66cd619e699cb9392ff8c45367796154 Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 2 Feb 2021 14:16:57 +0100 Subject: [PATCH] Improve media management in DocBook. --- .../OSGI-INF/l10n/bundle.properties | 1 + .../src/org/argeo/docbook/DbkMsg.java | 2 +- .../src/org/argeo/docbook/DbkUtils.java | 7 +-- .../argeo/docbook/ui/AbstractDbkViewer.java | 44 +++++++++++++++---- .../org/argeo/docbook/ui/DbkContextMenu.java | 10 +++++ .../org/argeo/docbook/ui/DbkImageManager.java | 25 ++++++++--- 6 files changed, 67 insertions(+), 22 deletions(-) diff --git a/publishing/org.argeo.publishing.ui/OSGI-INF/l10n/bundle.properties b/publishing/org.argeo.publishing.ui/OSGI-INF/l10n/bundle.properties index 77c5423..54f5ce8 100644 --- a/publishing/org.argeo.publishing.ui/OSGI-INF/l10n/bundle.properties +++ b/publishing/org.argeo.publishing.ui/OSGI-INF/l10n/bundle.properties @@ -3,6 +3,7 @@ section=section media=media style=style +insertParagraph=insert paragraph deleteParagraph=delete paragraph deleteSection=delete section diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkMsg.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkMsg.java index 6dc1c51..7f82178 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkMsg.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkMsg.java @@ -8,7 +8,7 @@ public enum DbkMsg implements Localized { // section, deleteSection, // - media, deleteMedia, insertMedia, + media, deleteMedia, insertMedia, insertParagraph, // ; } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java index 19f7cd4..226df4b 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java @@ -97,11 +97,6 @@ public class DbkUtils { public static Node insertImageAfter(Node sibling) { try { - // FIXME make it more robust - if (DbkType.imagedata.get().equals(sibling.getName())) { - sibling = sibling.getParent().getParent(); - } - Node parent = sibling.getParent(); Node mediaNode = addDbk(parent, DbkType.mediaobject); // TODO optimise? @@ -123,7 +118,7 @@ public class DbkUtils { // // TODO make it more robust and generic // String fileRef = mediaNode.getName(); // imageDataNode.setProperty(DocBookNames.DBK_FILEREF, fileRef); - return imageDataNode; + return mediaNode; } catch (RepositoryException e) { throw new JcrException("Cannot insert empty image after " + sibling, e); } diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java index 02509f2..44e6f54 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java @@ -114,10 +114,11 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke Node child = ni.nextNode(); SectionPart sectionPart = null; if (isDbk(child, DbkType.mediaobject)) { - if (child.hasNode(DbkType.imageobject.get())) { - Node imageDataNode = child.getNode(DbkType.imageobject.get()).getNode(DbkType.imagedata.get()); - sectionPart = newImg(textSection, imageDataNode); - } + sectionPart = newImg(textSection, child); +// if (child.hasNode(DbkType.imageobject.get())) { +// Node imageDataNode = child.getNode(DbkType.imageobject.get()).getNode(DbkType.imagedata.get()); +// sectionPart = newImg(textSection, imageDataNode); +// } } else if (isDbk(child, para)) { sectionPart = newParagraph(textSection, child); } else { @@ -356,22 +357,47 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke } } - void insertPart(Section section, Node node) { + SectionPart insertPart(Section section, Node node) { try { refresh(section); layoutPage(); + for (Control control : section.getChildren()) { + if (control instanceof SectionPart) { + SectionPart sectionPart = (SectionPart) control; + Node partNode = sectionPart.getNode(); + if (partNode.getPath().equals(node.getPath())) + return sectionPart; + } + } + throw new IllegalStateException("New section part " + node + "not found"); } catch (RepositoryException e) { throw new JcrException("Cannot insert part " + node + " in section " + section.getNode(), e); } } + void addParagraph(SectionPart partBefore, String txt) { + Section section = partBefore.getSection(); + SectionPart nextSectionPart = section.nextSectionPart(partBefore); + Node newNode = addDbk(section.getNode(), para); + textInterpreter.write(newNode, txt != null ? txt : ""); + if (nextSectionPart != null) { + try { + Node nextNode = nextSectionPart.getNode(); + section.getNode().orderBefore(Jcr.getIndexedName(newNode), Jcr.getIndexedName(nextNode)); + } catch (RepositoryException e) { + throw new JcrException("Cannot order " + newNode + " before " + nextSectionPart.getNode(), e); + } + } + Jcr.save(newNode); + Paragraph paragraph = (Paragraph) insertPart(partBefore.getSection(), newNode); + edit(paragraph, 0); + } + void deletePart(SectionPart sectionPart) { try { Node node = sectionPart.getNode(); Session session = node.getSession(); if (sectionPart instanceof DbkImg) { - // FIXME make it more robust - node = node.getParent().getParent(); if (!isDbk(node, DbkType.mediaobject)) throw new IllegalArgumentException("Node " + node + " is not a media object."); } @@ -862,8 +888,8 @@ public abstract class AbstractDbkViewer extends AbstractPageViewer implements Ke float height = bounds.height; float textLength = lbl.getText().length(); float area = width * height; - float charArea = area/textLength; - float lines = textLength/width; + float charArea = area / textLength; + float lines = textLength / width; float proportion = point.y * width + point.x; int pos = (int) (textLength * (proportion / area)); // TODO refine it diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java index ef2695a..77248d6 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java @@ -66,6 +66,7 @@ class DbkContextMenu { Img img = (Img) editablePart; deletePartB(parent, DbkMsg.deleteMedia.lead(), img); insertMediaB(parent, DbkMsg.insertMedia.lead(), img); + insertParagraphB(parent, DbkMsg.insertParagraph.lead(), img); } else if (editablePart instanceof DocBookSectionTitle) { DocBookSectionTitle sectionTitle = (DocBookSectionTitle) editablePart; @@ -145,6 +146,15 @@ class DbkContextMenu { } + protected void insertParagraphB(Composite parent, String msg, SectionPart sectionPart) { + Label insertMediaB = new Label(parent, SWT.NONE); + insertMediaB.setText(msg); + insertMediaB.addMouseListener((MouseDown) (e) -> { + textViewer.addParagraph(sectionPart, null); + hide(); + }); + } + protected void deletePartB(Composite parent, String msg, SectionPart sectionPart) { Label deleteB = new Label(shell, SWT.NONE); deleteB.setText(msg); diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java index 8ff17b8..008ac76 100644 --- a/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java @@ -38,11 +38,21 @@ public class DbkImageManager extends DefaultImageManager { this.baseFolder = baseFolder; } + Node getImageDataNode(Node mediaObjectNode) throws RepositoryException { + if (mediaObjectNode.hasNode(DbkType.imageobject.get())) { + Node imageDataNode = mediaObjectNode.getNode(DbkType.imageobject.get()).getNode(DbkType.imagedata.get()); + return imageDataNode; + } else { + throw new IllegalStateException("No image data found for " + mediaObjectNode); + } + } + @Override public Binary getImageBinary(Node node) throws RepositoryException { Node fileNode = null; - if (DbkUtils.isDbk(node, DbkType.imagedata)) { - fileNode = getFileNode(node); + if (DbkUtils.isDbk(node, DbkType.mediaobject)) { + Node imageDataNode = getImageDataNode(node); + fileNode = getFileNode(imageDataNode); } if (node.isNodeType(NT_FILE)) { fileNode = node; @@ -54,7 +64,8 @@ public class DbkImageManager extends DefaultImageManager { } } - public Point getImageSize(Node imageDataNode) throws RepositoryException { + public Point getImageSize(Node mediaObjectNode) throws RepositoryException { + Node imageDataNode = getImageDataNode(mediaObjectNode); Node fileNode = getFileNode(imageDataNode); if (fileNode == null) return new Point(0, 0); @@ -83,16 +94,18 @@ public class DbkImageManager extends DefaultImageManager { } @Override - protected void processNewImageFile(Node context, Node fileNode, ImageData id) + protected void processNewImageFile(Node mediaObjectNode, Node fileNode, ImageData id) throws RepositoryException, IOException { + Node imageDataNode = getImageDataNode(mediaObjectNode); updateSize(fileNode, id); String filePath = fileNode.getPath(); String relPath = filePath.substring(baseFolder.getPath().length() + 1); - context.setProperty(DbkAttr.fileref.name(), relPath); + imageDataNode.setProperty(DbkAttr.fileref.name(), relPath); } @Override - public String getImageUrl(Node imageDataNode) throws RepositoryException { + public String getImageUrl(Node mediaObjectNode) throws RepositoryException { + Node imageDataNode = getImageDataNode(mediaObjectNode); // TODO factorise String fileref = null; if (imageDataNode.hasProperty(DbkAttr.fileref.name())) -- 2.30.2