128d276de611263065ccdaf9d2b42735833abb0c
[potlatch2.git] / net / systemeD / halcyon / connection / EntityCollection.as
1 package net.systemeD.halcyon.connection {
2
3         import flash.events.TimerEvent;
4         import flash.utils.Timer;
5     
6         import net.systemeD.halcyon.connection.*;
7     import net.systemeD.halcyon.connection.actions.*;
8
9     public class EntityCollection extends Entity {
10                 private var entities:Array;
11                 private var tagChangedTimer:Timer;
12                 private var delayedEvent:TagEvent;
13
14         public function EntityCollection(entities:Array) {
15                         super(-1, 0, {}, true, -1, "");
16             this.entities = entities;
17                         
18                         //To avoid firing on every contained entity, we wait some short time before firing the tag changed event
19                         tagChangedTimer = new Timer(50, 1);
20                         tagChangedTimer.addEventListener(TimerEvent.TIMER, onTagChangedTimerFinished);                  
21                         for each(var entity:Entity in entities) {
22                                 entity.addEventListener(Connection.TAG_CHANGED, onTagChanged);  
23                         }
24         }
25                 
26                 private function onTagChanged(event:TagEvent):void {
27                         if(tagChangedTimer.running) return;
28                         delayedEvent = new TagEvent(Connection.TAG_CHANGED, this, event.key, event.key, event.oldValue, event.newValue)
29                         tagChangedTimer.start();
30                 }
31                 
32                 private function onTagChangedTimerFinished(event:TimerEvent): void {
33                         dispatchEvent(delayedEvent);
34                 }
35                 
36                 
37                 // Tag-handling methods
38                 
39                 private function getMergedTags():Object {
40                         //Builds an object with tags of all entities in this collection. If the value of a tag differs or is not set in all entities, value is marked
41                         var differentMarker:String = "<different>";
42                         var mergedTags:Object = entities[0].getTagsCopy();
43                         for each(var entity:Entity in entities) {
44                                 var entityTags:Object = entity.getTagsHash();
45                                 for(var key:String in entityTags) {
46                                         var value:String = entityTags[key];
47                                         if(mergedTags[key] == null || mergedTags[key] != value) {
48                                                 mergedTags[key] = differentMarker;
49                                         }
50                                 }
51                                 for(var mergedKey:String in mergedTags) {
52                                         var mergedValue:String = mergedTags[mergedKey];
53                                         if(entityTags[mergedKey] == null || entityTags[mergedKey] != mergedValue) {
54                                                 mergedTags[mergedKey] = differentMarker;
55                                         }
56                                 }
57                         }
58                         return mergedTags;
59                 }
60
61         public override function hasTags():Boolean {
62                         for (var key:String in getMergedTags())
63                 return true;
64             return false;
65         }
66
67         public override function hasInterestingTags():Boolean {
68                         for (var key:String in getMergedTags()) {
69               if (key != "attribution" && key != "created_by" && key != "source" && key.indexOf('tiger:') != 0) {
70                 return true;
71               }
72             }
73             return false;
74         }
75
76         public override function isUneditedTiger():Boolean {
77             return false;
78         }
79
80         public override function getTag(key:String):String {
81                         return getMergedTags()[key];
82         }
83
84                 public override function tagIs(key:String,value:String):Boolean {
85                         if (!getMergedTags[key]) { return false; }
86                         return getMergedTags[key]==value;
87                 }
88                 
89                 public override function setTag(key:String, value:String, performAction:Function):void {
90                         var oldValue:String = getMergedTags()[key];     
91                         var undoAction:CompositeUndoableAction = new CompositeUndoableAction("set_tag_entity_collection");
92                         for each (var entity:Entity in entities) {
93                                 undoAction.push(new SetTagAction(entity, key, value));
94                         }
95             performAction(undoAction);
96         }
97
98         public override function renameTag(oldKey:String, newKey:String, performAction:Function):void {
99                         var undoAction:CompositeUndoableAction = new CompositeUndoableAction("rename_tag_entity_collection");
100                         for each (var entity:Entity in entities) {
101                                 undoAction.push(new SetTagKeyAction(entity, oldKey, newKey));
102                         }
103             performAction(undoAction);
104         }
105
106         public override function getTagList():TagList {
107             return new TagList(getMergedTags());
108         }
109
110         public override function getTagsCopy():Object {
111                         return getMergedTags();
112         }
113
114                 public override function getTagsHash():Object {
115                         return getMergedTags();
116                 }
117
118         public override function getTagArray():Array {
119             var copy:Array = [];
120                         var mergedTags:Object = getMergedTags();
121             for (var key:String in mergedTags) {
122                 copy.push(new Tag(this, key, mergedTags[key]));
123                         }
124             return copy;
125         }
126
127                 // Clean/dirty methods
128
129         public override function get isDirty():Boolean {
130             for each (var entity:Entity in entities) {
131                                 if(entity.isDirty) return true;
132                         }
133                         return false;
134         }
135
136         public override function markClean():void {
137              for each (var entity:Entity in entities) {
138                                 entity.markClean();
139                         }
140         }
141
142         internal override function markDirty():void {
143             for each (var entity:Entity in entities) {
144                                 entity.markDirty();
145                         }
146         }
147         
148         
149                 // Others
150         
151                 public override function getDescription():String {
152                         var basic:String=this.getType();
153                         var mergedTags:Object = getMergedTags();
154                         if (mergedTags['ref'] && mergedTags['name']) { return mergedTags['ref']+' '+mergedTags['name']+' ('+basic+')'; }
155                         if (mergedTags['ref']) { return mergedTags['ref']+' ('+basic+')'; }
156                         if (mergedTags['name']) { return mergedTags['name']+' ('+basic+')'; }
157                         return basic;
158                 }
159
160         public override function getType():String {
161                         var entityType:String = '';
162                          for each (var entity:Entity in entities) {
163                                 if(entityType == '') entityType = entity.getType();
164                                 else if(entityType != entity.getType()) {
165                                         entityType = '';
166                                         break;
167                                 }
168                         }
169             return entityType;
170         }
171
172     }
173
174 }
175