]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.scenegraph/src/org/simantics/scenegraph/utils/NodeMapper.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.scenegraph / src / org / simantics / scenegraph / utils / NodeMapper.java
index 478d9177ba4668dce30b8353c9f8a51eed4ddf84..f59e25d996a60404a642bbc5ec7ab64f17ca54c9 100644 (file)
-package org.simantics.scenegraph.utils;\r
-\r
-import java.util.Collection;\r
-import java.util.HashMap;\r
-import java.util.Map;\r
-import java.util.UUID;\r
-\r
-import org.simantics.scenegraph.ILookupService;\r
-import org.simantics.scenegraph.INode;\r
-\r
-/**\r
- * A scene graph utility that manages a set of String ID <-> {@link INode}\r
- * mappings by ensuring that a specified set of nodes have ID mappings.\r
- * \r
- * When a node mapper is no longer needed, you should perform cleanup by\r
- * invoking {@link #clear()}. This will remove all the ID <-> INode mappings\r
- * managed by the mapper.\r
- * \r
- * The implementation is not thread-safe, it should only be used from the canvas\r
- * context thread (see {@link ICanvasContext#getThreadAccess()).\r
- * \r
- * @author Tuukka Lehtonen\r
- */\r
-public final class NodeMapper {\r
-\r
-    Map<INode, String> nodes        = new HashMap<INode, String>();\r
-    Map<INode, String> managedNodes = new HashMap<INode, String>();\r
-\r
-    public NodeMapper() {\r
-    }\r
-\r
-    public NodeMapper(INode node) {\r
-        add0(node);\r
-    }\r
-\r
-    public void clear() {\r
-        for (Map.Entry<INode, String> entry : managedNodes.entrySet()) {\r
-            ILookupService lu = NodeUtil.tryGetLookupService(entry.getKey());\r
-            if (lu != null) {\r
-                String mappedId = lu.lookupId(entry.getKey());\r
-                if (entry.getValue().equals(mappedId)) {\r
-                    lu.unmap(entry.getKey());\r
-                }\r
-            }\r
-        }\r
-        managedNodes.clear();\r
-        nodes.clear();\r
-    }\r
-\r
-    public NodeMapper(Collection<? extends INode> nodes) {\r
-        addAll(nodes);\r
-    }\r
-\r
-    public String add(INode node) {\r
-        return add0(node);\r
-    }\r
-\r
-    public void addAll(Collection<? extends INode> nodes) {\r
-        for (INode n : nodes) {\r
-            add0(n);\r
-        }\r
-    }\r
-\r
-    /**\r
-     * @param node\r
-     * @return\r
-     */\r
-    public String remove(INode node) {\r
-        return remove0(node);\r
-    }\r
-\r
-    public String getId(INode node) {\r
-        return nodes.get(node);\r
-    }\r
-\r
-    private String add0(INode node) {\r
-        String id = nodes.get(node);\r
-        if (id != null)\r
-            return id;\r
-\r
-        id = NodeUtil.lookupId(node);\r
-        if (id != null) {\r
-            nodes.put(node, id);\r
-            return id;\r
-        }\r
-\r
-        id = UUID.randomUUID().toString();\r
-        NodeUtil.map(node, id);\r
-        nodes.put(node, id);\r
-        managedNodes.put(node, id);\r
-        return id;\r
-    }\r
-\r
-    /**\r
-     * Removes mapping for specified node if and only if it is mapped through\r
-     * <em>this</em> mapper and the ID currently mapped to matches this mapper's\r
-     * conception of what the ID should be.\r
-     * \r
-     * @param node\r
-     * @return\r
-     */\r
-    private String remove0(INode node) {\r
-        String id = null;\r
-        if ((id = nodes.remove(node)) != null) {\r
-            if (managedNodes.remove(node) != null) {\r
-                ILookupService lu = NodeUtil.tryGetLookupService(node);\r
-                if (lu != null) {\r
-                    String mappedId = lu.lookupId(node);\r
-                    if (id.equals(mappedId)) {\r
-                        lu.unmap(node);\r
-                    }\r
-                }\r
-            }\r
-        }\r
-        return id;\r
-    }\r
-\r
-}\r
+package org.simantics.scenegraph.utils;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+import org.simantics.scenegraph.ILookupService;
+import org.simantics.scenegraph.INode;
+
+/**
+ * A scene graph utility that manages a set of String ID <-> {@link INode}
+ * mappings by ensuring that a specified set of nodes have ID mappings.
+ * 
+ * When a node mapper is no longer needed, you should perform cleanup by
+ * invoking {@link #clear()}. This will remove all the ID <-> INode mappings
+ * managed by the mapper.
+ * 
+ * The implementation is not thread-safe, it should only be used from the canvas
+ * context thread (see {@link ICanvasContext#getThreadAccess()).
+ * 
+ * @author Tuukka Lehtonen
+ */
+public final class NodeMapper {
+
+    Map<INode, String> nodes        = new HashMap<INode, String>();
+    Map<INode, String> managedNodes = new HashMap<INode, String>();
+
+    public NodeMapper() {
+    }
+
+    public NodeMapper(INode node) {
+        add0(node);
+    }
+
+    public void clear() {
+        for (Map.Entry<INode, String> entry : managedNodes.entrySet()) {
+            ILookupService lu = NodeUtil.tryGetLookupService(entry.getKey());
+            if (lu != null) {
+                String mappedId = lu.lookupId(entry.getKey());
+                if (entry.getValue().equals(mappedId)) {
+                    lu.unmap(entry.getKey());
+                }
+            }
+        }
+        managedNodes.clear();
+        nodes.clear();
+    }
+
+    public NodeMapper(Collection<? extends INode> nodes) {
+        addAll(nodes);
+    }
+
+    public String add(INode node) {
+        return add0(node);
+    }
+
+    public void addAll(Collection<? extends INode> nodes) {
+        for (INode n : nodes) {
+            add0(n);
+        }
+    }
+
+    /**
+     * @param node
+     * @return
+     */
+    public String remove(INode node) {
+        return remove0(node);
+    }
+
+    public String getId(INode node) {
+        return nodes.get(node);
+    }
+
+    private String add0(INode node) {
+        String id = nodes.get(node);
+        if (id != null)
+            return id;
+
+        id = NodeUtil.lookupId(node);
+        if (id != null) {
+            nodes.put(node, id);
+            return id;
+        }
+
+        id = UUID.randomUUID().toString();
+        NodeUtil.map(node, id);
+        nodes.put(node, id);
+        managedNodes.put(node, id);
+        return id;
+    }
+
+    /**
+     * Removes mapping for specified node if and only if it is mapped through
+     * <em>this</em> mapper and the ID currently mapped to matches this mapper's
+     * conception of what the ID should be.
+     * 
+     * @param node
+     * @return
+     */
+    private String remove0(INode node) {
+        String id = null;
+        if ((id = nodes.remove(node)) != null) {
+            if (managedNodes.remove(node) != null) {
+                ILookupService lu = NodeUtil.tryGetLookupService(node);
+                if (lu != null) {
+                    String mappedId = lu.lookupId(node);
+                    if (id.equals(mappedId)) {
+                        lu.unmap(node);
+                    }
+                }
+            }
+        }
+        return id;
+    }
+
+}