]> gerrit.simantics Code Review - simantics/interop.git/blobdiff - 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
diff --git a/org.simantics.interop.mapping/src/org/simantics/interop/mapping/MappingTools.java b/org.simantics.interop.mapping/src/org/simantics/interop/mapping/MappingTools.java
new file mode 100644 (file)
index 0000000..03ea947
--- /dev/null
@@ -0,0 +1,118 @@
+package org.simantics.interop.mapping;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.interop.mapping.data.GraphNode;\r
+import org.simantics.interop.mapping.data.Identifiable;\r
+import org.simantics.interop.mapping.data.Link;\r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+public class MappingTools {\r
+\r
+       public static Resource getSymbolType(ReadGraph g, GraphNode<Identifiable> node) throws DatabaseException {\r
+               Resource type = node.getHint(MappingHints.KEY_GENERATE_TYPE);\r
+               if (type == null) {\r
+                       ModuleResolver resolver = node.getHint(MappingHints.KEY_RESOLVER);\r
+                       if (resolver != null)\r
+                               type = resolver.getSymbolType(g);\r
+                       \r
+               }\r
+               return type;\r
+       }\r
+       \r
+       public static Resource getTerminal(ReadGraph g, Link<Identifiable> link) throws DatabaseException {\r
+               Resource terminal = link.getHint(MappingHints.KEY_CONNECTION_TERMINAL);\r
+               if (terminal == null) {\r
+                       ModuleResolver resolver = link.from().getHint(MappingHints.KEY_RESOLVER);\r
+                       if (resolver != null)\r
+                               terminal = resolver.getTerminal(g, link);\r
+                       \r
+               }\r
+               return terminal;\r
+       }\r
+       \r
+       public static void assignGenerationRule(GraphNode<Identifiable> node, int index, GenerationRule rule) {\r
+               MapList<Integer,GenerationRule> rules = node.getHint(MappingHints.KEY_GENERATION_RULES);\r
+               if (rules == null) {\r
+                       rules = new MapList<Integer,GenerationRule>();\r
+                       node.setHint(MappingHints.KEY_GENERATION_RULES, rules);\r
+               }\r
+               rules.add(index,rule);\r
+               \r
+       }\r
+       \r
+       public static void createEmptyGenerationRules(GraphNode<Identifiable> node) {\r
+               node.setHint(MappingHints.KEY_GENERATION_RULES, new MapList<Integer,GenerationRule>());\r
+       }\r
+       \r
+       public static void copyGenerationRules(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
+               MapList<Integer,GenerationRule> rules = from.getHint(MappingHints.KEY_GENERATION_RULES);\r
+               if (rules == null)\r
+                       return;\r
+               MapList<Integer,GenerationRule> toRules = to.getHint(MappingHints.KEY_GENERATION_RULES);\r
+               if (toRules == null) {\r
+                       toRules = new MapList<Integer,GenerationRule>();\r
+                       to.setHint(MappingHints.KEY_GENERATION_RULES, toRules);\r
+               }\r
+               for (Integer i : rules.getKeys()) {\r
+                       for (GenerationRule r : rules.getValues(i))\r
+                               toRules.add(i,r);\r
+               }\r
+       }\r
+       \r
+       public static void copyHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
+               to.setHints(from.getHints());\r
+               createEmptyGenerationRules(to);\r
+               copyGenerationRules(from, to);\r
+               reconnectParent(from, to);\r
+       }\r
+       \r
+       public static void copyDefaultHints(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
+               to.setHints(from.getHints());\r
+               createEmptyGenerationRules(to);\r
+               reconnectParent(from, to);\r
+       }\r
+       \r
+       private static void reconnectParent(GraphNode<Identifiable> from, GraphNode<Identifiable> to) {\r
+               GraphNode<Identifiable> parent = from.getHint(MappingHints.KEY_PARENT_NODE);\r
+               if (parent != null) {\r
+                       setParentNode(parent, to);\r
+                       \r
+               }\r
+       }\r
+       \r
+//     public static boolean hasModuleTypeAssigned(GraphNode<Identifiable> node) {\r
+//             return  (node.containsHint(MappingHints.KEY_RESOLVER) || node.containsHint(MappingHints.KEY_GENERATE_TYPE) || node.containsHint(MappingHints.KEY_GENERATED_SYMBOL));\r
+//     }\r
+       \r
+       public static <T> void disposeNodes(Collection<GraphNode<T>> nodes) {\r
+               for (GraphNode<T> node : nodes) {\r
+                       node.destroy();\r
+               }\r
+               nodes.clear();\r
+       }\r
+       \r
+       public static void setParentNode(GraphNode<?> parent, GraphNode<?> child) {\r
+               child.setHint(MappingHints.KEY_PARENT_NODE, parent);\r
+               Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);\r
+               if (children == null) {\r
+                       children = new ArrayList<GraphNode<?>>();\r
+                       parent.setHint(MappingHints.KEY_CHILD_NODE, children);\r
+               }\r
+               children.add(child);\r
+       }\r
+       \r
+       public static void unsetParentNode(GraphNode<?> child) {\r
+               GraphNode<Identifiable> parent = child.getHint(MappingHints.KEY_PARENT_NODE);\r
+               if (parent == null)\r
+                       return;\r
+               \r
+               Collection<GraphNode<?>> children = parent.getHint(MappingHints.KEY_CHILD_NODE);\r
+               children.remove(child);\r
+               child.removeHint(MappingHints.KEY_PARENT_NODE);\r
+       }\r
+}\r