]> gerrit.simantics Code Review - simantics/interop.git/blob - org.simantics.interop.mapping/src/org/simantics/interop/mapping/MappingTools.java
refs #3483
[simantics/interop.git] / org.simantics.interop.mapping / src / org / simantics / interop / mapping / MappingTools.java
1 package org.simantics.interop.mapping;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Collection;\r
5 \r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.interop.mapping.data.GraphNode;\r
10 import org.simantics.interop.mapping.data.Identifiable;\r
11 import org.simantics.interop.mapping.data.Link;\r
12 import org.simantics.utils.datastructures.MapList;\r
13 \r
14 public class MappingTools {\r
15 \r
16         public static Resource getSymbolType(ReadGraph g, GraphNode<Identifiable> node) throws DatabaseException {\r
17                 Resource type = node.getHint(MappingHints.KEY_GENERATE_TYPE);\r
18                 if (type == null) {\r
19                         ModuleResolver resolver = node.getHint(MappingHints.KEY_RESOLVER);\r
20                         if (resolver != null)\r
21                                 type = resolver.getSymbolType(g);\r
22                         \r
23                 }\r
24                 return type;\r
25         }\r
26         \r
27         public static Resource getTerminal(ReadGraph g, Link<Identifiable> link) throws DatabaseException {\r
28                 Resource terminal = link.getHint(MappingHints.KEY_CONNECTION_TERMINAL);\r
29                 if (terminal == null) {\r
30                         ModuleResolver resolver = link.from().getHint(MappingHints.KEY_RESOLVER);\r
31                         if (resolver != null)\r
32                                 terminal = resolver.getTerminal(g, link);\r
33                         \r
34                 }\r
35                 return terminal;\r
36         }\r
37         \r
38         public static void assignGenerationRule(GraphNode<Identifiable> node, int index, GenerationRule rule) {\r
39                 MapList<Integer,GenerationRule> rules = node.getHint(MappingHints.KEY_GENERATION_RULES);\r
40                 if (rules == null) {\r
41                         rules = new MapList<Integer,GenerationRule>();\r
42                         node.setHint(MappingHints.KEY_GENERATION_RULES, rules);\r
43                 }\r
44                 rules.add(index,rule);\r
45                 \r
46         }\r
47         \r
48         public static void createEmptyGenerationRules(GraphNode<Identifiable> node) {\r
49                 node.setHint(MappingHints.KEY_GENERATION_RULES, new MapList<Integer,GenerationRule>());\r
50         }\r
51         \r
52         public static void copyGenerationRules(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
53                 MapList<Integer,GenerationRule> rules = from.getHint(MappingHints.KEY_GENERATION_RULES);\r
54                 if (rules == null)\r
55                         return;\r
56                 MapList<Integer,GenerationRule> toRules = to.getHint(MappingHints.KEY_GENERATION_RULES);\r
57                 if (toRules == null) {\r
58                         toRules = new MapList<Integer,GenerationRule>();\r
59                         to.setHint(MappingHints.KEY_GENERATION_RULES, toRules);\r
60                 }\r
61                 for (Integer i : rules.getKeys()) {\r
62                         for (GenerationRule r : rules.getValues(i))\r
63                                 toRules.add(i,r);\r
64                 }\r
65         }\r
66         \r
67         public static void copyHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
68                 to.setHints(from.getHints());\r
69                 createEmptyGenerationRules(to);\r
70                 copyGenerationRules(from, to);\r
71                 reconnectParent(from, to);\r
72         }\r
73         \r
74         public static void copyDefaultHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
75                 to.setHints(from.getHints());\r
76                 createEmptyGenerationRules(to);\r
77                 reconnectParent(from, to);\r
78         }\r
79         \r
80         private static void reconnectParent(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
81                 GraphNode<Identifiable> parent = from.getHint(MappingHints.KEY_PARENT_NODE);\r
82                 if (parent != null) {\r
83                         setParentNode(parent, to);\r
84                         \r
85                 }\r
86         }\r
87         \r
88 //      public static boolean hasModuleTypeAssigned(GraphNode<Identifiable> node) {\r
89 //              return  (node.containsHint(MappingHints.KEY_RESOLVER) || node.containsHint(MappingHints.KEY_GENERATE_TYPE) || node.containsHint(MappingHints.KEY_GENERATED_SYMBOL));\r
90 //      }\r
91         \r
92         public static <T> void disposeNodes(Collection<GraphNode<T>> nodes) {\r
93                 for (GraphNode<T> node : nodes) {\r
94                         node.destroy();\r
95                 }\r
96                 nodes.clear();\r
97         }\r
98         \r
99         public static void setParentNode(GraphNode<?> parent, GraphNode<?> child) {\r
100                 child.setHint(MappingHints.KEY_PARENT_NODE, parent);\r
101                 Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);\r
102                 if (children == null) {\r
103                         children = new ArrayList<GraphNode<?>>();\r
104                         parent.setHint(MappingHints.KEY_CHILD_NODE, children);\r
105                 }\r
106                 children.add(child);\r
107         }\r
108         \r
109         public static void unsetParentNode(GraphNode<?> child) {\r
110                 GraphNode<Identifiable> parent = child.getHint(MappingHints.KEY_PARENT_NODE);\r
111                 if (parent == null)\r
112                         return;\r
113                 \r
114                 Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);\r
115                 children.remove(child);\r
116                 child.removeHint(MappingHints.KEY_PARENT_NODE);\r
117         }\r
118 }\r