more turn restriction stuff, and checkboxes, and a bunch of little fixes
[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         horizontalScrollPolicy="off"
7         borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3">
8
9         <mx:VBox width="100%">
10         <mx:Grid width="100%">
11                 <mx:GridRow width="100%">
12                         <mx:GridItem rowSpan="2">
13                                 <edit:ChoiceComboBox id="inputBox" dropdownFactory="mx.controls.TileList"
14                                         itemRenderer="net.systemeD.potlatch2.mapfeatures.editors.TurnRestrictionRenderer"
15                                                 change="setRestrictionType(inputBox.selectedItem.data)" 
16                                         click="event.stopPropagation()" 
17                                                 selectedItem="{findSelectedRestrictionType()}"
18                                             dropdownWidth="162" rowCount="2"
19                                             fontWeight="normal">
20                                         <mx:ArrayCollection id="restrictionTypes">
21                                                 <mx:Object label="No left turn" data='no_left_turn' />
22                                                 <mx:Object label="No right turn" data='no_right_turn' />
23                                                 <mx:Object label="No U turns" data='no_u_turn' />
24                                                 <mx:Object label="No straight on" data='no_straight_on' />
25                                                 <mx:Object label="Left turn only" data='only_left_turn' />
26                                                 <mx:Object label="Right turn only" data='only_right_turn' />
27                                                 <mx:Object label="Straight on only" data='only_straight_on' />
28                                         </mx:ArrayCollection>
29                                 </edit:ChoiceComboBox>
30                                 </mx:GridItem>
31                         <mx:GridItem>
32                         <mx:Text text="From" selectable="false"/>
33                         </mx:GridItem>
34                         <mx:GridItem width="100%" id="fromCt">
35                                                  <mx:ComboBox id="from" dataProvider="{connectingWays}" width="{fromCt.width}"
36                                                         selectedItem="{findSelected('from')}"
37                                                         change="setMember(from.selectedItem.data,'from')"
38                                                 click="event.stopPropagation()" /></mx:GridItem>
39                 </mx:GridRow>
40
41                 <mx:GridRow>
42                         <mx:GridItem><mx:Text text="To" selectable="false"/></mx:GridItem>
43                         <mx:GridItem width="100%" id="toCt">
44                                                  <mx:ComboBox id="to" dataProvider="{connectingWays}" width="{toCt.width}"
45                                                         selectedItem="{findSelected('to')}"
46                                                         change="setMember(to.selectedItem.data,'to')"
47                                                 click="event.stopPropagation()" /></mx:GridItem>
48                 </mx:GridRow>
49         </mx:Grid>
50
51         <mx:HBox>
52                 <mx:LinkButton id="show" label="Show" click="showFromTo()" paddingTop="0" paddingLeft="0" paddingBottom="0" paddingRight="0" />
53                 <mx:Text text="|" color='0xCCCCCC' />
54                 <mx:LinkButton label="Advanced..." click="openAdvanced()" paddingTop="0" paddingLeft="0" paddingBottom="0" paddingRight="0"  />
55                 <mx:Text text="|" color='0xCCCCCC' />
56                 <mx:LinkButton label="Delete" click="deleteRestriction()" paddingTop="0" paddingLeft="0" paddingBottom="0" paddingRight="0"  />
57         </mx:HBox>
58         </mx:VBox>
59
60     <mx:Script><![CDATA[
61     import net.systemeD.halcyon.connection.*;
62     import net.systemeD.halcyon.styleparser.RuleSet;
63     import net.systemeD.potlatch2.RelationEditorPanel;
64     import net.systemeD.potlatch2.mapfeatures.*;
65         import net.systemeD.halcyon.Map;
66         import net.systemeD.halcyon.Globals;
67     import flash.events.*;
68     import mx.collections.ArrayCollection;
69         import mx.utils.ObjectProxy;
70         import mx.core.Application;
71         import mx.managers.PopUpManager;
72
73     private var _turn:Object;
74         [Bindable(event="turn_inited")] private var connectingWays:ArrayCollection;
75     
76     public function setTurnRestriction(restriction:Object):void {   
77         _turn = restriction;
78
79                 var ways:Array=[];
80                 for each (var way:Way in Entity(_turn.entity).parentWays) {
81                         // ** FIXME - endsWith only works for nodes, and ways can be in the 'via' role too
82                         if (way.endsWith(Node(_turn.entity))) {
83                                 ways.push( { label:way.getDescription(), data:way } );
84                         }
85                 }
86                 connectingWays=new ArrayCollection(ways);
87
88                 dispatchEvent(new Event("turn_inited"));
89     }
90
91         private function findSelected(role:String):Object {
92                 var rel:Relation=Relation(_turn.relation);
93                 for each (var item:Object in connectingWays) {
94                         if (rel.hasMemberInRole(item.data,role)) { return item; }
95                 }
96                 return null;
97         }
98     
99         private function setMember(entity:Entity,role:String):void {
100                 var rel:Relation=Relation(_turn.relation);
101         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete refs");
102                 if (show.label=='Hide') { setHighlights(false); }
103                 
104                 // first, remove existing role from relation
105                 var old:Array=rel.findMembersByRole(role);
106                 for each (var memberEntity:Entity in old) {
107                         rel.removeMember(memberEntity, undo.push);
108                 }
109                 undo.doAction();
110
111                 // now add new entity
112                 // ** FIXME - appendMember should be undoable
113                 rel.appendMember(new RelationMember(entity,role))
114
115                 if (show.label=='Hide') { setHighlights(true); }
116         }
117         
118         private function setRestrictionType(type:String):void {
119                 var rel:Relation=Relation(_turn.relation);
120                 rel.setTag('restriction', type, MainUndoStack.getGlobalStack().addAction);
121         }
122         
123         private function findSelectedRestrictionType():Object {
124                 var type:String=Relation(_turn.relation).getTag('restriction');
125                 for each (var item:Object in restrictionTypes) {
126                         if (item.data==type) { return item; }
127                 }
128                 return null;
129         }
130
131     public function get turnRestriction():Object {
132         return _turn;
133     }
134
135         public function openAdvanced():void {
136                 var rel:Relation=Relation(_turn.relation);
137                 var panel:RelationEditorPanel = RelationEditorPanel(
138                         PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
139                 panel.setRelation(rel);
140                 PopUpManager.centerPopUp(panel);
141         }
142         
143         public function deleteRestriction():void {
144                 var rel:Relation=Relation(_turn.relation);
145                 rel.remove(MainUndoStack.getGlobalStack().addAction);
146         }
147
148         public function showFromTo():void {
149                 if (show.label=='Show') {
150                         show.label='Hide'; setHighlights(true);
151                 } else {
152                         show.label='Show'; setHighlights(false);
153                 }
154                 // ** FIXME - we should really remove the highlights when this icon panel is
155                 // closed. But since the removedFromStage event basically doesn't fire at the 
156                 // right point (thanks Flex), I can't yet see a way to do that.
157         }
158         
159         private function setHighlights(bool:Boolean):void {
160                 var map:Map = Globals.vars.root;
161                 var from:Object=findSelected('from'); if (from) { map.setHighlight(from.data, { restrictfrom: bool } ); }
162                 var to:Object  =findSelected('to'  ); if (to  ) { map.setHighlight(to.data  , { restrictto: bool   } ); }
163         }
164     
165     ]]></mx:Script>
166 </mx:HBox>
167