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