]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/children/RelationStatementRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / children / RelationStatementRule.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
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.Statement;
22 import org.simantics.db.exception.DatabaseException;
23
24 /**
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ö
28  */
29 public class RelationStatementRule implements ChildRule {
30
31     Resource relation;
32     
33     public RelationStatementRule(Resource relation, Test test) {
34         this.relation = relation;
35     }
36     
37     @Override
38     public Collection<?> getChildren(ReadGraph graph, Object parent) throws DatabaseException {
39         ArrayList<Statement> result = new ArrayList<Statement>();
40         for(Resource property : graph.getPredicates((Resource)parent)) {
41                 if(graph.isSubrelationOf(property, relation)) {
42                         result.addAll(graph.getStatements((Resource)parent, property));
43                 }
44         }
45         return result;
46     }
47
48     @Override
49     public Collection<?> getParents(ReadGraph graph, Object child)
50             throws DatabaseException {
51         return Collections.emptyList();
52     }
53     
54     @Override
55     public boolean isCompatible(Class<?> contentType) {
56         return contentType.equals(Resource.class);
57     }
58 }