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