]> gerrit.simantics Code Review - simantics/platform.git/blob
e2a264069d296560a5d188847443bea224e0472e
[simantics/platform.git] /
1 package org.simantics.document.linking.report.evaluator;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.eclipse.jface.resource.ImageDescriptor;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.exception.DatabaseException;
11 import org.simantics.db.layer0.variable.Variable;
12 import org.simantics.document.linking.Activator;
13 import org.simantics.document.linking.ontology.DocumentLink;
14 import org.simantics.document.linking.report.DocumentLine;
15 import org.simantics.objmap.graph.annotations.OrderedSetType;
16 import org.simantics.objmap.graph.annotations.RelatedGetValue;
17 import org.simantics.objmap.graph.annotations.RelatedSetValue;
18
19 /**
20  * And node combines contents of the child nodes with configurable separator (default " ")
21  * 
22  * If all children return null, the return value is null.
23  * 
24  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
25  *
26  */
27 @OrderedSetType(DocumentLink.URIs.EvaluatorTree_And)
28 public class And extends EvaluatorNode implements StringEditableNode{
29         
30         String separator;
31         
32         
33         public And() {
34                 separator = " ";
35         }
36         
37         public And(String separator) {
38                 this.separator = separator;
39         }
40         
41         public void setSeparator(String separator) {
42                 this.separator = separator;
43         }
44         
45         public String getSeparator() {
46                 return separator;
47         }
48         
49         
50         @Override
51         public String getValue(ReadGraph graph, Variable variable, Map<Object, Object> context) throws DatabaseException {
52                 String s = "";
53                 for (int i = 0 ; i < children.size(); i++) {
54                         String s2 = children.get(i).getValue(graph, variable, context);
55                         if (s2 != null) {
56                                 s+= s2;
57                                 if (i < children.size()-1)
58                                         s+=separator;
59                         }
60                 }
61                 if (s.length() == 0)
62                         return null;
63                 return s;
64         }
65         
66         @Override
67         public List<DocumentLine> getLines(ReadGraph graph,     Variable variable, Map<Object, Object> context) throws DatabaseException {
68                 List<DocumentLine> result = new ArrayList<DocumentLine>();
69                 for (int i = 0 ; i < children.size(); i++) {
70                         List<DocumentLine> list = children.get(i).getLines(graph, variable, context);
71                         for (int j = 0; j < list.size(); j++) {
72                                 DocumentLine line = list.get(j); 
73                                 if (j <= result.size()) {
74                                         result.add(line);
75                                 } else {
76                                         DocumentLine extLine = result.get(j);
77                                         String combined = extLine.getLine() + separator + line.getLine();
78                                         Map<Object,Object> combinedSet = new HashMap<Object, Object>();
79                                         combinedSet.putAll(extLine.getHints());
80                                         combinedSet.putAll(line.getHints());
81                                         
82                                         result.set(j, new DocumentLine(combined,combinedSet));
83                                 }
84                         }
85                 }
86                 return result;
87         }
88         
89
90         @Override
91         public String toString() {
92                 return "and " + "(" + separator +")" ;
93         }
94         
95         @RelatedGetValue(DocumentLink.URIs.EvaluatorTree_HasValue)
96         @Override
97         public String getValue() {
98                 return separator;
99         }
100         
101         @Override
102         public String setValue(String value) {
103                 separator = value;
104                 return null;
105         }
106         
107         @RelatedSetValue(DocumentLink.URIs.EvaluatorTree_HasValue)
108         public void _setValue(String value) {
109                 separator = value;
110         }
111         
112         @Override
113         public EvaluatorItem copy() {
114                 And a = new And(separator);
115                 copyChildren(a);
116                 return a;
117         }
118         
119         @Override
120         public ImageDescriptor getImage() {
121                 return Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/text_columns.png");
122         }
123
124 }