add undo for creating stuff
[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
19         public function init(e:Entity):void {
20           entity = e;
21           PopUpManager.addPopUp(this, Application(Application.application), true);
22           PopUpManager.centerPopUp(this);
23     
24           var titles:Array = [];
25           conn = Connection.getConnectionInstance();
26           relationList = conn.getAllRelationIDs();
27           if (relationList.length == 0) {
28             warning.text = "No relations loaded";
29           } else {
30             warning.text = "These relations are available:";
31           }
32           //relationSelector.dataProvider = relationList;
33           for each (var id:Number in relationList) {
34             var relation:Relation = conn.getRelation(id);
35             titles.push({label:id + ": " + relation.getDescription(), relId:id});
36           }
37           relationSelector.dataProvider = titles;
38         }
39         
40         public function updateEntityAndClose():void {
41           var relation:Relation = conn.getRelation(relationSelector.selectedItem.relId);
42           relation.appendMember(new RelationMember(entity, ''));
43           PopUpManager.removePopUp(this);
44         }
45         
46         public function closeAndNewRelation():void {
47           var relation:Relation = conn.createRelation({}, [new RelationMember(entity, '')],
48               MainUndoStack.getGlobalStack().addAction)
49           PopUpManager.removePopUp(this);
50           trace("edit relation "+id);
51           var panel:RelationEditorPanel = RelationEditorPanel(
52               PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
53           panel.setRelation(relation);
54           PopUpManager.centerPopUp(panel);
55         }
56         
57     ]]></mx:Script> 
58     <mx:Text id="warning" text="" />
59     <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on"> 
60     </mx:List>
61     <mx:ControlBar>
62       <mx:Button label="New Relation..." click="closeAndNewRelation();"/>
63       <mx:Spacer width="100%"/>
64       <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItem != null? true : false}"/>
65     </mx:ControlBar>
66 </mx:TitleWindow>