Merge branch 'master' into history
[potlatch2.git] / net / systemeD / halcyon / connection / Relation.as
index 999643f..db6d435 100644 (file)
@@ -60,6 +60,14 @@ package net.systemeD.halcyon.connection {
             return members[index];
         }
 
+               public function getFirstMember():RelationMember {
+                       return members[0];
+               }
+
+               public function getLastMember():RelationMember {
+                       return members[members.length-1];
+               }
+
         public function setMember(index:uint, member:RelationMember, performAction:Function):void {
             var composite:CompositeUndoableAction = new CompositeUndoableAction("Set Member at index "+index);
             composite.push(new RemoveMemberByIndexAction(this, members, index));
@@ -92,11 +100,19 @@ package net.systemeD.halcyon.connection {
         }
 
                public function removeMember(entity:Entity, performAction:Function):void {
-                       performAction(new RemoveEntityFromRelationAction(this, entity, members));
+                       if (length>1) {
+                               performAction(new RemoveEntityFromRelationAction(this, entity, members));
+                       } else {
+                               performAction(new DeleteRelationAction(this, setDeletedState, members));
+                       }
                }
 
         public function removeMemberByIndex(index:uint, performAction:Function):void {
-            performAction(new RemoveMemberByIndexAction(this, members, index));
+                       if (length>1) {
+                               performAction(new RemoveMemberByIndexAction(this, members, index));
+                       } else {
+                               performAction(new DeleteRelationAction(this, setDeletedState, members));
+                       }
         }
 
                public override function remove(performAction:Function):void {
@@ -112,20 +128,47 @@ package net.systemeD.halcyon.connection {
                        return (deleted || (members.length==0));
                }
 
-        public override function getDescription():String {
-            var desc:String = "";
-            var relTags:Object = getTagsHash();
-            if ( relTags["type"] ) {
-                desc = relTags["type"];
-                if ( relTags[desc] )
-                    desc += " " + relTags[desc];
-            }
-            if ( relTags["ref"] )
-                desc += " " + relTags["ref"];
-            if ( relTags["name"] )
-                desc += " " + relTags["name"];
-            return desc;
-        }
+               public override function getDescription():String {
+                       var desc:String = "";
+                       var relTags:Object = getTagsHash();
+                       var named:Boolean = false;
+                       if ( relTags["type"] ) {
+                               // type=route                           --> "route"
+                               desc = relTags["type"];
+                               // type=route, route=bicycle--> "route bicycle"
+                               if (relTags[desc]) { desc += " " + relTags[desc]; }
+                       }
+                       // type=route, route=bicycle, network=ncn, ref=54 -> "route bicycle ncn 54"
+                       if ( relTags["network"]) { desc += " " + relTags["network"]; }
+                       if ( relTags["ref"]    ) { desc += " " + relTags["ref"];  named=true; }
+                       if ( relTags["name"]   ) { desc += " " + relTags["name"]; named=true; }
+                       // handle node->node routes
+                       if ( !named && relTags["type"] && relTags["type"]=="route" ) {
+                               var firstName:String=getSignificantName(getFirstMember().entity);
+                               var lastName:String=getSignificantName(getLastMember().entity);
+                               if ((firstName+lastName)!='') desc+=" "+firstName+"-"+lastName;
+                       }
+                       return desc;
+               }
+               
+               public function getRelationType():String {
+                       var relTags:Object = getTagsHash();
+                       return relTags["type"] ? relTags["type"] : getType();
+               }
+               
+               private function getSignificantName(entity:Entity):String {
+                       if (!entity.loaded || (entity is Relation)) return '';
+
+                       var t:String;
+                       if (entity is Way) {
+                               t=getSignificantName(Way(entity).getFirstNode());
+                               if (t=='') t=getSignificantName(Way(entity).getLastNode());
+                               return t;
+                       }
+                       t=Node(entity).getTag('name');
+                       if (!t) t=Node(entity).getTagByRegex(/ref$/);
+                       return t ? t : '';
+               }
 
                public override function getType():String {
                        return 'relation';