Complete support for loading relations by ID
[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 relid:Number;
25                 
26                 public function setEntity(e:Entity):void {
27                         entity=e;
28                 }
29
30                 private function loadRelation():void {
31                         relid = Number(requestedID.text);
32                         PopUpManager.removePopUp(this);
33
34                         var conn:Connection = Connection.getConnectionInstance();
35                         if (!relid) return;
36                         if (conn.getRelation(relid)) {
37                                 relationLoaded(null);
38                         } else {
39                                 conn.loadEntityByID("relation",relid);
40                                 conn.addEventListener(Connection.LOAD_COMPLETED, relationLoaded);
41                         }
42                 }
43                 
44                 private function relationLoaded(event:Event):void {
45                         var conn:Connection = Connection.getConnectionInstance();
46                         var relation:Relation = conn.getRelation(relid);
47                         conn.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