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