]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/children/CompositeChildRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / children / CompositeChildRule.java
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.model.children;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.Collection;\r
16 import java.util.Collections;\r
17 import java.util.List;\r
18 \r
19 import org.simantics.db.ReadGraph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.db.common.utils.OrderedSetUtils;\r
22 import org.simantics.db.exception.DatabaseException;\r
23 \r
24 public class CompositeChildRule implements ChildRule {    \r
25     List<ChildRule> childRules;\r
26     \r
27     public CompositeChildRule(List<ChildRule> childRules) {\r
28         this.childRules = childRules;\r
29     }\r
30     \r
31     public CompositeChildRule(ReadGraph graph, Resource orderedSet) throws DatabaseException {\r
32         List<Resource> childRuleResources = OrderedSetUtils.toList(graph, orderedSet);\r
33         childRules = new ArrayList<ChildRule>(childRuleResources.size());\r
34         for(Resource childRuleResource : childRuleResources)\r
35             childRules.add(graph.adapt(childRuleResource, ChildRule.class));\r
36     }\r
37     \r
38     @Override\r
39     public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {\r
40         Collection<Object> result = Collections.singleton(parent);\r
41         for(int i=0;i<childRules.size();++i) {\r
42             ChildRule rule = childRules.get(i);\r
43             ArrayList<Object> children = new ArrayList<Object>();\r
44             for(Object r : result)\r
45                 children.addAll(rule.getChildren(graph, r));\r
46             result = children;\r
47         }\r
48         return result;\r
49     }\r
50     \r
51     @Override\r
52     public Collection<?> getParents(ReadGraph graph, Object child) throws DatabaseException {\r
53         Collection<Object> result = Collections.singleton(child);\r
54         for(int i=childRules.size()-1;i>=0;--i) {\r
55             ChildRule rule = childRules.get(i);\r
56             ArrayList<Object> parents = new ArrayList<Object>();\r
57             for(Object r : result)\r
58                 parents.addAll(rule.getParents(graph, r));\r
59             result = parents;\r
60         }\r
61         return result;\r
62     }\r
63 \r
64     @Override\r
65     public boolean isCompatible(Class<?> contentType) {\r
66         if(childRules.isEmpty())\r
67            return true; \r
68         return childRules.get(0).isCompatible(contentType);\r
69     }\r
70 }\r