start of turn restriction editor
[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         /* ** FIXME as of Wed:
53                   - 'from' and 'to' don't get populated with original values
54                   - scrollbar ugh
55                   - "warning: unable to bind to property 'data' on class 'Object' (class is not an IEventDispatcher)"
56                   - (plus general node redraw bug when you change something)
57         */
58
59     private var _turn:Object;
60     
61     public function setTurnRestriction(restriction:Object):void {   
62         _turn = restriction;
63     }
64     
65         private function findSelected(role:String):Object {
66                 trace("calling findSelected for "+role);
67                 var rel:Relation=Relation(_turn.relation);
68                 for each (var item:Object in connectingWays) {
69                         if (rel.hasMemberInRole(item.data,role)) { return item; }
70                 }
71                 return null;
72         }
73
74         private function get connectingWays():ArrayCollection {
75                 trace("calling connectingWays");
76                 var ways:Array=[];
77                 for each (var way:Way in Entity(_turn.entity).parentWays) {
78                         ways.push( { label:way.getDescription(), data:way } );
79                 }
80                 return new ArrayCollection(ways);
81         }
82     
83         private function setMember(entity:Entity,role:String):void {
84                 trace("setMember "+role+" to "+entity);
85                 var rel:Relation=Relation(_turn.relation);
86         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete refs");
87
88                 // first, remove existing role from relation
89                 var old:Array=rel.findMembersByRole(role);
90                 for each (var memberEntity:Entity in old) {
91                         rel.removeMember(memberEntity, undo.push);
92                 }
93                 undo.doAction();
94
95                 // now add new entity
96                 // ** FIXME - appendMember should be undoable
97                 rel.appendMember(new RelationMember(entity,role))
98         }
99         
100         private function setRestrictionType(type:String):void {
101                 var rel:Relation=Relation(_turn.relation);
102                 rel.setTag('restriction', type, MainUndoStack.getGlobalStack().addAction);
103                 trace ("set restriction type to "+type);
104         }
105         
106         private function findSelectedRestrictionType():Object {
107                 var type:String=Relation(_turn.relation).getTag('restriction');
108                 for each (var item:Object in restrictionTypes) {
109                         if (item.data==type) { return item; }
110                 }
111                 return null;
112         }
113
114     public function get turnRestriction():Object {
115         return _turn;
116     }
117     
118     ]]></mx:Script>
119 </mx:HBox>
120