1 package org.simantics.sysdyn.ui.properties.widgets.arrays;
\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
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
17 public class UsedEnumerations extends ViewpointContributorImpl<ResourceArray> {
\r
20 public Collection<?> getContribution(ReadGraph graph, ResourceArray input)
\r
21 throws DatabaseException {
\r
22 SysdynResource sr = SysdynResource.getInstance(graph);
\r
24 Resource[] selection = input.resources;
\r
25 if(selection.length < 1)
\r
26 return Collections.emptyList();
\r
28 /* Collect enumerations for all variables
\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
43 variableEnumerations.add(new ArrayList<Resource>());
\r
48 /* Construct a list of enumerations
\r
57 ArrayList<List<Resource>> enumerations = new ArrayList<List<Resource>>();
\r
62 // Break loop if enumerations for all variables have been recorded
\r
64 for(List<Resource> varEnums : variableEnumerations) {
\r
65 if(index < varEnums.size()) {
\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
84 ArrayList<AbstractNode<?>> result = new ArrayList<AbstractNode<?>>();
\r
86 if(enumerations.size() == 1 && (enumerations.get(0).size() == 0 || (enumerations.get(0).size() == 1 && enumerations.get(0).get(0) == null)))
\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
100 result.add(new ConflictingEnumerationNode(enumeration.toArray(new Resource[enumeration.size()])));
\r
102 result.add(new EnumerationNode(enumeration.get(0)));
\r
112 public String getViewpointId() {
\r
113 return "Used enumerations";
\r