]> gerrit.simantics Code Review - simantics/platform.git/blob
1f47964376f6540f380fbd4f29c77eb6a85be1ed
[simantics/platform.git] /
1 package org.simantics.document.linking.report.templates;
2
3 import java.net.URL;
4 import java.util.Collection;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.document.DocumentResource;
11 import org.simantics.document.linking.report.Document.TextSize;
12 import org.simantics.document.linking.report.TableColumn.Alignment;
13 import org.simantics.document.linking.report.Document;
14 import org.simantics.document.linking.report.TextItem;
15 import org.simantics.document.linking.report.URLItem;
16 import org.simantics.document.linking.report.evaluator.AlignmentHint;
17 import org.simantics.document.linking.report.evaluator.Date;
18 import org.simantics.document.linking.report.evaluator.EvaluatorNode;
19 import org.simantics.document.linking.report.evaluator.Lines;
20 import org.simantics.document.linking.report.evaluator.Or;
21 import org.simantics.document.linking.report.evaluator.TextSizeHint;
22 import org.simantics.document.linking.report.evaluator.Variable;
23 import org.simantics.document.linking.report.templates.custom.CustomizableContent;
24 import org.simantics.document.linking.report.templates.custom.EvaluatorCustomizableContent;
25
26
27 public abstract  class DocumentWriter<T> extends TableReportWriter<T> implements CustomizableContentProvider{
28         
29         Map<String, CustomizableContent> content = new HashMap<String, CustomizableContent>();
30         ReadGraph graph;
31         DocumentResource doc;
32         Document document;
33         Map<Object, Object> context;
34         
35         public DocumentWriter() {
36                 setDefaultContent(null);
37         }
38         
39         @Override
40         public void start(ReadGraph graph, Resource model, Document writer,
41                         Map<Object, Object> context) throws Exception {
42                 this.graph = graph;
43                 this.doc = DocumentResource.getInstance(graph);
44                 this.context = context;
45                 this.document = writer;
46         }
47         
48         
49         @Override
50         public CustomizableContent getContent(String id) {
51                 return content.get(id);
52         }
53         
54         @Override
55         public void setContent(String id, CustomizableContent content) {
56                 this.content.put(id, content);
57         }
58         
59         @Override
60         public void setDefaultContent(String id) {
61                 if ("Document".equals(id) || id == null) {
62                         EvaluatorCustomizableContent c = new EvaluatorCustomizableContent("Document format");
63                         Or item = new Or();
64                         item.addChild(new Variable("#HasLabel"));
65                         item.addChild(new Variable("#HasName"));
66                         c.setItem(item);
67                         c.setSupportStyles(false);
68                         c.setSupportMultiline(true);
69                         content.put("Document", c);
70                 } 
71                 if ("Title".equals(id) || id == null) {
72                         EvaluatorCustomizableContent e = new EvaluatorCustomizableContent("Document Title");
73                         EvaluatorNode lines = new Lines();
74                         Or nameOr = new Or();
75                         nameOr.createChild(Variable.class).setVariableRef("#HasLabel");
76                         nameOr.createChild(Variable.class).setVariableRef("#HasName");
77                         lines.createChild(TextSizeHint.class).setTextSize(TextSize.HUGE).createChild(AlignmentHint.class).setAlignment(Alignment.CENTER).addChild(nameOr.copy());
78                         lines.createChild(TextSizeHint.class).setTextSize(TextSize.HUGE).createChild(AlignmentHint.class).setAlignment(Alignment.CENTER).createChild(Variable.class).setVariableRef("DocumentName");
79                         lines.createChild(TextSizeHint.class).setTextSize(TextSize.LARGE).createChild(AlignmentHint.class).setAlignment(Alignment.CENTER).createChild(Date.class);
80                         e.setItem(lines);
81                         e.setSupportStyles(true);
82                         e.setSupportMultiline(true);
83                         content.put("Title", e);
84                 }
85         }
86         
87         @Override
88         public Collection<String> getContentIds() {
89                 return content.keySet();
90         }
91         
92         protected TextItem getDocumentItem(Resource document) throws Exception{
93                 TextItem item;
94                 if (graph.isInstanceOf(document, doc.UrlDocument)) {
95                         item = this.document.newItem(URLItem.class);
96                         ((URLItem)item).setURL(new URL((String)graph.getRelatedValue(document, doc.HasUrl)));
97                 } else {
98                         item = this.document.newItem(TextItem.class);
99                 }
100                 item.setText(getContent("Document").getContent(graph, document,context));
101                 return item;
102         }
103         
104         protected TextItem getNonExistingDocumentItem() throws Exception{
105                 TextItem item = this.document.newItem(TextItem.class);
106                 item.setText("[DOCUMENT DOES NOT EXIST]");
107                 return item;
108         }
109
110
111 }