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;
18 import org.simantics.browsing.ui.model.tests.Test;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.exception.NoInverseException;
25 * A child rule that says that there is exactly one child node whose content
26 * is equal to the content of the parent.
27 * @author Hannu Niemistö
29 public class RelationChildRule implements ChildRule {
34 public RelationChildRule(Resource relation, Test test) {
35 this.relation = relation;
40 public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
41 Collection<Resource> result = graph.getObjects((Resource)parent, relation);
43 ArrayList<Resource> filtered = new ArrayList<Resource>(result.size());
44 for(Resource r : result)
45 if(test.test(graph, r))
53 public Collection<?> getParents(ReadGraph graph, Object child)
54 throws DatabaseException {
56 if(child instanceof Resource) {
57 Resource inverse = graph.getInverse(relation);
58 return graph.getObjects((Resource)child, inverse);
60 } catch(NoInverseException e) {
62 return Collections.emptyList();
66 public boolean isCompatible(Class<?> contentType) {
67 return contentType.equals(Resource.class);