Don't die if user undoes way creation while in DrawWay.
[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" id="fromLabel" 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" id="toLabel" 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.MapPaint;
67         import net.systemeD.halcyon.Globals;
68     import flash.events.*;
69     import mx.collections.ArrayCollection;
70         import mx.utils.ObjectProxy;
71         import mx.core.Application;
72         import mx.managers.PopUpManager;
73
74     private var _turn:Object;
75         [Bindable(event="turn_inited")] private var connectingWays:ArrayCollection;
76     
77     public function setTurnRestriction(restriction:Object):void {   
78         _turn = restriction;
79
80                 var ways:Array=[];
81                 for each (var way:Way in Entity(_turn.entity).parentWays) {
82                         // ** FIXME - endsWith only works for nodes, and ways can be in the 'via' role too
83                         if (way.endsWith(Node(_turn.entity))) {
84                                 ways.push( { label:way.getDescription(), data:way } );
85                         }
86                 }
87                 connectingWays=new ArrayCollection(ways);
88
89                 dispatchEvent(new Event("turn_inited"));
90     }
91
92         private function findSelected(role:String):Object {
93                 var rel:Relation=Relation(_turn.relation);
94                 for each (var item:Object in connectingWays) {
95                         if (rel.hasMemberInRole(item.data,role)) { return item; }
96                 }
97                 return null;
98         }
99     
100         private function setMember(entity:Entity,role:String):void {
101                 var rel:Relation=Relation(_turn.relation);
102         var undo:CompositeUndoableAction = new CompositeUndoableAction("Delete refs");
103                 if (show.label=='Hide') { setHighlights(false); }
104                 
105                 // first, remove existing role from relation
106                 var old:Array=rel.findMembersByRole(role);
107                 for each (var memberEntity:Entity in old) {
108                         rel.removeMember(memberEntity, undo.push);
109                 }
110
111                 // now add new entity
112                 rel.appendMember(new RelationMember(entity,role), undo.push);
113
114         undo.doAction();
115
116                 if (show.label=='Hide') { setHighlights(true); }
117         }
118         
119         private function setRestrictionType(type:String):void {
120                 var rel:Relation=Relation(_turn.relation);
121                 rel.setTag('restriction', type, MainUndoStack.getGlobalStack().addAction);
122         }
123         
124         private function findSelectedRestrictionType():Object {
125                 var type:String=Relation(_turn.relation).getTag('restriction');
126                 for each (var item:Object in restrictionTypes) {
127                         if (item.data==type) { return item; }
128                 }
129                 return null;
130         }
131
132     public function get turnRestriction():Object {
133         return _turn;
134     }
135
136         public function openAdvanced():void {
137                 var rel:Relation=Relation(_turn.relation);
138                 var panel:RelationEditorPanel = RelationEditorPanel(
139                         PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
140                 panel.setRelation(rel);
141                 PopUpManager.centerPopUp(panel);
142         }
143         
144         public function deleteRestriction():void {
145                 var rel:Relation=Relation(_turn.relation);
146                 rel.remove(MainUndoStack.getGlobalStack().addAction);
147         }
148
149         public function showFromTo():void {
150                 if (show.label=='Show') {
151                         show.label='Hide'; setHighlights(true);
152                         fromLabel.setStyle('color',0xFF0000);
153                         toLabel.setStyle('color',0x0000FF);
154                 } else {
155                         show.label='Show'; setHighlights(false);
156                         fromLabel.setStyle('color',0)
157                         toLabel.setStyle('color',0);
158                 }
159                 // ** FIXME - we should really remove the highlights when this icon panel is
160                 // closed. But since the removedFromStage event basically doesn't fire at the 
161                 // right point (thanks Flex), I can't yet see a way to do that.
162         }
163         
164         private function setHighlights(bool:Boolean):void {
165                 var paint:MapPaint = Map(Globals.vars.root).editableLayer;      // ** FIXME: should really be the mapPaint layer where the objects are located
166                 var from:Object=findSelected('from'); if (from) { paint.setHighlight(from.data, { restrictfrom: bool } ); }
167                 var to:Object  =findSelected('to'  ); if (to  ) { paint.setHighlight(to.data  , { restrictto: bool   } ); }
168         }
169     
170     ]]></mx:Script>
171 </mx:HBox>
172