Merge branch 'master' of github.com:systemed/potlatch2
[potlatch2.git] / net / systemeD / halcyon / connection / actions / SetTagKeyAction.as
1 package net.systemeD.halcyon.connection.actions {
2
3     import net.systemeD.halcyon.connection.*;
4     
5     public class SetTagKeyAction extends UndoableEntityAction {
6         private var oldKey:String;
7         private var newKey:String;
8         private var oldValue:String;
9         
10         public function SetTagKeyAction(entity:Entity, oldKey:String, newKey:String) {
11             super(entity, "Rename tag "+oldKey+"->"+newKey);
12             this.oldKey = oldKey;
13             this.newKey = newKey;
14         }
15             
16         public override function doAction():uint {
17             var tags:Object = entity.getTagsHash();
18             oldValue = tags[oldKey];
19             var newValue:String;
20             if ( oldKey != newKey ) {
21                 delete tags[oldKey];
22                 if (newKey=='') {
23                     newValue = null;
24                 } else {
25                     tags[newKey] = oldValue;
26                     newValue = oldValue;
27                                 } 
28                 markDirty();
29                 entity.dispatchEvent(new TagEvent(Connection.TAG_CHANGED, entity, oldKey, newKey, oldValue, newValue));
30                 return SUCCESS;
31             } else {
32                 return NO_CHANGE;
33             }
34         }
35             
36         public override function undoAction():uint {
37             var tags:Object = entity.getTagsHash();
38             delete tags[newKey];
39             tags[oldKey] = oldValue;
40             markClean();
41             entity.dispatchEvent(new TagEvent(Connection.TAG_CHANGED, entity, newKey, oldKey, oldValue, oldValue));
42             
43             return SUCCESS;
44         }
45     }
46 }
47