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