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