]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/evaluator/If.java
e0e7839391ca9fefc508d8dc32bf818db8a3c8af
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / evaluator / If.java
1 package org.simantics.document.linking.report.evaluator;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.Map;
6
7 import org.eclipse.jface.resource.ImageDescriptor;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.document.linking.Activator;
12 import org.simantics.document.linking.ontology.DocumentLink;
13 import org.simantics.document.linking.report.DocumentLine;
14 import org.simantics.objmap.graph.annotations.OrderedSetType;
15
16 /**
17  * Checks that the first size does not return null or "false". 
18  * Depending on results evaluates the second child or the third (if it exists).
19  * 
20  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
21  *
22  */
23 @OrderedSetType(DocumentLink.URIs.EvaluatorTree_If)
24 public class If extends EvaluatorNode{
25
26         @Override
27         public String getValue(ReadGraph graph, Variable variable, Map<Object, Object> context) throws DatabaseException {
28                 if (children.size() > 3)
29                         throw new DatabaseException("If node has more than 3 children.");
30                 
31                 String ifVal = children.get(0).getValue(graph, variable, context);
32                 if (ifVal != null && ifVal.length() > 0 && !Boolean.FALSE.toString().equals(ifVal)) {
33                         return children.get(1).getValue(graph, variable, context);
34                 } else if (children.size() == 3) {
35                         return children.get(2).getValue(graph, variable, context);
36                 }
37                 return null;
38         }
39         
40         @Override
41         public List<DocumentLine> getLines(ReadGraph graph,     Variable variable, Map<Object, Object> context) throws DatabaseException {
42                 if (children.size() > 3)
43                         throw new DatabaseException("If node has more than 3 children.");
44                 String ifVal = children.get(0).getValue(graph, variable, context);
45                 if (ifVal != null && ifVal.length() > 0 && !Boolean.FALSE.toString().equals(ifVal)) {
46                         return children.get(1).getLines(graph, variable, context);
47                 } else if (children.size() == 3) {
48                         return children.get(2).getLines(graph, variable, context);
49                 }
50                 return Collections.emptyList();
51         }
52         
53
54         @Override
55         public String toString() {
56                 return "if";
57         }
58         
59         @Override
60         public List<Class<? extends EvaluatorItem>> getPossibleChildren(boolean add) {
61                 if (add && children.size() == 3)
62                         return Collections.emptyList();
63                 return super.getPossibleChildren(add);
64         }
65         
66         @Override
67         public EvaluatorItem copy() {
68                 If node = new If();
69                 copyChildren(node);
70                 return node;
71                 
72         }
73         
74         @Override
75         public ImageDescriptor getImage() {
76                 return Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/help.png");
77         }
78         
79         @Override
80         public boolean acceptChild(EvaluatorItem item) {
81                 return children.size() < 3;
82         }
83         
84         @Override
85         public boolean acceptChild(int index, EvaluatorItem item) {
86                 return children.size() < 3;
87         }
88
89 }