relation editing for multiple selections
[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 relation:Relation = conn.getRelation(relationSelector.selectedItem.relId);
44                         if (entity is EntityCollection) {
45                                 var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
46                                 for each (var e:Entity in EntityCollection(entity).entities) {
47                                         if (relation.findEntityMemberIndex(e)==-1) {
48                                                 relation.appendMember(new RelationMember(e, ''), undo.push);
49                                         }
50                                 }
51                                 MainUndoStack.getGlobalStack().addAction(undo);
52                         } else {
53                                 relation.appendMember(new RelationMember(entity, ''), MainUndoStack.getGlobalStack().addAction);
54                         }
55                         PopUpManager.removePopUp(this);
56         }
57         
58         public function closeAndNewRelation():void {
59                         var members:Array=[];
60                         if (entity is EntityCollection) {
61                                 for each (var e:Entity in EntityCollection(entity).entities) {
62                                         members.push(new RelationMember(e, ''));
63                                 }
64                         } else members.push(new RelationMember(entity, ''));
65                         var relation:Relation = conn.createRelation(tags, members, MainUndoStack.getGlobalStack().addAction);
66
67                         PopUpManager.removePopUp(this);
68                         var panel:RelationEditorPanel = RelationEditorPanel(
69                             PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
70                         panel.setRelation(relation);
71                         PopUpManager.centerPopUp(panel);
72         }
73         
74     ]]></mx:Script> 
75     <mx:Text id="warning" text="" />
76     <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on"> 
77     </mx:List>
78     <mx:ControlBar>
79       <mx:Button label="New Relation..." click="closeAndNewRelation();" styleName="titleWindowButton" />
80       <mx:Spacer width="100%"/>
81       <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItem != null? true : false}" styleName="titleWindowButton" />
82     </mx:ControlBar>
83 </mx:TitleWindow>