refactor EditController to make it stateful, and add addWayNodes functionality with...
[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               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           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
76           if ( feature != null )
77               setFeatureIcon(selectedEntity, feature);
78           else
79               blankFeatureIcon(selectedEntity);
80       }
81
82       private function setFeatureIcon(entity:Entity, feature:Feature):void {
83           blankFeatureIcon(entity);
84           
85           iconImage.source = feature.image;
86
87           var txt:String = feature.htmlDetails(entity);
88           iconText.htmlText = txt;
89           popupChange.label = feature.name;
90           tw.setSelectedFeature(feature);
91       }
92
93       private function blankFeatureIcon(entity:Entity):void {
94           iconImage.source = null;
95           iconText.htmlText = entity == null ?
96                "<i>Nothing selected</i>" :
97                "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
98           popupChange.label = "unknown";
99           tw.setSelectedFeature(null);
100       }
101
102       private function checkAdvanced():void {
103           if ( selectedEntity != null )
104              setupAdvanced(selectedEntity);
105       }
106
107       private function setupAdvanced(entity:Entity):void {
108           if ( collection == null ) {
109               collection = new ArrayCollection();
110               advancedTagGrid.dataProvider = collection;
111           }
112
113           collection.removeAll();
114           
115           if ( entity == null ) {
116               advancedID.htmlText = "";
117           } else {
118               var entityText:String = "xx";
119               if ( entity is Node ) entityText = "Node";
120               else if ( entity is Way ) entityText = "Way";
121               else if ( entity is Relation ) entityText = "Relation";
122               advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
123
124               var tags:Array = entity.getTagArray();
125               tags.sortOn("key");
126               for each(var tag:Tag in tags)
127                   collection.addItem(tag);
128           }
129       }
130
131       private function tagChanged(event:TagEvent):void {
132           refreshFeatureIcon();
133           
134           if ( collection != null ) {
135               // check to see if the key is already in our list
136               var exists:Boolean = false;
137               var tag:Tag = null;
138               var i:uint;
139               for ( i = 0; i < collection.length && !exists; i++ ) {
140                   tag = Tag(collection.getItemAt(i));
141                   exists = tag.key == event.key;
142               }
143               if ( !exists ) {
144                   tag = new Tag(selectedEntity, event.key, event.newValue);
145                   collection.addItem(tag);
146                   collection.refresh();
147               } else {
148                   if ( event.newValue == null ) {
149                       collection.removeItemAt(i-1);
150                       collection.refresh();
151                   }
152               }
153           }
154       }
155
156       public function loadFeatures():void {
157           mapFeatures = MapFeatures.getInstance();
158       }
159
160       public function openDescription():void {
161           trace("open description here");
162       }
163
164       public function addNewTag():void {
165           var newKey:String = "(new tag)";
166           var newTag:Tag = new Tag(selectedEntity, newKey, "(new value)");
167           collection.addItem(newTag);
168           advancedTagGrid.editedItemPosition = {rowIndex: collection.getItemIndex(newTag), columnIndex: 0};
169       }
170
171       public function removeTag():void {
172           var k:String = advancedTagGrid.selectedItem.key;
173           selectedEntity.setTag(k, null);
174       }
175       
176       public function initFeatureBox():void {
177           tw = new CategorySelector();
178           tw.addEventListener("selectedType", changeFeatureType);
179           popupChange.popUp = tw;
180       }
181       
182       public function changeFeatureType(event:Event):void {
183           if ( selectedEntity == null )
184               return;
185
186           var newFeature:Feature = tw.selectedType;
187           
188           // remove tags from the current feature
189           if ( feature != null ) {
190               for each( var oldtag:Object in feature.tags ) {
191                   selectedEntity.setTag(oldtag["k"], null);
192               }
193           }
194           
195           // set tags for new feature
196           if ( newFeature != null ) {
197               for each( var newtag:Object in newFeature.tags ) {
198                   selectedEntity.setTag(newtag["k"], newtag["v"]);
199               }
200           }
201           
202           popupChange.close();
203       }
204   ]]></mx:Script>
205 </mx:VBox>
206