]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/children/ChildContribution.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / children / ChildContribution.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.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;
29
30 /**
31  * A child contribution adds child nodes to the instances of a specific node type.
32  * @author Hannu Niemistö
33  */
34 public class ChildContribution {
35         
36         double priority;
37         String identifier;
38     NodeType parentNodeType;
39     NodeType childNodeType;
40     ChildRule childRule;
41         
42     public ChildContribution(String identifier, double priority, NodeType parentNodeType, NodeType childNodeType,
43             ChildRule childRule) throws InvalidContribution {
44         if(!childRule.isCompatible(
45                 parentNodeType.getContentType()
46                 ))
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;
53     }
54
55     public static ChildContribution create(ReadGraph g, Resource childContributionResource) throws DatabaseException, InvalidContribution {
56         ViewpointResource vr = ViewpointResource.getInstance(g);
57         
58         Resource parentNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasParentNodeType);
59         NodeType parentNodeType = g.adapt(parentNodeTypeResource, NodeType.class);
60         
61         Resource childNodeTypeResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasChildNodeType);
62         NodeType childNodeType = g.adapt(childNodeTypeResource, NodeType.class);
63         
64         Resource childRuleResource = g.getSingleObject(childContributionResource, vr.ChildContribution_HasRule);
65         ChildRule childRule = g.adapt(childRuleResource, ChildRule.class);
66         
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);
72         }
73         
74         Double priority = g.getPossibleRelatedValue(childContributionResource, vr.ChildContribution_priority, Bindings.DOUBLE);
75         if(priority == null) priority = 0.0;
76         
77         return new ChildContribution(identifier, priority, parentNodeType, childNodeType, childRule);
78         
79     }
80
81     public NodeType getParentNodeType() {
82         return parentNodeType;
83     }
84     
85     public NodeType getChildNodeType() {
86         return childNodeType;
87     }
88     
89     public String getIdentifier() {
90         return identifier;
91     }
92
93     public double getPriority() {
94         return priority;
95     }
96     
97     /**
98      * Given a parent node context returns a child node contexts contributed by
99      * this contribution.
100      */
101     public Collection<NodeContext> getChildren(ReadGraph graph, NodeContext parent) {
102         try {
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);
110                 if(child != null)
111                     children.add(child);
112             }
113             return children;
114         } catch(PendingVariableException e) {
115             return Collections.emptyList();
116         } catch(DatabaseException e) {
117             e.printStackTrace();
118             // TODO return some kind of error node
119             return Collections.emptyList();
120         }
121     }
122     
123     /**
124      * Given a child node context returns a collection of possible parent node contexts.
125      */
126     public Collection<NodeContext> getParents(ReadGraph graph, NodeContext child) {
127         try {
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);
135                 if(parent != null)
136                     parents.add(parent);
137             }
138             return parents;
139         } catch(DatabaseException e) {
140             e.printStackTrace();
141             return Collections.emptyList();
142         }
143     }
144
145     public boolean hasChildren(ReadGraph graph, NodeContext parent) {
146         try {
147             Object parentContent = parent.getConstant(BuiltinKeys.INPUT);
148             Collection<?> childContents = childRule.getChildren(graph, parentContent);
149             if(childContents.isEmpty())
150                 return false;
151             for(Object childContent : childContents) {
152                 NodeContext child = childNodeType.createNodeContext(graph, childContent);
153                 if(child != null)
154                     return true;
155             }
156         } catch(DatabaseException e) {
157             e.printStackTrace();
158         }
159         return false;
160     }
161     
162     @Override
163     public String toString() {
164         return identifier;
165     }
166     
167 }