Add to several relations at once. Closes #3485
[potlatch2.git] / net / systemeD / potlatch2 / RelationSelectPanel.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="Select Relation" width="350" height="400"
6     showCloseButton="true" close="PopUpManager.removePopUp(this);">
7
8     <mx:Script><![CDATA[
9         import net.systemeD.halcyon.*;
10         import net.systemeD.halcyon.connection.*;
11         import net.systemeD.potlatch2.*;
12         import mx.managers.PopUpManager;
13         import mx.core.Application;
14
15         private var conn:Connection;
16         private var relationList:Array;
17         private var entity:Entity;
18                 private var tags:Object;
19
20         public function init(e:Entity, t:Object):void {
21           entity = e;
22           tags = {}; for (var k:String in t) { tags[k]=t[k]; }  // deep copy tags
23           PopUpManager.addPopUp(this, Application(Application.application), true);
24           PopUpManager.centerPopUp(this);
25     
26           var titles:Array = [];
27           conn = Connection.getConnectionInstance();
28           relationList = conn.getMatchingRelationIDs(tags);
29           if (relationList.length == 0) {
30             warning.text = "No relations available";
31           } else {
32             warning.text = "These relations are available:";
33           }
34           //relationSelector.dataProvider = relationList;
35           for each (var id:Number in relationList) {
36             var relation:Relation = conn.getRelation(id);
37             titles.push({label:id + ": " + relation.getDescription(), relId:id});
38           }
39           relationSelector.dataProvider = titles;
40         }
41         
42         public function updateEntityAndClose():void {
43                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
44                         for each (var relObject:Object in relationSelector.selectedItems) {
45                                 var relation:Relation = conn.getRelation(relObject.relId);
46                                 if (entity is EntityCollection) {
47                                         for each (var e:Entity in EntityCollection(entity).entities) {
48                                                 if (relation.findEntityMemberIndex(e)==-1) {
49                                                         relation.appendMember(new RelationMember(e, ''), undo.push);
50                                                 }
51                                         }
52                                 } else {
53                                         relation.appendMember(new RelationMember(entity, ''), undo.push);
54                                 }
55                         }
56                         MainUndoStack.getGlobalStack().addAction(undo);
57                         PopUpManager.removePopUp(this);
58         }
59         
60         public function closeAndNewRelation():void {
61                         var members:Array=[];
62                         if (entity is EntityCollection) {
63                                 for each (var e:Entity in EntityCollection(entity).entities) {
64                                         members.push(new RelationMember(e, ''));
65                                 }
66                         } else members.push(new RelationMember(entity, ''));
67                         var relation:Relation = conn.createRelation(tags, members, MainUndoStack.getGlobalStack().addAction);
68
69                         PopUpManager.removePopUp(this);
70                         var panel:RelationEditorPanel = RelationEditorPanel(
71                             PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
72                         panel.setRelation(relation);
73                         PopUpManager.centerPopUp(panel);
74         }
75         
76     ]]></mx:Script> 
77     <mx:Text id="warning" text="" />
78     <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on" allowMultipleSelection="true" > 
79     </mx:List>
80     <mx:ControlBar>
81       <mx:Button label="New Relation..." click="closeAndNewRelation();" styleName="titleWindowButton" />
82       <mx:Spacer width="100%"/>
83       <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItems.length>0}" styleName="titleWindowButton" />
84     </mx:ControlBar>
85 </mx:TitleWindow>