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