Merge pull request #56 from tomhughes/master
[potlatch2.git] / net / systemeD / potlatch2 / mapfeatures / editors / RouteIcon.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         borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3"
8         backgroundColor="{bg}" color="{fg}" backgroundAlpha="1.0">
9         
10         <mx:Image source="{iconImage}"/>
11     <mx:Text condenseWhite="true" width="100%" htmlText="{iconHTML}" selectable="false"/>
12     <mx:ComboBox id="dir" color="black" dataProvider="{routeDirs}" selectedItem="{findSelected(role)}"
13         click="event.stopPropagation()"
14         change="setRole(dir.selectedItem.data)" />
15         <mx:Image source="@Embed('../../../../../embedded/delete_small.svg')" click='event.stopPropagation();removeFromRelation()'
16         buttonMode="true" useHandCursor="true" />
17     
18     <fx:Script><![CDATA[
19     import net.systemeD.halcyon.connection.*;
20     import net.systemeD.halcyon.styleparser.RuleSet;
21     import net.systemeD.potlatch2.mapfeatures.*;
22     import net.systemeD.halcyon.FileBank;
23     import flash.events.*;
24
25     import mx.collections.ArrayCollection;
26
27     [Bindable]
28     public var routeDirs:ArrayCollection = new ArrayCollection(
29         [ {label:">>", data:"forward"}, 
30           {label:"<<", data:"backward"}, 
31           {label:"<>", data:""} ]);
32
33     private var _iconImageName:String = null;
34     private var _iconImageData:ByteArray = null;
35     private var _iconHTML:String = "";
36     private var _bg:String = "white";
37     private var _fg:String = "black";
38     private var _route:Object;
39     
40     private function findSelected(role:String):Object {
41         for each ( var item:Object in routeDirs ) {
42             if ( item.data == role )
43                 return item;
44         }
45         return routeDirs[2];
46     }
47          
48     public function setRoute(route:Object, icon:XMLList):void {   
49         _route = route;
50         var relation:Relation = route["relation"];
51         _iconHTML = Feature.makeHTMLIcon(icon, relation);
52
53         if ( icon.length() > 0 ) {
54             if (icon[0].hasOwnProperty("@image"))
55                 _iconImageName = icon[0].@image;
56             if (icon[0].hasOwnProperty("@foreground"))
57                 _fg = icon[0].@foreground;
58             if (icon[0].hasOwnProperty("@background"))
59                 _bg = icon[0].@background;
60         }    
61
62         dispatchEvent(new Event("route_changed"));
63     }
64     
65     [Bindable(event="route_changed")]
66     private function get iconImage():ByteArray {
67         if (_iconImageName && !_iconImageData) {
68             FileBank.getInstance().addFromFile(_iconImageName, iconImageLoaded);
69         }
70         return _iconImageData;
71     }
72
73     private function iconImageLoaded(fileBank:FileBank, name:String):void {
74         _iconImageData = fileBank.getAsByteArray(name);
75         dispatchEvent(new Event("route_changed"));
76     }
77
78     [Bindable(event="route_changed")]
79     private function get iconHTML():String {
80         return _iconHTML;
81     }
82
83     [Bindable(event="route_changed")]
84     private function get role():String {
85         return _route["role"];
86     }
87
88     [Bindable(event="route_changed")]
89     private function get fg():uint {
90         return RuleSet.parseCSSColor(_fg);
91     }
92
93     [Bindable(event="route_changed")]
94     private function get bg():uint {
95         return RuleSet.parseCSSColor(_bg);
96     }
97     
98     public function get route():Object {
99         return _route;
100     }
101     
102     private function setRole(role:String):void {
103         var member:RelationMember = new RelationMember(_route["entity"], role);
104         Relation(_route["relation"]).setMember(_route["index"], member, MainUndoStack.getGlobalStack().addAction);
105     }
106
107         private function removeFromRelation():void {
108                 Relation(_route["relation"]).removeMember(_route["entity"], MainUndoStack.getGlobalStack().addAction);
109         }
110     
111     ]]></fx:Script>
112 </mx:HBox>
113