]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DParentNode.java
Use unique names at the model scope instead of pipe run
[simantics/3d.git] / org.simantics.plant3d / src / org / simantics / plant3d / scenegraph / P3DParentNode.java
index dabae5ac46528c3ab445f8976fe003c4f7f83b80..c37d4627d4107f9a29ebadb11dfa3ea90820d072 100644 (file)
@@ -1,6 +1,7 @@
 package org.simantics.plant3d.scenegraph;
 
 import java.util.HashSet;
+import java.util.Objects;
 import java.util.Set;
 
 import javax.vecmath.Quat4d;
@@ -35,7 +36,7 @@ public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> im
        @RelatedSetValue(Layer0.URIs.HasName)
        @SetPropertyValue(Layer0.URIs.HasName)
        public void setName(String name) {
-               if (name == null)
+               if (name == null || name.equals(this.name))
                        return;
                this.name = name;
                firePropertyChanged(Layer0.URIs.HasName);
@@ -80,6 +81,9 @@ public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> im
        @SetPropertyValue(G3D.URIs.hasOrientation)
        public void setOrientation(Quat4d orientation) {
                assert(orientation != null);
+               if (Objects.equals(this.orientation, orientation))
+                       return;
+               
                this.orientation = orientation;
                
                firePropertyChanged(G3D.URIs.hasOrientation);
@@ -89,6 +93,9 @@ public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> im
        @SetPropertyValue(G3D.URIs.hasPosition)
        public void setPosition(Vector3d position) {
                assert(position != null);
+               if (Objects.equals(this.position, position))
+                       return;
+               
                this.position = position;
                
                firePropertyChanged(G3D.URIs.hasPosition);
@@ -167,26 +174,45 @@ public abstract class P3DParentNode<T extends IP3DNode> extends ParentNode<T> im
        }
        
        @Override
-       public Object getAdapter(Class adapter) {
-               if (IG3DNode.class == adapter)
-                       return this;
+       public <C> C getAdapter(Class<C> adapter) {
+               if (adapter.isInstance(this))
+                       return adapter.cast(this);
                return null;
        }
        
-       public String getUniqueName(String prefix) {
-               Set<String> names = new HashSet<String>();
+       /**
+        * Get a set of all of the names contained by this node and all its descendants
+        * @param names  A set to which the names are added
+        */
+       public void getComponentNames(Set<String> names) {
                for (IP3DNode node : getNodes()) {
                        if (!(node instanceof IP3DVisualNode))
                                continue;
                        IP3DVisualNode n = (IP3DVisualNode)node;
                        names.add(n.getName());
+                       if (node instanceof P3DParentNode)
+                               ((P3DParentNode<?>) node).getComponentNames(names);
                }
-               int i = 1;
-               while (true) {
-                       String genName = prefix + "_" + i;
-                       if (!names.contains(genName))
-                               return genName;
-                       i++;
+       }
+       
+       /**
+        * Get a unique name in the Plant3D model with a given prefix followed by
+        * an underscore and number.
+        */
+       public String getUniqueName(String prefix) {
+               ParentNode<?> root = getRootNode();
+               if (root instanceof P3DRootNode) {
+                       return ((P3DRootNode) root).getUniqueName(prefix);
+               } else {
+                       Set<String> names = new HashSet<String>();
+                       getComponentNames(names);
+                       int i = 1;
+                       while (true) {
+                               String genName = prefix + "_" + i;
+                               if (!names.contains(genName))
+                                       return genName;
+                               i++;
+                       }
                }
        }