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