1 package org.simantics.scenegraph.utils;
3 import java.util.Collection;
4 import java.util.HashMap;
8 import org.simantics.scenegraph.ILookupService;
9 import org.simantics.scenegraph.INode;
12 * A scene graph utility that manages a set of String ID <-> {@link INode}
13 * mappings by ensuring that a specified set of nodes have ID mappings.
15 * When a node mapper is no longer needed, you should perform cleanup by
16 * invoking {@link #clear()}. This will remove all the ID <-> INode mappings
17 * managed by the mapper.
19 * The implementation is not thread-safe, it should only be used from the canvas
20 * context thread (see {@link ICanvasContext#getThreadAccess()).
22 * @author Tuukka Lehtonen
24 public final class NodeMapper {
26 Map<INode, String> nodes = new HashMap<INode, String>();
27 Map<INode, String> managedNodes = new HashMap<INode, String>();
32 public NodeMapper(INode node) {
37 for (Map.Entry<INode, String> entry : managedNodes.entrySet()) {
38 ILookupService lu = NodeUtil.tryGetLookupService(entry.getKey());
40 String mappedId = lu.lookupId(entry.getKey());
41 if (entry.getValue().equals(mappedId)) {
42 lu.unmap(entry.getKey());
50 public NodeMapper(Collection<? extends INode> nodes) {
54 public String add(INode node) {
58 public void addAll(Collection<? extends INode> nodes) {
59 for (INode n : nodes) {
68 public String remove(INode node) {
72 public String getId(INode node) {
73 return nodes.get(node);
76 private String add0(INode node) {
77 String id = nodes.get(node);
81 id = NodeUtil.lookupId(node);
87 id = UUID.randomUUID().toString();
88 NodeUtil.map(node, id);
90 managedNodes.put(node, id);
95 * Removes mapping for specified node if and only if it is mapped through
96 * <em>this</em> mapper and the ID currently mapped to matches this mapper's
97 * conception of what the ID should be.
102 private String remove0(INode node) {
104 if ((id = nodes.remove(node)) != null) {
105 if (managedNodes.remove(node) != null) {
106 ILookupService lu = NodeUtil.tryGetLookupService(node);
108 String mappedId = lu.lookupId(node);
109 if (id.equals(mappedId)) {