]> gerrit.simantics Code Review - simantics/platform.git/blob
f3d67e69f03b887f0f60fdb48760cdd95063abc9
[simantics/platform.git] /
1 package org.simantics.document.linking.report.evaluator;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashMap;
6 import java.util.List;
7 import java.util.Map;
8
9 import org.eclipse.jface.resource.ImageDescriptor;
10 import org.simantics.db.ReadGraph;
11 import org.simantics.db.Resource;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.variable.Variable;
14 import org.simantics.document.linking.Activator;
15 import org.simantics.document.linking.ontology.DocumentLink;
16 import org.simantics.document.linking.report.DocumentLine;
17 import org.simantics.objmap.graph.annotations.OrderedSetType;
18 import org.simantics.objmap.graph.annotations.RelatedGetValue;
19 import org.simantics.objmap.graph.annotations.RelatedSetValue;
20
21 @OrderedSetType(DocumentLink.URIs.EvaluatorTree_Path)
22 public class Path extends EvaluatorNode implements StringEditableNode{
23         
24         String separator;
25         
26         public Path() {
27                 separator = "/";
28         }
29         
30         public Path(String separator) {
31                 this.separator = separator;
32         }
33         
34         @Override
35         public String toString() {
36                 return "path " + "(" + separator +")" ;
37         }
38         
39         @Override
40         public String setValue(String value) {
41                 separator = value;
42                 return null;
43         }
44         
45         @RelatedSetValue(DocumentLink.URIs.EvaluatorTree_HasValue)
46         public void _setValue(String value) {
47                 separator = value;
48         }
49         
50         @RelatedGetValue(DocumentLink.URIs.EvaluatorTree_HasValue)
51         @Override
52         public String getValue() {
53                 return separator;
54         }
55         
56         @Override
57         public String getValue(ReadGraph graph, Variable variable,
58                         Map<Object, Object> context) throws DatabaseException {
59                 Resource model = (Resource)context.get("model");
60                 String text = "";
61                 Variable parent = variable.getParent(graph);
62                 while (parent != null) {
63                         text = children.get(0).getValue(graph, parent, context) + separator + text;
64                         parent = parent.getParent(graph);
65                         if (model.equals(parent.getRepresents(graph)))
66                                 parent = null;
67                 }
68                 if (text.length() == 0)
69                         return null;
70                 return text.substring(0,text.length()-separator.length());
71         }
72         
73         @Override
74         public List<DocumentLine> getLines(ReadGraph graph, Variable variable,
75                         Map<Object, Object> context) throws DatabaseException {
76                 List<DocumentLine> result = new ArrayList<DocumentLine>();
77                 Resource model = (Resource)context.get("model");
78                 for (int i = 0 ; i < children.size(); i++) {
79                         Variable parent = variable.getParent(graph);
80                         while (parent != null) {
81                                 List<DocumentLine> list = children.get(i).getLines(graph, parent, context);
82                                 for (int j = 0; j < list.size(); j++) {
83                                         DocumentLine line = list.get(j); 
84                                         if (j <= result.size()) {
85                                                 result.add(line);
86                                         } else {
87                                                 DocumentLine extLine = result.get(j);
88                                                 String combined = line.getLine() + separator + extLine.getLine();
89                                                 Map<Object,Object> combinedSet = new HashMap<Object, Object>();
90                                                 combinedSet.putAll(extLine.getHints());
91                                                 combinedSet.putAll(line.getHints());
92                                                 
93                                                 result.set(j, new DocumentLine(combined,combinedSet));
94                                         }
95                                 }
96                                 parent = parent.getParent(graph);
97                                 if (model.equals(parent.getRepresents(graph)))
98                                         parent = null;
99                         }
100                 }
101                 return result;
102         }
103         
104         @Override
105         public ImageDescriptor getImage() {
106                 return Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/folder.png");
107         }
108         
109         @Override
110         public EvaluatorItem copy() {
111                 Path path = new Path();
112                 return path;
113         }
114         
115         @Override
116         public List<Class<? extends EvaluatorItem>> getPossibleChildren(boolean add) {
117                 if (add && children.size() > 0)
118                         return Collections.emptyList();
119                 return super.getPossibleChildren(add);
120         }
121         
122         @Override
123         public boolean acceptChild(EvaluatorItem item) {
124                 return children.size() == 0;
125         }
126         
127         @Override
128         public boolean acceptChild(int index, EvaluatorItem item) {
129                 return children.size() == 0;
130         }
131
132 }