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.procedure.adapter.TransientCacheAsyncListener;
26 import org.simantics.db.common.request.ResourceRead;
27 import org.simantics.db.common.utils.NameUtils;
28 import org.simantics.db.exception.DatabaseException;
29 import org.simantics.db.layer0.exception.PendingVariableException;
30 import org.simantics.viewpoint.ontology.ViewpointResource;
33 * A child contribution adds child nodes to the instances of a specific node type.
34 * @author Hannu Niemistö
36 public class ChildContribution {
40 NodeType parentNodeType;
41 NodeType childNodeType;
44 public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,
45 ChildRule childRule) throws InvalidContribution {
46 if(!childRule.isCompatible(
47 parentNodeType.getContentType()
49 throw new InvalidContribution("Child rule is not compatible with the parent content type.");
50 this.parentNodeType = parentNodeType;
51 this.childNodeType = childNodeType;
52 this.childRule = childRule;
53 this.identifier = identifier;
54 this.priority = priority;
57 public static ChildContribution createCached(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
59 return g.syncRequest(new ResourceRead<ChildContribution>(childContributionResource) {
61 public ChildContribution perform(ReadGraph graph) throws DatabaseException {
63 return create(g, resource);
64 } catch (InvalidContribution e) {
65 throw new DatabaseException(e);
68 }, TransientCacheAsyncListener.instance());
69 } catch (DatabaseException e) {
70 Throwable c = e.getCause();
71 if (c instanceof InvalidContribution)
72 throw (InvalidContribution) c;
77 public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
78 ViewpointResource vr = ViewpointResource.getInstance(g);
80 Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);
81 NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);
83 Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);
84 NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);
86 Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);
87 ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);
89 String identifier = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_identifier, Bindings.STRING);
90 if(identifier == null) identifier = "";
91 if("".equals(identifier)) {
92 identifier = g.getPossibleURI(childContributionResource);
93 if(identifier == null) identifier = NameUtils.getSafeName(g, childContributionResource, true);
96 Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);
97 if(priority == null) priority = 0.0;
99 return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);
103 public NodeType getParentNodeType() {
104 return parentNodeType;
107 public NodeType getChildNodeType() {
108 return childNodeType;
111 public String getIdentifier() {
115 public double getPriority() {
120 * Given a parent node context returns a child node contexts contributed by
123 public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {
125 Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
126 Collection<?> childContents = childRule.getChildren(graph, parentContent);
127 if(childContents.isEmpty())
128 return Collections.emptyList();
129 ArrayList<NodeContext> children = new ArrayList<NodeContext>(childContents.size());
130 for(Object childContent : childContents) {
131 NodeContext child = childNodeType.createNodeContext(graph, childContent);
136 } catch(PendingVariableException e) {
137 return Collections.emptyList();
138 } catch(DatabaseException e) {
140 // TODO return some kind of error node
141 return Collections.emptyList();
146 * Given a child node context returns a collection of possible parent node contexts.
148 public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {
150 Object childContent = child.getConstant(BuiltinKeys.INPUT);
151 Collection<?> parentContents = childRule.getParents(graph, childContent);
152 if(parentContents.isEmpty())
153 return Collections.emptyList();
154 ArrayList<NodeContext> parents = new ArrayList<NodeContext>(parentContents.size());
155 for(Object parentContent : parentContents) {
156 NodeContext parent = parentNodeType.createNodeContext(graph, parentContent);
161 } catch(DatabaseException e) {
163 return Collections.emptyList();
167 public boolean hasChildren(ReadGraph graph, NodeContext parent) {
169 Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
170 Collection<?> childContents = childRule.getChildren(graph, parentContent);
171 if(childContents.isEmpty())
173 for(Object childContent : childContents) {
174 NodeContext child = childNodeType.createNodeContext(graph, childContent);
178 } catch(DatabaseException e) {
185 public String toString() {