support GPX 1.1 too
[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:flexlib="flexlib.containers.*"
5         xmlns:controls="net.systemeD.controls.*"
6         xmlns:potlatch2="net.systemeD.potlatch2.*"
7     xmlns:sidepanel="net.systemeD.potlatch2.panels.*"
8         horizontalScrollPolicy="off"
9     backgroundColor="white"
10     initialize="loadFeatures()">
11
12 <mx:ViewStack id="sidebar" width="100%" height="100%" creationPolicy="all">
13   <mx:VBox id="dndPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
14     <mx:Text id="dndPanelText" text="{dndPrompt}" width="100%" styleName="helpInfo" />
15         <mx:Repeater id="dndRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType('point')}">
16                 <mx:HBox width="100%"><mx:Label text="{dndRep.currentItem.name}:" styleName="dndPanelCategoryLabel"/></mx:HBox>
17
18                 <mx:TileList dataProvider="{dndRep.currentItem.getFeaturesForType('point')}" width="100%" height="1"
19                                      rowHeight="32" columnWidth="32" updateComplete="resizePOIGrid(event)" styleName="dndPanelTileList">
20                         <mx:itemRenderer>
21                                 <mx:Component>
22                                         <mx:VBox toolTip="{data.name}">
23                                                 <mx:Script><![CDATA[
24                                                         import mx.events.DragEvent;
25                                                         import mx.managers.DragManager;
26                                                         import mx.core.DragSource;
27
28                                                         private function dragPOI(event:MouseEvent, tags:Array):void {
29                                                                 // Get the drag initiator component from the event object.
30                                                                 var dragInitiator:Image = event.currentTarget as Image;
31                                                                 var dragSource:DragSource = new DragSource();
32                                                                 dragSource.addData(tags, 'tags');
33                                                                 dragSource.addData(event.target.parent.parent.parent.parent, 'container');
34
35                                                                 var dragProxy:Image = new Image();
36                                                                 dragProxy.source = dragInitiator.source;
37                                                                 dragProxy.width = dragInitiator.width;   // must set width and height explicitly
38                                                                 dragProxy.height = dragInitiator.height; // for non-embedded images
39                                                                 DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
40                                                         }
41                                                 ]]></mx:Script>
42                                                 <mx:Image id="foo" source="{data.image}" height="24" width="24" mouseMove="dragPOI(event, data.tags)" toolTip="{data.name}" />
43                                         </mx:VBox>
44                                 </mx:Component>
45                         </mx:itemRenderer>
46                 </mx:TileList>
47         </mx:Repeater>
48   </mx:VBox>
49   
50   <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto">
51     <mx:ViewStack id="stack" width="100%" height="100%">
52       <mx:VBox width="100%" height="100%" label="Simple" id="editorContainer" creationComplete="initEditorStackUIs()" styleName="dndPanelVbox">
53         <mx:VBox width="100%" verticalGap="1" styleName="dndTagHeader">
54           <mx:HBox width="100%" id="iconContainer" styleName="featureSelector">
55             <mx:Image id="iconImage"/>
56             <mx:Text condenseWhite="true" width="100%" id="iconText" styleName="dndIconText"/>
57             <mx:LinkButton label="?" click="openDescription()" id="helpLabel" styleName="helpInfo"/>
58           </mx:HBox>
59           <mx:PopUpButton id="popupChange" creationComplete="initFeatureBox()" openAlways="true" width="100%" styleName="dndTagPopUpMenu"/>
60         </mx:VBox>
61       </mx:VBox>
62       <mx:VBox width="100%" height="100%" label="Advanced" id="advancedContainer" initialize="checkAdvanced()" verticalGap="1">
63         <mx:Label id="advancedID" click="openEntityPage()">
64           <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
65         </mx:Label>
66
67                 <potlatch2:TagGrid id="advancedTagGrid" width="100%" height="75%" />
68
69         <mx:HBox horizontalAlign="right" width="100%">
70           <mx:LinkButton label="Delete" click="advancedTagGrid.removeTag()" enabled="{advancedTagGrid.selectedItem != null? true : false}"/>
71           <mx:LinkButton label="Add" click="advancedTagGrid.addNewTag()" id="advancedAddButton"/>
72         </mx:HBox>
73         
74         <mx:DataGrid editable="true" width="100%" height="25%" id="relationsGrid"
75             doubleClickEnabled="true"
76             itemDoubleClick="editRelation(relationsGrid.selectedItem.id)"
77             doubleClick="if (event.target.parent==relationsGrid) { addToRelation(); }">
78             <mx:columns>
79                 <mx:DataGridColumn editable="false" dataField="description" headerText="Relation"/>
80                 <mx:DataGridColumn editable="false" dataField="id_idx" headerText="ID"/>
81                 <mx:DataGridColumn editable="true" dataField="role" headerText="Role"/>
82             </mx:columns>
83         </mx:DataGrid>
84
85         <mx:HBox horizontalAlign="right" width="100%">
86           <mx:LinkButton label="Remove from" click="removeFromRelation(relationsGrid.selectedItem.id, relationsGrid.selectedItem.index)" 
87                           enabled="{relationsGrid.selectedItem != null? true : false}"/>
88           <mx:LinkButton label="Add to" click="addToRelation()"/>
89         </mx:HBox>
90
91       </mx:VBox>
92
93       <mx:VBox width="100%" height="100%" label="Members" id="membersVBox" initialize="checkMembers()" verticalGap="1">
94         <mx:Label id="membersText" text="Relation Members"/>
95         <mx:DataGrid editable="true" width="100%" height="100%" id="membersGrid"
96                       dragEnabled="true" dragMoveEnabled="true" dropEnabled="true">
97           <mx:columns>
98             <mx:DataGridColumn editable="false" dataField="type" headerText="Type"/>
99             <mx:DataGridColumn editable="false" dataField="id" headerText="ID"/>
100             <mx:DataGridColumn editable="true" dataField="role" headerText="Role"/>
101           </mx:columns>
102         </mx:DataGrid>
103       </mx:VBox>
104       
105       </mx:ViewStack>
106
107           <mx:HBox width="100%">
108                 <mx:LinkBar dataProvider="{stack}"/>
109                 <mx:Spacer width="100%"/>
110                 <mx:LinkButton 
111                         icon="@Embed('../../../embedded/view_tabbed.png')"
112                         disabledIcon="@Embed('../../../embedded/view_tabbed_disabled.png')"
113                         click="setEditorStackUI(true)" id="tabNavigatorLabel" paddingTop="6" 
114                         toolTip="Show in tabs"
115                         enabled="{editorStack is Accordion &amp;&amp; stack.selectedIndex==0}" />
116                 <mx:LinkButton
117                         icon="@Embed('../../../embedded/view_accordion.png')" 
118                         disabledIcon="@Embed('../../../embedded/view_accordion_disabled.png')"
119                         click="setEditorStackUI(false)" id="accordionLabel" paddingTop="6"
120                         toolTip="Show in sliding windows"
121                         enabled="{editorStack is SuperTabNavigator &amp;&amp; stack.selectedIndex==0}" />
122           </mx:HBox>
123   </mx:VBox>
124
125   <mx:VBox id="multiplePanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
126     <mx:Text id="multiplePanelText" text="You have selected multiple items." width="100%" styleName="helpInfo" />
127   </mx:VBox>
128
129   <mx:VBox id="markerPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
130     <sidepanel:MarkerPanel id="markerPanelContents" width="100%"/>
131   </mx:VBox>
132
133   <mx:VBox id="bugPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
134     <sidepanel:BugPanel id="bugPanelContents" width="100%"/>
135   </mx:VBox>
136
137 </mx:ViewStack>
138
139   <mx:Script><![CDATA[
140       import net.systemeD.halcyon.connection.*;
141       import net.systemeD.halcyon.VectorLayer;
142       import net.systemeD.potlatch2.mapfeatures.*;
143
144       import mx.collections.*;
145       import mx.containers.*;
146       import mx.events.*;
147       import mx.core.*;
148       import mx.managers.PopUpManager;
149       import flash.geom.Point;
150       import flash.net.*;
151       import mx.events.DragEvent;
152       import mx.managers.DragManager;
153       import mx.core.DragSource;
154       import flexlib.containers.SuperTabNavigator;
155
156       private static const dndPrompt:String="Add new points by dragging them onto the map";
157
158       private var editorStackTabNavigator:SuperTabNavigator;
159       private var editorStackAccordion:Accordion;
160       [Bindable] private var editorStack:Container;
161
162       public var mapFeatures:MapFeatures;
163       private var selectedEntity:Entity;
164       private var tw:CategorySelector = null;
165       private var feature:Feature = null;
166
167           public function setEntity(entities:Array, layer:VectorLayer=null):void {
168                 UIComponent.suspendBackgroundProcessing();
169                 
170                 var firstSelected:Entity=null;
171                 if (entities.length==1) { firstSelected=entities[0]; }
172                 
173                 if (selectedEntity!=firstSelected && selectedEntity!=null) {
174                         selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
175                 }
176                 
177                 if (entities.length==0) {
178                         // Nothing selected, so show drag-and-drop panel
179                         sidebar.selectedChild = dndPanel;
180                 
181                 } else if (entities.length==1) {
182                         // Single entity selected, so show tag panel
183                         selectedEntity=firstSelected;
184                         if (selectedEntity!=null) { selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged); }
185                         if (advancedID!=null) { setupAdvanced(firstSelected); }
186                         if (firstSelected is Relation) { stack.addChild(membersVBox); }
187             if (selectedEntity is Marker && layer is BugLayer) {
188               bugPanelContents.init(selectedEntity, layer);
189               sidebar.selectedChild = bugPanel;
190             } else if (selectedEntity is Marker) {
191               markerPanelContents.init(selectedEntity, layer);
192               sidebar.selectedChild = markerPanel;
193             } else {
194               refreshFeatureIcon();
195               initialiseEditors();
196               sidebar.selectedChild = tagsPanel;
197             }
198                 
199                 } else {
200                         // Multiple selection, so for now show a largely blank panel/
201                         // ** TODO - proper multiple selection tagging
202                         sidebar.selectedChild = multiplePanel;
203                 }
204                 UIComponent.resumeBackgroundProcessing();
205       }
206
207       private function refreshFeatureIcon():void {
208           var oldFeature:Feature = feature;
209           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
210           if ( feature != oldFeature ) {
211               if ( oldFeature != null )
212                   oldFeature.removeEventListener("imageChanged", featureImageChanged);
213               if ( feature != null )
214                   feature.addEventListener("imageChanged", featureImageChanged);
215           }
216
217           if ( feature != null && feature.name != null ) {
218               setFeatureIcon(selectedEntity, feature);
219           } else {
220               blankFeatureIcon(selectedEntity);
221           }
222       }
223
224       private function featureImageChanged(event:Event):void {
225           setFeatureIcon(selectedEntity, feature);
226       }
227       
228       private function setFeatureIcon(entity:Entity, feature:Feature):void {
229           //blankFeatureIcon(entity);
230           
231           iconImage.source = feature.image;
232
233           var txt:String = feature.htmlDetails(entity);
234           iconText.htmlText = txt;
235           popupChange.label = feature.name;
236           setLimitTypes(entity);
237           tw.setSelectedFeature(feature);
238           helpLabel.visible = feature.hasHelpURL();
239       }
240       
241       private function setLimitTypes(entity:Entity):void {
242           var type:String = null;
243           if ( entity is Node )
244               type = "point";
245           else if ( entity is Way )
246               type = Way(entity).isArea() ? "area" : "line";
247           else if ( entity is Relation )
248               type = "relation";
249           tw.setLimitTypes(type);
250       }
251
252       private function blankFeatureIcon(entity:Entity):void {
253           iconImage.source = null;
254           popupChange.label = "unknown";
255           setLimitTypes(entity);
256                   if (entity == null) { 
257                         iconText.htmlText = "<i>Nothing selected</i>";
258             tw.setNoSelectedFeature();
259                   } else if (entity.hasTags()) {
260                         iconText.htmlText = "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
261             tw.setSelectedFeature(null);
262                   } else {
263                         iconText.htmlText = "<b>No tags set</b><br/>Please use the menu below to define what this "+entity.getType()+" is";
264             tw.setSelectedFeature(null);
265           }
266           helpLabel.visible = false;
267       }
268
269       private var tabComponents:Object = {};
270       
271       private function initialiseEditors():void {
272           editorStack.removeAllChildren();
273           if ( selectedEntity == null || feature == null )
274               return;
275           
276           var editorBox:VBox = createEditorBox();
277           editorBox.label = "Basic";
278           editorStack.addChild(editorBox);
279           
280           var tabs:Object = {};
281           tabComponents = {};
282           
283           for each (var factory:EditorFactory in feature.editors) {
284               if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
285                   var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
286                   if ( editor != null )
287                       editorBox.addChild(editor);
288               }
289               var category:String = factory.category;
290               if (category!='') {
291                       var tab:VBox = tabs[category];
292                       if ( tab == null) {
293                           tab = createEditorBox();
294                           tab.label = category;
295                           editorStack.addChild(tab);
296                           tabs[category] = tab;
297                           tabComponents[tab] = [];
298                       }
299                       var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
300                       if ( catEditor != null )
301                           tabComponents[tab].push(catEditor);
302                       //    tab.addChild(catEditor);
303               }
304           }
305       }
306       
307       private function createEditorBox():VBox {
308           var box:VBox = new VBox();
309           box.percentWidth = 100;
310           box.percentHeight = 100;
311           box.styleName = "dndEditorContainer";
312           return box;
313       }
314
315       private function ensureEditorsPopulated(tab:VBox):void {
316           var components:Array = tabComponents[tab];
317           if ( components == null || tab == null || tab.numChildren >= components.length )
318               return;
319           for each (var component:DisplayObject in components ) {
320               tab.addChild(component);
321           }
322       }
323       
324         private function initEditorStackUIs():void {
325                 editorStackTabNavigator = new SuperTabNavigator();
326                 editorStackTabNavigator.allowTabSqueezing=false;
327                 editorStackTabNavigator.minTabWidth=10;
328                 editorStackTabNavigator.closePolicy="close_never";
329                 editorStackTabNavigator.scrollSpeed=20;
330                 editorStackTabNavigator.creationPolicy="auto";
331                 editorStackTabNavigator.percentWidth=100;
332                 editorStackTabNavigator.percentHeight=100;
333                 editorStackTabNavigator.styleName="dndStackTab";
334                 editorStackTabNavigator.popUpButtonPolicy="off"
335
336
337                 editorStackAccordion = new Accordion();
338                 editorStackAccordion.percentWidth=100;
339                 editorStackAccordion.percentHeight=100;
340                 editorStackAccordion.creationPolicy="auto";
341                 editorStackAccordion.styleName="dndStackAccordion";
342
343                 setEditorStackUI(true);
344         }
345
346         private function setEditorStackUI(isTabbed:Boolean):void {
347                 var children:Array=[]; var i:uint;
348
349                 if (editorStack) {
350                         // blank existing component
351                         editorStack.removeEventListener("change",editorStackUIChange);
352                         editorStack.removeEventListener("updateComplete",editorStackUIUpdate);
353                         editorStack.removeAllChildren();
354                         editorContainer.removeChildAt(1);
355                 }
356
357                 // replace with new component
358                 editorStack = (isTabbed ? editorStackTabNavigator : editorStackAccordion) as Container;
359                 editorContainer.addChild(editorStack);
360
361                 // re-add children and listeners
362                 initialiseEditors();
363                 editorStack.addEventListener("change",editorStackUIChange);
364                 editorStack.addEventListener("updateComplete",editorStackUIUpdate);
365         }
366         
367         private function editorStackUIChange(event:Event):void {
368                 ensureEditorsPopulated(IndexChangedEvent(event).relatedObject as VBox);
369         }
370
371         private function editorStackUIUpdate(event:Event):void {
372                 if (editorStack is SuperTabNavigator) {
373                         var e:SuperTabNavigator = editorStack as SuperTabNavigator;
374                         if (e.selectedIndex<0) { return; }
375                         for (var i:uint=0; i<e.numChildren; i++) {
376                                 e.getTabAt(i).selected=(i==e.selectedIndex);
377                         }
378                 }
379         }
380
381
382         public function togglePanel():void {
383                 if (stack.selectedChild==editorContainer && selectedEntity!=null) {
384                         stack.selectedChild=advancedContainer;
385                 } else if (stack.selectedChild!=editorContainer) {
386                         stack.selectedChild=editorContainer;
387                 }
388         }
389
390       private function checkAdvanced():void {
391           if ( selectedEntity != null )
392              setupAdvanced(selectedEntity);
393       }
394
395       /**
396       * When you enter a new tag and press enter, the focus moves to the add button
397       * Adding an event listener to the button and listening for + means that you
398       * can add two consecutive tags with the same keypress. This relies on focus
399       * moving to the "add" button, though, so it's not robust to rearranging the UI.
400       */
401       private function advancedAddButtonKeyboardEvent(event:KeyboardEvent):void {
402           switch (event.keyCode) {
403             case 187:   addNewTag(); break;                          // + - add tag
404           }
405       }
406
407       private var listeningToRelations:Array = [];
408       
409       private function setupAdvanced(entity:Entity):void {
410                 if (!advancedTagGrid) advancedContainer.createComponentsFromDescriptors();      // if Flex hasn't created it, force it
411                 advancedTagGrid.init(entity);
412         advancedAddButton.addEventListener(KeyboardEvent.KEY_DOWN, advancedAddButtonKeyboardEvent);
413
414                 if ( entity == null ) {
415                         advancedID.htmlText = "";
416                 } else {
417                         var entityText:String = "xx";
418                         if      ( entity is Node     ) entityText = "Node";
419                         else if ( entity is Way      ) entityText = "Way";
420                         else if ( entity is Relation ) entityText = "Relation";
421                         advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
422                 }
423
424                 removeRelationListeners();
425                 if ( selectedEntity != null ) {
426                         selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
427                         selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
428                 }
429
430                 if ( entity == null ) {
431                         relationsGrid.dataProvider = null;
432                 } else {
433                         resetRelationsGrid(entity);
434                         entity.addEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
435                         entity.addEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
436                 }
437       }
438
439           public function addNewTag():void {
440                 if (stack.selectedChild!=advancedContainer) { return; }
441                 advancedTagGrid.addNewTag();
442           }
443       
444       private function addedToRelation(event:RelationMemberEvent):void {
445          resetRelationsGrid(selectedEntity);
446       }
447
448       private function removedFromRelation(event:RelationMemberEvent):void {
449          resetRelationsGrid(selectedEntity);
450       }
451
452       private function removeRelationListeners():void {
453           for each( var rel:Relation in listeningToRelations ) {
454               rel.removeEventListener(Connection.TAG_CHANGED, relationTagChanged);
455               rel.removeEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
456               rel.removeEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
457           }
458           listeningToRelations = [];
459           relationsGrid.removeEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged);
460       }
461             
462       private function resetRelationsGrid(entity:Entity):void {
463           removeRelationListeners();
464           var relations:Array = [];
465           for each( var rel:Relation in entity.parentRelations ) {
466               for each( var memberIndex:int in rel.findEntityMemberIndexes(entity)) {
467                   var props:Object = {};
468                   props["relation"] = rel;
469                   props["id"] = rel.id;
470                   props["index"] = memberIndex;
471                   props["role"] = rel.getMember(memberIndex).role;
472                   props["description"] = rel.getDescription();
473                   props["id_idx"] = rel.id + "/"+memberIndex;
474
475                   relations.push(props);
476               }
477               
478               rel.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
479               rel.addEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
480               rel.addEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
481               listeningToRelations.push(rel);
482           }
483           relationsGrid.dataProvider = relations;
484           relationsGrid.addEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged, false, -100);
485       }
486       
487       private function relationRoleChanged(event:DataGridEvent):void {
488                 if (event.dataField != 'role') { return; }      // shouldn't really happen
489
490                 var relations:Array=event.currentTarget.dataProvider.toArray();
491                 var props:Object=relations[event.rowIndex];
492
493                 var relation:Relation=props['relation'];
494                 var index:uint=props['index'];
495                 var newRole:String=event.itemRenderer.data['role'];
496
497                 relation.setMember(index, new RelationMember(selectedEntity,newRole));
498       }
499
500       private function relationTagChanged(event:TagEvent):void {
501           resetRelationsGrid(selectedEntity);
502       }
503
504       private function entityRelationMemberChanged(event:RelationMemberEvent):void {
505           resetRelationsGrid(selectedEntity);
506       }
507       
508       private function checkMembers():void {
509           if (selectedEntity is Relation) {
510             setupMembers(selectedEntity as Relation);
511           }
512       }
513       
514       private function setupMembers(rel:Relation):void {
515           var members:Array = [];
516           for (var i:int=0 ; i<rel.length; i++) {
517             var props:Object = {};
518             var member:RelationMember = rel.getMember(i);
519             props["id"] = member.entity.id;
520             props["type"] = member.entity.getType();
521             props["role"] = member.role;
522             
523             members.push(props);
524           }
525           membersGrid.dataProvider = members;
526           membersGrid.dataProvider.addEventListener('collectionChange', membersChange); 
527       }
528       
529       private function membersChange(event:Event):void {
530           // Dropping all the members and re-adding them isn't exactly optimal
531           // but is at least robust for any kind of change.
532           // Figuring out a better way is someone else's FIXME
533           
534           var conn:Connection = Connection.getConnectionInstance();
535           var rel:Relation = selectedEntity as Relation
536           
537           // drop members
538           for (var i:int=rel.length-1 ; i>=0; i--) {
539             rel.removeMemberByIndex(i);
540           }
541           
542           // add members in new order
543           for each(var memberObject:Object in membersGrid.dataProvider) {
544             var e:Entity;
545             var id:Number = memberObject.id;
546             if(memberObject.type == 'node') {
547               e = conn.getNode(id);
548             } else if (memberObject.type == 'way') {
549               e = conn.getWay(id);
550             } else if (memberObject.type == 'relation') {
551               e = conn.getRelation(id);
552             }
553             rel.appendMember(new RelationMember(e, memberObject.role));
554           }
555       }
556       
557       private function editRelation(id:Number):void {
558           var panel:RelationEditorPanel = RelationEditorPanel(
559               PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
560           panel.setRelation(Connection.getConnectionInstance().getRelation(id));
561           PopUpManager.centerPopUp(panel);
562       }
563       
564       private function tagChanged(event:TagEvent):void {
565           refreshFeatureIcon();
566           // if the advancedTagGrid has already been set up, it needs to be refreshed.
567           // FIXME make this better, maybe advancedTagGrid should be event listening.
568           if(advancedContainer.initialized) {
569             checkAdvanced();
570           }
571       }
572
573       public function loadFeatures():void {
574           mapFeatures = MapFeatures.getInstance();
575           stack.removeChild(membersVBox); // remove by default, will be added if relation
576       }
577
578       public function openDescription():void {
579           if ( feature != null && feature.hasHelpURL() )
580               navigateToURL(new URLRequest(feature.helpURL), "potlatch_help");
581       }
582
583       public function openEntityPage():void {
584           if (selectedEntity != null && selectedEntity.id >= 0) {
585               // This is slightly hard-coded, but not drastically. The ../s could be changed for string manipulation of the apiBase
586               var urlBase:String = Connection.getConnectionInstance().apiBase + '../../browse/'
587               navigateToURL(new URLRequest(urlBase+selectedEntity.getType()+'/'+selectedEntity.id), "potlatch_browse");
588           }
589       }
590
591       public function addToRelation():void {
592           new RelationSelectPanel().init(selectedEntity,new Object());
593       }
594       
595       public function removeFromRelation(id:Number, index:int):void {
596           Connection.getConnectionInstance().getRelation(id).removeMemberByIndex(index);
597       }
598       
599       public function initFeatureBox():void {
600           tw = new CategorySelector();
601           tw.addEventListener("selectedType", changeFeatureType);
602           popupChange.popUp = tw;
603       }
604       
605       public function changeFeatureType(event:Event):void {
606           if ( selectedEntity == null )
607               return;
608
609           UIComponent.suspendBackgroundProcessing();
610           var newFeature:Feature = tw.selectedType;
611           var undoStack:Function = MainUndoStack.getGlobalStack().addAction;
612           var action:CompositeUndoableAction = new CompositeUndoableAction(
613                   "Set "+selectedEntity.getType()+" "+selectedEntity.id+" to "+newFeature.name);
614                   selectedEntity.suspend();
615
616           // remove tags from the current feature
617           if ( feature != null ) {
618               for each( var oldtag:Object in feature.tags ) {
619                   selectedEntity.setTag(oldtag["k"], null, action.push);
620               }
621           }
622           
623           // set tags for new feature
624           if ( newFeature != null ) {
625               for each( var newtag:Object in newFeature.tags ) {
626                   selectedEntity.setTag(newtag["k"], newtag["v"], action.push);
627               }
628           }
629           
630                   selectedEntity.resume();
631           undoStack(action);
632           popupChange.close();
633           initialiseEditors();
634           UIComponent.resumeBackgroundProcessing();
635       }
636       
637         private function resizePOIGrid(event:Event):void {
638                 var rows:Number=event.target.dataProvider.length/event.target.columnCount;
639                 if (rows!=Math.floor(rows)) { rows=Math.floor(rows+1); }
640                 event.target.height=rows*(event.target.rowHeight+1);
641         }
642   ]]></mx:Script>
643 </mx:VBox>
644