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.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.model.InvalidContribution;
21 import org.simantics.browsing.ui.model.nodetypes.NodeType;
22 import org.simantics.databoard.Bindings;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.utils.NameUtils;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.layer0.exception.PendingVariableException;
28 import org.simantics.viewpoint.ontology.ViewpointResource;
31 * A child contribution adds child nodes to the instances of a specific node type.
32 * @author Hannu Niemistö
34 public class ChildContribution {
38 NodeType parentNodeType;
39 NodeType childNodeType;
42 public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,
43 ChildRule childRule) throws InvalidContribution {
44 if(!childRule.isCompatible(
45 parentNodeType.getContentType()
47 throw new InvalidContribution("Child rule is not compatible with the parent content type.");
48 this.parentNodeType = parentNodeType;
49 this.childNodeType = childNodeType;
50 this.childRule = childRule;
51 this.identifier = identifier;
52 this.priority = priority;
55 public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
56 ViewpointResource vr = ViewpointResource.getInstance(g);
58 Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);
59 NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);
61 Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);
62 NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);
64 Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);
65 ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);
67 String identifier = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_identifier, Bindings.STRING);
68 if(identifier == null) identifier = "";
69 if("".equals(identifier)) {
70 identifier = g.getPossibleURI(childContributionResource);
71 if(identifier == null) identifier = NameUtils.getSafeName(g, childContributionResource, true);
74 Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);
75 if(priority == null) priority = 0.0;
77 return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);
81 public NodeType getParentNodeType() {
82 return parentNodeType;
85 public NodeType getChildNodeType() {
89 public String getIdentifier() {
93 public double getPriority() {
98 * Given a parent node context returns a child node contexts contributed by
101 public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {
103 Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
104 Collection<?> childContents = childRule.getChildren(graph, parentContent);
105 if(childContents.isEmpty())
106 return Collections.emptyList();
107 ArrayList<NodeContext> children = new ArrayList<NodeContext>(childContents.size());
108 for(Object childContent : childContents) {
109 NodeContext child = childNodeType.createNodeContext(graph, childContent);
114 } catch(PendingVariableException e) {
115 return Collections.emptyList();
116 } catch(DatabaseException e) {
118 // TODO return some kind of error node
119 return Collections.emptyList();
124 * Given a child node context returns a collection of possible parent node contexts.
126 public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {
128 Object childContent = child.getConstant(BuiltinKeys.INPUT);
129 Collection<?> parentContents = childRule.getParents(graph, childContent);
130 if(parentContents.isEmpty())
131 return Collections.emptyList();
132 ArrayList<NodeContext> parents = new ArrayList<NodeContext>(parentContents.size());
133 for(Object parentContent : parentContents) {
134 NodeContext parent = parentNodeType.createNodeContext(graph, parentContent);
139 } catch(DatabaseException e) {
141 return Collections.emptyList();
145 public boolean hasChildren(ReadGraph graph, NodeContext parent) {
147 Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
148 Collection<?> childContents = childRule.getChildren(graph, parentContent);
149 if(childContents.isEmpty())
151 for(Object childContent : childContents) {
152 NodeContext child = childNodeType.createNodeContext(graph, childContent);
156 } catch(DatabaseException e) {
163 public String toString() {