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