fix some random bugs + do something a bit cooler with maxspeed
[potlatch2.git] / net / systemeD / potlatch2 / TagViewer.mxml
1 <?xml version="1.0" encoding="utf-8"?>
2 <mx:VBox
3         xmlns:mx="http://www.adobe.com/2006/mxml" 
4     backgroundColor="white"
5     creationComplete="loadFeatures()">
6
7   <mx:ViewStack id="stack" width="100%" height="100%">
8   <mx:VBox width="100%" height="100%" label="Simple">
9     <mx:HBox borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3" id="iconContainer">
10         <mx:Image id="iconImage"/>
11         <mx:VBox width="100%" verticalGap="1">
12           <mx:PopUpButton id="popupChange" creationComplete="initFeatureBox()" openAlways="true" width="100%"/>
13           <mx:Text condenseWhite="true" width="100%" id="iconText"/>
14         </mx:VBox>
15         <mx:LinkButton label="?" click="openDescription()" id="helpLabel"/>
16     </mx:HBox>
17     <mx:VBox width="100%" id="editorBox" paddingLeft="2" paddingRight="2"/>
18   </mx:VBox>
19
20   <mx:VBox width="100%" height="100%" label="Advanced" initialize="checkAdvanced()" verticalGap="1">
21     <mx:Label id="advancedID">
22       <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
23     </mx:Label>
24
25     <mx:DataGrid editable="true" width="100%" height="100%" id="advancedTagGrid">
26             <mx:columns>
27                 <mx:DataGridColumn editable="true" dataField="key" headerText="Key"/>
28                 <mx:DataGridColumn editable="true" dataField="value" headerText="Value"/>
29             </mx:columns>
30     </mx:DataGrid>
31
32     <mx:HBox horizontalAlign="right" width="100%">
33       <mx:LinkButton label="Delete" click="removeTag()"/>
34       <mx:LinkButton label="Add" click="addNewTag()"/>
35     </mx:HBox>
36   </mx:VBox>
37
38   </mx:ViewStack>
39
40   <mx:LinkBar dataProvider="{stack}"/>
41
42   <mx:Script><![CDATA[
43       import net.systemeD.halcyon.connection.*;
44       import net.systemeD.potlatch2.mapfeatures.*;
45
46       import mx.collections.*;
47       import mx.containers.*;
48       import mx.events.*;
49       import mx.core.*;
50       import mx.managers.PopUpManager;
51       import flash.geom.Point;
52       
53       private var mapFeatures:MapFeatures;
54       private var selectedEntity:Entity;
55       private var collection:ArrayCollection;
56       private var tw:CategorySelector = null;
57       private var feature:Feature = null;
58
59       public function setEntity(entity:Entity):void {
60           if ( selectedEntity != entity ) {
61               if ( selectedEntity != null )
62                   selectedEntity.removeEventListener(Connection.TAG_CHANGE, tagChanged);
63               selectedEntity = entity;
64               if ( selectedEntity != null )
65                   selectedEntity.addEventListener(Connection.TAG_CHANGE, tagChanged);
66           }
67
68           if ( advancedID != null )
69               setupAdvanced(entity);
70
71           refreshFeatureIcon();
72       }
73
74       private function refreshFeatureIcon():void {
75           var oldFeature:Feature = feature;
76           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
77           if ( feature != oldFeature )
78               initialiseEditors();
79
80           if ( feature != null )
81               setFeatureIcon(selectedEntity, feature);
82           else
83               blankFeatureIcon(selectedEntity);
84       }
85
86       private function setFeatureIcon(entity:Entity, feature:Feature):void {
87           //blankFeatureIcon(entity);
88           
89           iconImage.source = feature.image;
90
91           var txt:String = feature.htmlDetails(entity);
92           iconText.htmlText = txt;
93           popupChange.label = feature.name;
94           tw.setSelectedFeature(feature);
95       }
96
97       private function blankFeatureIcon(entity:Entity):void {
98           iconImage.source = null;
99           iconText.htmlText = entity == null ?
100                "<i>Nothing selected</i>" :
101                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
102           popupChange.label = "unknown";
103           tw.setSelectedFeature(null);
104       }
105
106       private function initialiseEditors():void {
107           editorBox.removeAllChildren();
108           if ( selectedEntity == null || feature == null )
109               return;
110               
111           for each (var factory:EditorFactory in feature.editors) {
112               var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
113               if ( editor != null )
114                  editorBox.addChild(editor);
115           }
116       }
117       
118       private function checkAdvanced():void {
119           if ( selectedEntity != null )
120              setupAdvanced(selectedEntity);
121       }
122
123       private function setupAdvanced(entity:Entity):void {
124           if ( collection == null ) {
125               collection = new ArrayCollection();
126               advancedTagGrid.dataProvider = collection;
127           }
128
129           collection.removeAll();
130           
131           if ( entity == null ) {
132               advancedID.htmlText = "";
133           } else {
134               var entityText:String = "xx";
135               if ( entity is Node ) entityText = "Node";
136               else if ( entity is Way ) entityText = "Way";
137               else if ( entity is Relation ) entityText = "Relation";
138               advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
139
140               var tags:Array = entity.getTagArray();
141               tags.sortOn("key");
142               for each(var tag:Tag in tags)
143                   collection.addItem(tag);
144           }
145       }
146
147       private function tagChanged(event:TagEvent):void {
148           refreshFeatureIcon();
149           
150           if ( collection != null ) {
151               // check to see if the key is already in our list
152               var exists:Boolean = false;
153               var tag:Tag = null;
154               var i:uint;
155               for ( i = 0; i < collection.length && !exists; i++ ) {
156                   tag = Tag(collection.getItemAt(i));
157                   exists = tag.key == event.key;
158               }
159               if ( !exists ) {
160                   tag = new Tag(selectedEntity, event.key, event.newValue);
161                   collection.addItem(tag);
162                   collection.refresh();
163               } else {
164                   if ( event.newValue == null ) {
165                       collection.removeItemAt(i-1);
166                       collection.refresh();
167                   } else {
168                       collection.itemUpdated(tag, "value");
169                   }
170               }
171           }
172       }
173
174       public function loadFeatures():void {
175           mapFeatures = MapFeatures.getInstance();
176       }
177
178       public function openDescription():void {
179           trace("open description here");
180       }
181
182       public function addNewTag():void {
183           var newKey:String = "(new tag)";
184           var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
185           collection.addItem(newTag);
186           advancedTagGrid.editedItemPosition = {rowIndex: collection.getItemIndex(newTag), columnIndex: 0};
187       }
188
189       public function removeTag():void {
190           var k:String = advancedTagGrid.selectedItem.key;
191           selectedEntity.setTag(k, null);
192       }
193       
194       public function initFeatureBox():void {
195           tw = new CategorySelector();
196           tw.addEventListener("selectedType", changeFeatureType);
197           popupChange.popUp = tw;
198       }
199       
200       public function changeFeatureType(event:Event):void {
201           if ( selectedEntity == null )
202               return;
203
204           var newFeature:Feature = tw.selectedType;
205           
206           // remove tags from the current feature
207           if ( feature != null ) {
208               for each( var oldtag:Object in feature.tags ) {
209                   selectedEntity.setTag(oldtag["k"], null);
210               }
211           }
212           
213           // set tags for new feature
214           if ( newFeature != null ) {
215               for each( var newtag:Object in newFeature.tags ) {
216                   selectedEntity.setTag(newtag["k"], newtag["v"]);
217               }
218           }
219           
220           popupChange.close();
221       }
222   ]]></mx:Script>
223 </mx:VBox>
224