]> git.argeo.org Git - gpl/argeo-suite.git/blob - docbook/DocBookViewer.java
Prepare next development cycle
[gpl/argeo-suite.git] / docbook / DocBookViewer.java
1 package org.argeo.app.swt.docbook;
2
3 import static org.argeo.app.docbook.DbkAcrUtils.isDbk;
4 import static org.argeo.app.docbook.DbkType.para;
5
6 import java.util.Optional;
7
8 import org.argeo.api.acr.Content;
9 import org.argeo.api.cms.ux.CmsEditable;
10 import org.argeo.app.docbook.DbkAttr;
11 import org.argeo.app.docbook.DbkType;
12 import org.argeo.cms.swt.CmsSwtUtils;
13 import org.argeo.cms.swt.SwtEditablePart;
14 import org.argeo.cms.swt.acr.AbstractPageViewer;
15 import org.argeo.cms.swt.acr.SwtSection;
16 import org.argeo.cms.swt.acr.SwtSectionPart;
17 import org.argeo.cms.swt.widgets.EditableText;
18 import org.argeo.cms.swt.widgets.StyledControl;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21
22 public class DocBookViewer extends AbstractPageViewer {
23
24 private TextInterpreter textInterpreter = new DbkTextInterpreter();
25
26 private TextSection mainSection;
27
28 public DocBookViewer(Composite parent, int style, Content item, CmsEditable cmsEditable) {
29 super(parent, style, cmsEditable);
30 for (Content child : item) {
31 if (child.hasContentClass(DbkType.article.qName())) {
32 if (mainSection != null)
33 throw new IllegalStateException("Main section already created");
34 mainSection = new TextSection(parent, 0, child);
35 mainSection.setLayoutData(CmsSwtUtils.fillAll());
36 }
37 }
38 }
39
40 @Override
41 protected void refresh(Control control) {
42 if (!(control instanceof SwtSection))
43 return;
44 long begin = System.currentTimeMillis();
45 SwtSection section = (SwtSection) control;
46 if (section instanceof TextSection) {
47 CmsSwtUtils.clear(mainSection);
48 refreshTextSection(mainSection);
49
50 }
51 long duration = System.currentTimeMillis() - begin;
52 // System.out.println(duration + " ms - " + DbkUtils.getTitle(section.getNode()));
53
54 }
55
56 protected void refreshTextSection(TextSection section) {
57 for (Content child : section.getContent()) {
58 if (child.hasContentClass(DbkType.section.qName())) {
59 TextSection childSection = new TextSection(section, 0, child);
60 childSection.setLayoutData(CmsSwtUtils.fillAll());
61 refreshTextSection(childSection);
62 } else if (child.hasContentClass(DbkType.para.qName())) {
63 Paragraph para = new Paragraph(section, 0, child);
64 para.setLayoutData(CmsSwtUtils.fillWidth());
65 updateContent(para);
66 }
67
68 }
69 }
70
71 protected void updateContent(SwtEditablePart part) {
72 if (part instanceof SwtSectionPart) {
73 SwtSectionPart sectionPart = (SwtSectionPart) part;
74 Content partContent = sectionPart.getContent();
75
76 if (part instanceof StyledControl && (sectionPart.getSection() instanceof TextSection)) {
77 TextSection section = (TextSection) sectionPart.getSection();
78 StyledControl styledControl = (StyledControl) part;
79 if (isDbk(partContent, para)) {
80 Optional<String> roleAttr = partContent.get(DbkAttr.role.qName(), String.class);
81 String style = roleAttr.orElse(section.getDefaultTextStyle());
82 styledControl.setStyle(style);
83 }
84 }
85 // use control AFTER setting style, since it may have been reset
86
87 if (part instanceof EditableText) {
88 EditableText paragraph = (EditableText) part;
89 if (paragraph == getEdited())
90 paragraph.setText(textInterpreter.raw(partContent));
91 else
92 paragraph.setText(textInterpreter.raw(partContent));
93 //paragraph.setText(textInterpreter.readSimpleHtml(partContent));
94
95 // } else if (part instanceof DbkImg) {
96 // DbkImg editableImage = (DbkImg) part;
97 // // imageManager.load(partNode, part.getControl(),
98 // // editableImage.getPreferredImageSize());
99 // } else if (part instanceof DbkVideo) {
100 // DbkVideo video = (DbkVideo) part;
101 // video.load(part.getControl());
102 // }
103 }
104 }
105 // else if (part instanceof DbkSectionTitle) {
106 // DbkSectionTitle title = (DbkSectionTitle) part;
107 // title.setStyle(title.getSection().getTitleStyle());
108 // // use control AFTER setting style
109 // if (title == getEdited())
110 // title.setText(textInterpreter.read(title.getNode()));
111 // else
112 // title.setText(textInterpreter.readSimpleHtml(title.getNode()));
113 // }
114 }
115
116 @Override
117 public Control getControl() {
118 return mainSection;
119 }
120
121 }