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