Complete support for loading relations by ID
[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 defaulttags:Object;
19
20         /** Initialise panel used to add and remove relations from an entity.
21         *   @param e Entity that is being edited.
22         *   @param t Object containing tag match patterns as properties, for the kind of relation sought.
23         */
24         public function init(e:Entity, t:Object):void {
25           entity = e;
26           defaulttags = {};
27           for (var k:String in t) {  
28             //tagmatches[k]=t[k];// deep copy match pattern tags
29             defaulttags[k]=t[k][0];  // route=[hiking,foot] -> route=hiking
30           }     
31           PopUpManager.addPopUp(this, Application(Application.application), true);
32           PopUpManager.centerPopUp(this);
33     
34           var titles:Array = [];
35           conn = Connection.getConnectionInstance();
36           relationList = conn.getMatchingRelationIDs(t);
37           if (relationList.length == 0) {
38             warning.text = "No relations available";
39           } else {
40             warning.text = "These relations are available:";
41           }
42           //relationSelector.dataProvider = relationList;
43           for each (var id:Number in relationList) {
44             var relation:Relation = conn.getRelation(id);
45             titles.push({label:id + ": " + relation.getDescription(), relId:id});
46           }
47           relationSelector.dataProvider = titles;
48         }
49         
50         public function updateEntityAndClose():void {
51                         var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
52                         for each (var relObject:Object in relationSelector.selectedItems) {
53                                 var relation:Relation = conn.getRelation(relObject.relId);
54                                 if (entity is EntityCollection) {
55                                         for each (var e:Entity in EntityCollection(entity).entities) {
56                                                 if (relation.findEntityMemberIndex(e)==-1) {
57                                                         relation.appendMember(new RelationMember(e, ''), undo.push);
58                                                 }
59                                         }
60                                 } else {
61                                         relation.appendMember(new RelationMember(entity, ''), undo.push);
62                                 }
63                         }
64                         MainUndoStack.getGlobalStack().addAction(undo);
65                         PopUpManager.removePopUp(this);
66         }
67         
68         public function closeAndNewRelation():void {
69                         var members:Array=[];
70                         if (entity is EntityCollection) {
71                                 for each (var e:Entity in EntityCollection(entity).entities) {
72                                         members.push(new RelationMember(e, ''));
73                                 }
74                         } else members.push(new RelationMember(entity, ''));
75                         var relation:Relation = conn.createRelation(defaulttags, members, MainUndoStack.getGlobalStack().addAction);
76
77                         PopUpManager.removePopUp(this);
78                         var panel:RelationEditorPanel = RelationEditorPanel(
79                             PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
80                         panel.setRelation(relation);
81                         PopUpManager.centerPopUp(panel);
82         }
83
84                 public function closeAndLoadRelation():void {
85                         PopUpManager.removePopUp(this);
86                         var panel:RelationLoaderPanel = RelationLoaderPanel(
87                             PopUpManager.createPopUp(Application(Application.application), RelationLoaderPanel, true));
88                         panel.setEntity(entity);
89                         PopUpManager.centerPopUp(panel);
90                 }
91         
92     ]]></mx:Script> 
93     <mx:Text id="warning" text="" />
94     <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on" allowMultipleSelection="true" > 
95     </mx:List>
96     <mx:ControlBar>
97       <mx:Button label="New Relation..." click="closeAndNewRelation();" styleName="titleWindowButton" />
98       <mx:Button label="Load Relation..." click="closeAndLoadRelation();" styleName="titleWindowButton" />
99       <mx:Spacer width="100%"/>
100       <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItems.length>0}" styleName="titleWindowButton" />
101     </mx:ControlBar>
102 </mx:TitleWindow>