Improve media management in DocBook.
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 2 Feb 2021 13:16:57 +0000 (14:16 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 2 Feb 2021 13:16:57 +0000 (14:16 +0100)
publishing/org.argeo.publishing.ui/OSGI-INF/l10n/bundle.properties
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkMsg.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/DbkUtils.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/AbstractDbkViewer.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkContextMenu.java
publishing/org.argeo.publishing.ui/src/org/argeo/docbook/ui/DbkImageManager.java

index 77c5423fcf6df057ca9bcc1ad6ef377b3b81b135..54f5ce8e75c78c324faebaec97e00a390be6cab9 100644 (file)
@@ -3,6 +3,7 @@ section=section
 media=media
 style=style
 
+insertParagraph=insert paragraph
 deleteParagraph=delete paragraph
 deleteSection=delete section
 
index 6dc1c5110d23d1456487eb115985f3e2d6321919..7f82178ce181cc4fa32c5e1a00a9479f12f1e806 100644 (file)
@@ -8,7 +8,7 @@ public enum DbkMsg implements Localized {
        //
        section, deleteSection,
        //
-       media, deleteMedia, insertMedia,
+       media, deleteMedia, insertMedia, insertParagraph,
        //
        ;
 }
index 19f7cd40fd905dd46c543785e3d292cb655caf34..226df4b544786178fe21107741247b5c7cfd818b 100644 (file)
@@ -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);
                }
index 02509f25732bec1b037f840a9b81526e94e3c68b..44e6f547a4088f6ff59f642c30a7cf58df1e7102 100644 (file)
@@ -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
index ef2695a778831db7518994d820472bd3fa286bd0..77248d6f140fa065ed6a30e7c7f4bcfedea449d5 100644 (file)
@@ -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);
index 8ff17b8676281961916e68aa672e0dd38f8b427b..008ac76538d6f658b075b42473d4ea2a540d98b9 100644 (file)
@@ -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()))