Merge branch 'master' into snapshotserver
[potlatch2.git] / net / systemeD / potlatch2 / RelationLoaderPanel.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:TitleWindow
3         xmlns:mx="http://www.adobe.com/2006/mxml"
4         xmlns:potlatch2="net.systemeD.potlatch2.*"
5         title="Load Relation" width="350" height="160"
6         creationComplete="requestedID.setFocus()"
7         showCloseButton="true" close="PopUpManager.removePopUp(this);">
8         
9         <mx:HBox paddingLeft="3" paddingTop="4">
10                 <mx:Label text="Relation ID: " />
11                 <mx:TextInput id="requestedID" restrict="0-9" />
12         </mx:HBox>
13     <mx:ControlBar>
14       <mx:Spacer width="100%"/>
15       <mx:Button label="Load" click="loadRelation();" styleName="titleWindowButton" />
16     </mx:ControlBar>
17         <mx:Script><![CDATA[
18                 import net.systemeD.halcyon.*;
19                 import net.systemeD.halcyon.connection.*;
20                 import net.systemeD.potlatch2.*;
21                 import mx.managers.PopUpManager;
22
23                 private var entity:Entity;
24                 private var connection:Connection;
25                 private var relid:Number;
26                 
27                 public function setEntity(e:Entity):void {
28                         entity=e;
29                         connection=e.connection;
30                 }
31
32                 private function loadRelation():void {
33                         relid = Number(requestedID.text);
34                         PopUpManager.removePopUp(this);
35
36                         if (!relid) return;
37                         if (connection.getRelation(relid)) {
38                                 relationLoaded(null);
39                         } else {
40                                 connection.loadEntityByID("relation",relid);
41                                 connection.addEventListener(Connection.LOAD_COMPLETED, relationLoaded);
42                         }
43                 }
44                 
45                 private function relationLoaded(event:Event):void {
46                         var relation:Relation = connection.getRelation(relid);
47                         connection.removeEventListener(Connection.LOAD_COMPLETED, relationLoaded);
48                         if (!relation) return;
49
50                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
51                         if (entity is EntityCollection) {
52                                 for each (var e:Entity in EntityCollection(entity).entities) {
53                                         if (relation.findEntityMemberIndex(e)==-1) {
54                                                 relation.appendMember(new RelationMember(e, ''), undo.push);
55                                         }
56                                 }
57                         } else {
58                                 relation.appendMember(new RelationMember(entity, ''), undo.push);
59                         }
60                         MainUndoStack.getGlobalStack().addAction(undo);
61                 }
62                 
63     ]]></mx:Script>     
64 </mx:TitleWindow>
65