Support section titles
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Sep 2022 06:53:58 +0000 (08:53 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 14 Sep 2022 06:53:58 +0000 (08:53 +0200)
org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkSectionTitle.java [new file with mode: 0644]
org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkTextInterpreter.java
org.argeo.app.swt/src/org/argeo/app/swt/docbook/DocBookViewer.java

diff --git a/org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkSectionTitle.java b/org.argeo.app.swt/src/org/argeo/app/swt/docbook/DbkSectionTitle.java
new file mode 100644 (file)
index 0000000..58dd263
--- /dev/null
@@ -0,0 +1,30 @@
+package org.argeo.app.swt.docbook;
+
+import org.argeo.api.acr.Content;
+import org.argeo.cms.swt.SwtEditablePart;
+import org.argeo.cms.swt.widgets.EditableText;
+import org.argeo.cms.ux.acr.ContentPart;
+import org.eclipse.swt.widgets.Composite;
+
+/** The title of a section, based on an XML text node. */
+public class DbkSectionTitle extends EditableText implements SwtEditablePart, ContentPart {
+       private static final long serialVersionUID = -1787983154946583171L;
+
+       private final TextSection section;
+
+       public DbkSectionTitle(Composite parent, int swtStyle, Content titleNode) {
+               super(parent, swtStyle);
+               section = (TextSection) TextSection.findSection(this);
+               setData(titleNode);
+       }
+
+       public TextSection getSection() {
+               return section;
+       }
+
+       @Override
+       public Content getContent() {
+               return (Content) getData();
+       }
+
+}
index 2fac3bbf09f226deafb549de7f21ce1c5da71f5c..1eff7a4edc80bbf5b3b220f87562c5f5591610df 100644 (file)
@@ -9,16 +9,20 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.util.List;
 
-import javax.xml.parsers.DocumentBuilderFactory;
 import javax.xml.transform.Result;
 import javax.xml.transform.Source;
 import javax.xml.transform.TransformerException;
 import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMResult;
 import javax.xml.transform.stream.StreamResult;
 
 import org.apache.commons.io.IOUtils;
 import org.argeo.api.acr.Content;
 import org.argeo.app.docbook.DbkType;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+import org.w3c.dom.NodeList;
+import org.w3c.dom.Text;
 
 /** Based on HTML with a few Wiki-like shortcuts. */
 public class DbkTextInterpreter implements TextInterpreter {
@@ -95,15 +99,12 @@ public class DbkTextInterpreter implements TextInterpreter {
        @Override
        public String raw(Content node) {
                if (isDbk(node, para) || isDbk(node, title)) {
-                       Source source = node.adapt(Source.class);
-
-                       StringWriter stringWriter = new StringWriter();
-                       Result result = new StreamResult(stringWriter);
-
-                       try {
+                       try (StringWriter stringWriter = new StringWriter()) {
+                               Source source = node.adapt(Source.class);
+                               Result result = new StreamResult(stringWriter);
                                transformerFactory.newTransformer().transform(source, result);
                                return stringWriter.toString();
-                       } catch (TransformerException e) {
+                       } catch (TransformerException | IOException e) {
                                throw new RuntimeException("Could not convert " + node + " to XML", e);
                        }
 
@@ -168,6 +169,27 @@ public class DbkTextInterpreter implements TextInterpreter {
 //     }
 
        private void readAsSimpleHtml(Content node, StringBuilder sb) {
+               DOMResult result = new DOMResult();
+               try {
+                       Source source = node.adapt(Source.class);
+                       transformerFactory.newTransformer().transform(source, result);
+               } catch (TransformerException e) {
+                       throw new RuntimeException("Could not convert " + node + " to XML", e);
+               }
+
+               NodeList nl = result.getNode().getChildNodes();
+               for (int i = 0; i < nl.getLength(); i++) {
+                       Node n = nl.item(i);
+//                     if (n instanceof Text) {
+//                             Text text = (Text) n;
+//                             sb.append(text.getTextContent());
+//                     } else 
+                               if (n instanceof Element) {
+                               Element elem = (Element) n;
+                               sb.append(elem.getTextContent());
+                       }
+               }
+
 //             NodeIterator nit = node.getNodes();
 //             while (nit.hasNext()) {
 //                     Node child = nit.nextNode();
index 6cd5172aceda5bcbe68a0b5ef9cea61d97552ba3..5e94e48f1695a687840ca6597d5c70b30fb0780b 100644 (file)
@@ -16,6 +16,7 @@ import org.argeo.cms.swt.acr.SwtSection;
 import org.argeo.cms.swt.acr.SwtSectionPart;
 import org.argeo.cms.swt.widgets.EditableText;
 import org.argeo.cms.swt.widgets.StyledControl;
+import org.eclipse.swt.SWT;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Control;
 
@@ -25,6 +26,10 @@ public class DocBookViewer extends AbstractPageViewer {
 
        private TextSection mainSection;
 
+       private boolean showMainTitle = true;
+
+       private String defaultSectionStyle;
+
        public DocBookViewer(Composite parent, int style, Content item, CmsEditable cmsEditable) {
                super(parent, style, cmsEditable);
                for (Content child : item) {
@@ -54,6 +59,27 @@ public class DocBookViewer extends AbstractPageViewer {
        }
 
        protected void refreshTextSection(TextSection section) {
+               Content sectionContent = section.getContent();
+               // Style
+               Optional<String> roleAttr = sectionContent.get(DbkAttr.role.qName(), String.class);
+               String style = roleAttr.orElse(section.getDefaultTextStyle());
+               if (style != null)
+                       CmsSwtUtils.style(section, style);
+
+               // Title
+               Optional<Content> titleContent = sectionContent.soleChild(DbkType.title.qName());
+
+               if (titleContent.isPresent()) {
+                       boolean showTitle = getMainSection() == section ? showMainTitle : true;
+                       if (showTitle) {
+                               if (section.getHeader() == null)
+                                       section.createHeader();
+                               DbkSectionTitle title = newSectionTitle(section, titleContent.get());
+                               title.setLayoutData(CmsSwtUtils.fillWidth());
+                               updateContent(title);
+                       }
+               }
+
                for (Content child : section.getContent()) {
                        if (child.hasContentClass(DbkType.section.qName())) {
                                TextSection childSection = new TextSection(section, 0, child);
@@ -89,9 +115,9 @@ public class DocBookViewer extends AbstractPageViewer {
                                if (paragraph == getEdited())
                                        paragraph.setText(textInterpreter.raw(partContent));
                                else
-                                       paragraph.setText(textInterpreter.raw(partContent));
-                                       //paragraph.setText(textInterpreter.readSimpleHtml(partContent));
-                               
+                                       paragraph.setText(textInterpreter.readSimpleHtml(partContent));
+                               // paragraph.setText(textInterpreter.readSimpleHtml(partContent));
+
 //                     } else if (part instanceof DbkImg) {
 //                             DbkImg editableImage = (DbkImg) part;
 //                             // imageManager.load(partNode, part.getControl(),
@@ -101,16 +127,54 @@ public class DocBookViewer extends AbstractPageViewer {
 //                             video.load(part.getControl());
 //                     }
                        }
+               } else if (part instanceof DbkSectionTitle) {
+                       DbkSectionTitle title = (DbkSectionTitle) part;
+                       title.setStyle(title.getSection().getTitleStyle());
+                       // use control AFTER setting style
+                       if (title == getEdited())
+                               title.setText(textInterpreter.read(title.getContent()));
+                       else
+                               title.setText(textInterpreter.readSimpleHtml(title.getContent()));
                }
-//                     else if (part instanceof DbkSectionTitle) {
-//                     DbkSectionTitle title = (DbkSectionTitle) part;
-//                     title.setStyle(title.getSection().getTitleStyle());
-//                     // use control AFTER setting style
-//                     if (title == getEdited())
-//                             title.setText(textInterpreter.read(title.getNode()));
-//                     else
-//                             title.setText(textInterpreter.readSimpleHtml(title.getNode()));
-//             }
+       }
+
+       protected DbkSectionTitle newSectionTitle(TextSection parent, Content titleNode) {
+               int style = parent.getStyle();
+               Composite titleParent = newSectionHeader(parent);
+               if (parent.isTitleReadOnly())
+                       style = style | SWT.READ_ONLY;
+               DbkSectionTitle title = new DbkSectionTitle(titleParent, style, titleNode);
+               updateContent(title);
+               title.setMouseListener(getMouseListener());
+               title.setFocusListener(getFocusListener());
+               return title;
+       }
+
+       /**
+        * To be overridden in order to provide additional processing at the section
+        * level.
+        * 
+        * @return the parent to use for the {@link DbkSectionTitle}, by default
+        *         {@link Section#getHeader()}
+        */
+       protected Composite newSectionHeader(TextSection section) {
+               return section.getHeader();
+       }
+
+       public TextSection getMainSection() {
+               return mainSection;
+       }
+
+       public void setShowMainTitle(boolean showMainTitle) {
+               this.showMainTitle = showMainTitle;
+       }
+
+       public String getDefaultSectionStyle() {
+               return defaultSectionStyle;
+       }
+
+       public void setDefaultSectionStyle(String defaultSectionStyle) {
+               this.defaultSectionStyle = defaultSectionStyle;
        }
 
        @Override