1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.children;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
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;
24 public class CompositeChildRule implements ChildRule {
25 List<ChildRule> childRules;
27 public CompositeChildRule(List<ChildRule> childRules) {
28 this.childRules = childRules;
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));
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));
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));
65 public boolean isCompatible(Class<?> contentType) {
66 if(childRules.isEmpty())
68 return childRules.get(0).isCompatible(contentType);