]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
29402910c8d0b2f01ea5e79c7838d670a54ef645
[simantics/sysdyn.git] /
1 package org.simantics.sysdyn.ui.properties.widgets.arrays;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 import java.util.Collections;\r
6 import java.util.List;\r
7 \r
8 import org.simantics.browsing.ui.common.node.AbstractNode;\r
9 import org.simantics.browsing.ui.graph.impl.contributor.viewpoint.ViewpointContributorImpl;\r
10 import org.simantics.db.ReadGraph;\r
11 import org.simantics.db.Resource;\r
12 import org.simantics.db.common.ResourceArray;\r
13 import org.simantics.db.common.utils.ListUtils;\r
14 import org.simantics.db.exception.DatabaseException;\r
15 import org.simantics.sysdyn.SysdynResource;\r
16 \r
17 public class UsedEnumerations extends ViewpointContributorImpl<ResourceArray> {\r
18 \r
19                 @Override\r
20                 public Collection<?> getContribution(ReadGraph graph, ResourceArray input)\r
21                                 throws DatabaseException {\r
22                 SysdynResource sr = SysdynResource.getInstance(graph);\r
23                 \r
24                 Resource[] selection = input.resources;\r
25                 if(selection.length < 1)\r
26                     return Collections.emptyList();\r
27                 \r
28                 /* Collect enumerations for all variables\r
29                  * var1\r
30                  *     - enum1\r
31                  *     - enum2\r
32                  * var2\r
33                  *     - enum1\r
34                  *     - enum2\r
35                  *     - enum3\r
36                  */\r
37                 ArrayList<List<Resource>> variableEnumerations = new ArrayList<List<Resource>>();\r
38                 for(Resource variable : selection) {\r
39                     Resource arrayIndexes = graph.getPossibleObject(variable, sr.Variable_arrayIndexesList);\r
40                     if(arrayIndexes != null) {\r
41                         variableEnumerations.add(ListUtils.toList(graph, arrayIndexes));\r
42                     } else {\r
43                         variableEnumerations.add(new ArrayList<Resource>());\r
44                     }\r
45                 }\r
46 \r
47 \r
48                 /* Construct a list of enumerations\r
49                  * FirstEnumeration\r
50                  *     - var1.first\r
51                  *     - var2.first\r
52                  *     - var3.first\r
53                  * SecondEnumeration\r
54                  *     - var1.second\r
55                  *     - ...\r
56                  */\r
57                 ArrayList<List<Resource>> enumerations = new ArrayList<List<Resource>>();\r
58 \r
59                 int index = 0;\r
60                 while(true) {\r
61 \r
62                     // Break loop if enumerations for all variables have been recorded\r
63                     boolean b = true;\r
64                     for(List<Resource> varEnums : variableEnumerations) {\r
65                         if(index < varEnums.size()) {\r
66                             b = false;\r
67                             break;\r
68                         }\r
69                     }\r
70                     \r
71                     if(b) break;\r
72                     \r
73                     for(List<Resource> current : variableEnumerations) {\r
74                         if(index == enumerations.size())\r
75                             enumerations.add(index, new ArrayList<Resource>());\r
76                         Resource enumeration =  index < current.size()? current.get(index) : null;\r
77                         enumerations.get(index).add(enumeration);\r
78                     }\r
79                     \r
80                     index++;\r
81                 }\r
82                 \r
83                 // Build nodes\r
84                 ArrayList<AbstractNode<?>> result = new ArrayList<AbstractNode<?>>();\r
85 \r
86                 if(enumerations.size() == 1 && (enumerations.get(0).size() == 0 || (enumerations.get(0).size() == 1 && enumerations.get(0).get(0) == null)))\r
87                     return result;\r
88                 \r
89                 for(List<Resource> enumeration : enumerations) {\r
90                     boolean same = true;\r
91                     for(int i = 0; i < enumeration.size() - 1; i++) {\r
92                         if(enumeration.get(i) == null || enumeration.get(i + 1) == null ||\r
93                                 !enumeration.get(i).equals(enumeration.get(i + 1))) {\r
94                             same = false;\r
95                             break;\r
96                         }\r
97                     }\r
98                     \r
99                     if(!same) {\r
100                         result.add(new ConflictingEnumerationNode(enumeration.toArray(new Resource[enumeration.size()])));\r
101                     } else {\r
102                         result.add(new EnumerationNode(enumeration.get(0)));\r
103                     }\r
104                 }\r
105                 \r
106 \r
107                 \r
108                 return result;\r
109                 }\r
110 \r
111                 @Override\r
112                 public String getViewpointId() {\r
113                         return "Used enumerations";\r
114                 }\r
115 }\r