Add support for relations to match multiple tags. Turned out to be a bit more involve...
[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 relationTagPatterns:Object = _factory.relationTagPatterns;
29           var matched:Array = [];
30           
31           for each(var relation:Relation in _entity.parentRelations) {
32               var addable:Boolean = true;
33               for ( var k:String in relationTagPatterns ) {
34                   var relVal:String = relation.getTag(k);
35                   if ( relationTagPatterns[k].indexOf(relVal) < 0 ) {
36                       addable = false;
37                       break;
38                   }
39               }
40               if (_factory.role && !relation.hasMemberInRole(_entity,_factory.role) ) { continue; }
41
42               if (addable) {
43                   for each( var memberIndex:int in relation.findEntityMemberIndexes(_entity)) {
44                       var props:Object = {};
45                       props["relation"] = relation;
46                       props["id"] = relation.id;
47                       props["index"] = memberIndex;
48                       props["role"] = relation.getMember(memberIndex).role;
49                       props["entity"] = _entity;
50                       matched.push(props);
51                   }
52               }
53           }
54           return matched;
55       }
56       
57       public function addMember(relation:Relation, role:String):void {
58           if (_entity != null && !_entity.hasParent(relation))
59               relation.appendMember(new RelationMember(_entity, role), MainUndoStack.getGlobalStack().addAction);
60       }
61
62       public function set factory(factory:RelationMemberEditorFactory):void {
63           _factory = factory;
64           dispatchEvent(new Event("factory_set"));
65       }
66       
67       public function set entity(entity:Entity):void {
68           _entity = entity;
69           
70           entity.addEventListener(Connection.ADDED_TO_RELATION, relationsChanged, false, 0, true);
71           entity.addEventListener(Connection.REMOVED_FROM_RELATION, relationsChanged, false, 0, true);
72           dispatchEvent(new Event("relations_changed"));
73       }
74       
75       protected function relationsChanged(event:Event):void {
76           dispatchEvent(new Event("relations_changed"));
77       }
78
79     }
80
81 }
82
83