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