how do I hate thee, Flex? let me count the ways
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / TurnRestrictionIcon.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:HBox
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4         xmlns:edit="net.systemeD.potlatch2.mapfeatures.editors.*"
5         xmlns:flexlib="flexlib.controls.*"
6         borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3">
7         
8         <mx:Grid width="100%">
9                 <mx:GridRow>
10                         <mx:GridItem rowSpan="2">
11                                 <edit:ChoiceComboBox id="inputBox" dropdownFactory="mx.controls.TileList"
12                                         itemRenderer="net.systemeD.potlatch2.mapfeatures.editors.TurnRestrictionRenderer"
13                                                 change="setRestrictionType(inputBox.selectedItem.data)" 
14                                         click="event.stopPropagation()" 
15                                                 selectedItem="{findSelectedRestrictionType()}"
16                                             dropdownWidth="162" rowCount="2"
17                                             fontWeight="normal">
18                                         <mx:ArrayCollection id="restrictionTypes">
19                                                 <mx:Object label="No left turn" data='no_left_turn' />
20                                                 <mx:Object label="No right turn" data='no_right_turn' />
21                                                 <mx:Object label="No U turns" data='no_u_turn' />
22                                                 <mx:Object label="No straight on" data='no_straight_on' />
23                                                 <mx:Object label="Left turn only" data='only_left_turn' />
24                                                 <mx:Object label="Right turn only" data='only_right_turn' />
25                                                 <mx:Object label="Straight on only" data='only_straight_on' />
26                                         </mx:ArrayCollection>
27                                 </edit:ChoiceComboBox>
28                                 </mx:GridItem>
29                         <mx:GridItem><mx:Text text="From" selectable="false"/></mx:GridItem>
30                         <mx:GridItem><mx:ComboBox id="from" dataProvider="{connectingWays}"
31                                                         selectedItem="{findSelected('from')}"
32                                                         change="setMember(from.selectedItem.data,'from')"
33                                                 click="event.stopPropagation()" /></mx:GridItem>
34                 </mx:GridRow>
35                 <mx:GridRow>
36                         <mx:GridItem><mx:Text text="To" selectable="false"/></mx:GridItem>
37                         <mx:GridItem><mx:ComboBox id="to" dataProvider="{connectingWays}"
38                                                         selectedItem="{findSelected('to')}"
39                                                         change="setMember(to.selectedItem.data,'to')"
40                                                 click="event.stopPropagation()" /></mx:GridItem>
41                 </mx:GridRow>
42         </mx:Grid>
43
44     <mx:Script><![CDATA[
45     import net.systemeD.halcyon.connection.*;
46     import net.systemeD.halcyon.styleparser.RuleSet;
47     import net.systemeD.potlatch2.mapfeatures.*;
48     import flash.events.*;
49     import mx.collections.ArrayCollection;
50         import mx.utils.ObjectProxy;
51
52     private var _turn:Object;
53         [Bindable(event="turn_inited")] private var connectingWays:ArrayCollection;
54     
55     public function setTurnRestriction(restriction:Object):void {   
56         _turn = restriction;
57
58                 var ways:Array=[];
59                 for each (var way:Way in Entity(_turn.entity).parentWays) {
60                         ways.push( { label:way.getDescription(), data:way } );
61                 }
62                 connectingWays=new ArrayCollection(ways);
63
64                 dispatchEvent(new Event("turn_inited"));
65     }
66
67         private function findSelected(role:String):Object {
68                 trace("calling findSelected for "+role);
69                 var rel:Relation=Relation(_turn.relation);
70                 for each (var item:Object in connectingWays) {
71                         if (rel.hasMemberInRole(item.data,role)) { return item; }
72                 }
73                 return null;
74         }
75     
76         private function setMember(entity:Entity,role:String):void {
77                 trace("setMember "+role+" to "+entity);
78                 var rel:Relation=Relation(_turn.relation);
79         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete refs");
80
81                 // first, remove existing role from relation
82                 var old:Array=rel.findMembersByRole(role);
83                 for each (var memberEntity:Entity in old) {
84                         rel.removeMember(memberEntity, undo.push);
85                 }
86                 undo.doAction();
87
88                 // now add new entity
89                 // ** FIXME - appendMember should be undoable
90                 rel.appendMember(new RelationMember(entity,role))
91         }
92         
93         private function setRestrictionType(type:String):void {
94                 var rel:Relation=Relation(_turn.relation);
95                 rel.setTag('restriction', type, MainUndoStack.getGlobalStack().addAction);
96                 trace ("set restriction type to "+type);
97         }
98         
99         private function findSelectedRestrictionType():Object {
100                 var type:String=Relation(_turn.relation).getTag('restriction');
101                 for each (var item:Object in restrictionTypes) {
102                         if (item.data==type) { return item; }
103                 }
104                 return null;
105         }
106
107     public function get turnRestriction():Object {
108         return _turn;
109     }
110     
111     ]]></mx:Script>
112 </mx:HBox>
113