start of turn restriction editor
[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 (_factory.role && !relation.hasMemberInRole(_entity,_factory.role) ) { addable=false; }
38
39               if (addable) {
40                   for each( var memberIndex:int in relation.findEntityMemberIndexes(_entity)) {
41                       var props:Object = {};
42                       props["relation"] = relation;
43                       props["id"] = relation.id;
44                       props["index"] = memberIndex;
45                       props["role"] = relation.getMember(memberIndex).role;
46                       props["entity"] = _entity;
47                       matched.push(props);
48                   }
49               }
50           }
51           return matched;
52       }
53       
54       public function addMember(relation:Relation, role:String):void {
55           if (_entity != null && !_entity.hasParent(relation))
56               relation.appendMember(new RelationMember(_entity, role));
57       }
58
59       public function set factory(factory:RelationMemberEditorFactory):void {
60           _factory = factory;
61           dispatchEvent(new Event("factory_set"));
62       }
63       
64       public function set entity(entity:Entity):void {
65           _entity = entity;
66           
67           entity.addEventListener(Connection.ADDED_TO_RELATION, relationsChanged);
68           entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationsChanged);
69           dispatchEvent(new Event("relations_changed"));
70       }
71       
72       protected function relationsChanged(event:Event):void {
73           dispatchEvent(new Event("relations_changed"));
74       }
75
76     }
77
78 }
79
80