split potlatch2 into entirely different namespace
[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         xmlns:halcyon="net.systemeD.halcyon.*"
5     backgroundColor="white"
6     creationComplete="loadFeatures()">
7
8   <mx:ViewStack id="stack" width="100%" height="100%">
9   <mx:VBox width="100%" height="100%" label="Simple">
10     <mx:HBox borderStyle="inset" verticalAlign="middle" width="100%" paddingLeft="3" id="iconContainer">
11         <mx:Image id="iconImage"/>
12         <mx:VBox width="100%" verticalGap="1">
13           <mx:PopUpButton id="popupChange" creationComplete="initFeatureBox()" openAlways="true" width="100%"/>
14           <mx:Text condenseWhite="true" width="100%" id="iconText"/>
15         </mx:VBox>
16         <mx:LinkButton label="?" click="openDescription()" id="helpLabel"/>
17     </mx:HBox>
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               selectedEntity.addEventListener(Connection.TAG_CHANGE, tagChanged);
65           }
66
67           if ( advancedID != null )
68               setupAdvanced(entity);
69
70           refreshFeatureIcon();
71       }
72
73       private function refreshFeatureIcon():void {
74           feature = mapFeatures.findMatchingFeature(selectedEntity);
75           if ( feature != null )
76               setFeatureIcon(selectedEntity, feature);
77           else
78               blankFeatureIcon(selectedEntity);
79       }
80
81       private function setFeatureIcon(entity:Entity, feature:Feature):void {
82           blankFeatureIcon(entity);
83           
84           iconImage.source = feature.image;
85
86           var txt:String = feature.htmlDetails(entity);
87           iconText.htmlText = txt;
88           popupChange.label = feature.name;
89           tw.setSelectedFeature(feature);
90       }
91
92       private function blankFeatureIcon(entity:Entity):void {
93           iconImage.source = null;
94           iconText.htmlText = entity == null ?
95                "<i>Nothing selected</i>" :
96                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
97           popupChange.label = "unknown";
98           tw.setSelectedFeature(null);
99       }
100
101       private function checkAdvanced():void {
102           if ( selectedEntity != null )
103              setupAdvanced(selectedEntity);
104       }
105
106       private function setupAdvanced(entity:Entity):void {
107           var entityText:String = "xx";
108           if ( entity is Node ) entityText = "Node";
109           else if ( entity is Way ) entityText = "Way";
110           else if ( entity is Relation ) entityText = "Relation";
111
112           advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
113
114           if ( collection == null ) {
115               collection = new ArrayCollection();
116               //var sort:Sort = new Sort();
117               //sort.fields = [new SortField("key", true)];
118               //collection.sort = sort;
119               //collection.refresh();
120               advancedTagGrid.dataProvider = collection;
121           }
122           collection.removeAll();
123           var tags:Array = entity.getTagArray();
124           tags.sortOn("key");
125           for each(var tag:Tag in tags)
126               collection.addItem(tag);
127       }
128
129       private function tagChanged(event:TagEvent):void {
130           refreshFeatureIcon();
131           
132           if ( collection != null ) {
133               // check to see if the key is already in our list
134               var exists:Boolean = false;
135               var tag:Tag = null;
136               var i:uint;
137               for ( i = 0; i < collection.length && !exists; i++ ) {
138                   tag = Tag(collection.getItemAt(i));
139                   exists = tag.key == event.key;
140               }
141               if ( !exists ) {
142                   tag = new Tag(selectedEntity, event.key, event.newValue);
143                   collection.addItem(tag);
144                   collection.refresh();
145               } else {
146                   if ( event.newValue == null ) {
147                       collection.removeItemAt(i-1);
148                       collection.refresh();
149                   }
150               }
151           }
152       }
153
154       public function loadFeatures():void {
155           mapFeatures = MapFeatures.getInstance();
156       }
157
158       public function openDescription():void {
159           trace("open description here");
160       }
161
162       public function addNewTag():void {
163           var newKey:String = "(new tag)";
164           var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
165           collection.addItem(newTag);
166           advancedTagGrid.editedItemPosition = {rowIndex: collection.getItemIndex(newTag), columnIndex: 0};
167       }
168
169       public function removeTag():void {
170           var k:String = advancedTagGrid.selectedItem.key;
171           selectedEntity.setTag(k, null);
172       }
173       
174       public function initFeatureBox():void {
175           tw = new CategorySelector();
176           tw.addEventListener("selectedType", changeFeatureType);
177           popupChange.popUp = tw;
178       }
179       
180       public function changeFeatureType(event:Event):void {
181           if ( selectedEntity == null )
182               return;
183
184           var newFeature:Feature = tw.selectedType;
185           
186           // remove tags from the current feature
187           if ( feature != null ) {
188               for each( var oldtag:Object in feature.tags ) {
189                   selectedEntity.setTag(oldtag["k"], null);
190               }
191           }
192           
193           // set tags for new feature
194           if ( newFeature != null ) {
195               for each( var newtag:Object in newFeature.tags ) {
196                   selectedEntity.setTag(newtag["k"], newtag["v"]);
197               }
198           }
199           
200           popupChange.close();
201       }
202   ]]></mx:Script>
203 </mx:VBox>
204