update route editors when entity joins a relation
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / RelationMemberEditor.as
1 package net.systemeD.potlatch2.mapfeatures.editors {
2
3     import net.systemeD.halcyon.connection.*;
4     import net.systemeD.potlatch2.mapfeatures.*;
5     import mx.containers.VBox;
6     import flash.events.*;
7
8         public class RelationMemberEditor extends VBox {
9
10       protected var _factory:RelationMemberEditorFactory;
11       protected var _entity:Entity;
12       
13       [Bindable(event="factory_set")]
14       public function get fieldName():String {
15           return _factory == null ? "" : _factory.name;
16       }
17       
18       [Bindable(event="factory_set")]
19       public function get fieldDescription():String {
20           return _factory == null ? "" : _factory.description;
21       }
22       
23       [Bindable(event="relations_changed")]
24       public function get matchedRelations():Array {
25           if (_entity == null)
26               return [];
27           
28           var relationTags:Object = _factory.relationTags;
29           var matched:Array = [];
30           for each(var relation:Relation in _entity.parentRelations) {
31               var addable:Boolean = true;
32               for ( var k:String in relationTags ) {
33                   var relVal:String = relation.getTag(k);
34                   if ( relVal != relationTags[k] )
35                       addable = false;
36               }
37               if (addable) {
38                   for each( var memberIndex:int in relation.findEntityMemberIndexes(_entity)) {
39                       var props:Object = {};
40                       props["relation"] = relation;
41                       props["id"] = relation.id;
42                       props["index"] = memberIndex;
43                       props["role"] = relation.getMember(memberIndex).role;
44                       props["entity"] = _entity;
45                       matched.push(props);
46                   }
47               }
48           }
49           return matched;
50       }
51       
52       public function addMember(relation:Relation, role:String):void {
53           if (_entity != null && !_entity.hasParent(relation))
54               relation.appendMember(new RelationMember(_entity, role));
55       }
56
57       public function set factory(factory:RelationMemberEditorFactory):void {
58           _factory = factory;
59           dispatchEvent(new Event("factory_set"));
60       }
61       
62       public function set entity(entity:Entity):void {
63           _entity = entity;
64           
65           entity.addEventListener(Connection.ADDED_TO_RELATION, relationsChanged);
66           entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationsChanged);
67           dispatchEvent(new Event("relations_changed"));
68       }
69       
70       protected function relationsChanged(event:Event):void {
71           dispatchEvent(new Event("relations_changed"));
72       }
73
74     }
75
76 }
77
78