Figure out the layer a Marker is on, and bounce it all the way to the MarkerPanel...
[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.halcyon.VectorLayer;
137       import net.systemeD.potlatch2.mapfeatures.*;
138
139       import mx.collections.*;
140       import mx.containers.*;
141       import mx.events.*;
142       import mx.core.*;
143       import mx.managers.PopUpManager;
144       import flash.geom.Point;
145       import flash.net.*;
146       import mx.events.DragEvent;
147       import mx.managers.DragManager;
148       import mx.core.DragSource;
149       import flexlib.containers.SuperTabNavigator;
150
151       private static const dndPrompt:String="Add new points by dragging them onto the map";
152
153       private var editorStackTabNavigator:SuperTabNavigator;
154       private var editorStackAccordion:Accordion;
155       [Bindable] private var editorStack:Container;
156
157       public var mapFeatures:MapFeatures;
158       private var selectedEntity:Entity;
159       private var tw:CategorySelector = null;
160       private var feature:Feature = null;
161
162           public function setEntity(entities:Array, layer:VectorLayer=null):void {
163                 UIComponent.suspendBackgroundProcessing();
164                 
165                 var firstSelected:Entity=null;
166                 if (entities.length==1) { firstSelected=entities[0]; }
167                 
168                 if (selectedEntity!=firstSelected && selectedEntity!=null) {
169                         selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
170                 }
171                 
172                 if (entities.length==0) {
173                         // Nothing selected, so show drag-and-drop panel
174                         sidebar.selectedChild = dndPanel;
175                 
176                 } else if (entities.length==1) {
177                         // Single entity selected, so show tag panel
178                         selectedEntity=firstSelected;
179                         if (selectedEntity!=null) { selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged); }
180                         if (advancedID!=null) { setupAdvanced(firstSelected); }
181                         if (firstSelected is Relation) { stack.addChild(membersVBox); }
182             if (selectedEntity is Marker) {
183               markerPanelContents.init(selectedEntity, layer);
184               sidebar.selectedChild = markerPanel;
185             } else {
186               refreshFeatureIcon();
187               initialiseEditors();
188               sidebar.selectedChild = tagsPanel;
189             }
190                 
191                 } else {
192                         // Multiple selection, so for now show a largely blank panel/
193                         // ** TODO - proper multiple selection tagging
194                         sidebar.selectedChild = multiplePanel;
195                 }
196                 UIComponent.resumeBackgroundProcessing();
197       }
198
199       private function refreshFeatureIcon():void {
200           var oldFeature:Feature = feature;
201           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
202           if ( feature != oldFeature ) {
203               if ( oldFeature != null )
204                   oldFeature.removeEventListener("imageChanged", featureImageChanged);
205               if ( feature != null )
206                   feature.addEventListener("imageChanged", featureImageChanged);
207           }
208
209           if ( feature != null && feature.name != null ) {
210               setFeatureIcon(selectedEntity, feature);
211           } else {
212               blankFeatureIcon(selectedEntity);
213           }
214       }
215
216       private function featureImageChanged(event:Event):void {
217           setFeatureIcon(selectedEntity, feature);
218       }
219       
220       private function setFeatureIcon(entity:Entity, feature:Feature):void {
221           //blankFeatureIcon(entity);
222           
223           iconImage.source = feature.image;
224
225           var txt:String = feature.htmlDetails(entity);
226           iconText.htmlText = txt;
227           popupChange.label = feature.name;
228           setLimitTypes(entity);
229           tw.setSelectedFeature(feature);
230           helpLabel.visible = feature.hasHelpURL();
231       }
232       
233       private function setLimitTypes(entity:Entity):void {
234           var type:String = null;
235           if ( entity is Node )
236               type = "point";
237           else if ( entity is Way )
238               type = Way(entity).isArea() ? "area" : "line";
239           else if ( entity is Relation )
240               type = "relation";
241           tw.setLimitTypes(type);
242       }
243
244       private function blankFeatureIcon(entity:Entity):void {
245           iconImage.source = null;
246           popupChange.label = "unknown";
247           setLimitTypes(entity);
248                   if (entity == null) { 
249                         iconText.htmlText = "<i>Nothing selected</i>";
250             tw.setNoSelectedFeature();
251                   } else if (entity.hasTags()) {
252                         iconText.htmlText = "<b>Not recognised</b><br/>Try looking at the tags under the advanced properties";
253             tw.setSelectedFeature(null);
254                   } else {
255                         iconText.htmlText = "<b>No tags set</b><br/>Use the menu above to say what this "+entity.getType()+" is";
256             tw.setSelectedFeature(null);
257           }
258           helpLabel.visible = false;
259       }
260
261       private var tabComponents:Object = {};
262       
263       private function initialiseEditors():void {
264           editorStack.removeAllChildren();
265           if ( selectedEntity == null || feature == null )
266               return;
267           
268           var editorBox:VBox = createEditorBox();
269           editorBox.label = "Basic";
270           editorStack.addChild(editorBox);
271           
272           var tabs:Object = {};
273           tabComponents = {};
274           
275           for each (var factory:EditorFactory in feature.editors) {
276               if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
277                   var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
278                   if ( editor != null )
279                       editorBox.addChild(editor);
280               }
281               var category:String = factory.category;
282               if (category!='') {
283                       var tab:VBox = tabs[category];
284                       if ( tab == null) {
285                           tab = createEditorBox();
286                           tab.label = category;
287                           editorStack.addChild(tab);
288                           tabs[category] = tab;
289                           tabComponents[tab] = [];
290                       }
291                       var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
292                       if ( catEditor != null )
293                           tabComponents[tab].push(catEditor);
294                       //    tab.addChild(catEditor);
295               }
296           }
297       }
298       
299       private function createEditorBox():VBox {
300           var box:VBox = new VBox();
301           box.percentWidth = 100;
302           box.percentHeight = 100;
303           box.styleName = "editorContainer";
304           return box;
305       }
306
307       private function ensureEditorsPopulated(tab:VBox):void {
308           var components:Array = tabComponents[tab];
309           if ( components == null || tab == null || tab.numChildren >= components.length )
310               return;
311           for each (var component:DisplayObject in components ) {
312               tab.addChild(component);
313           }
314       }
315       
316         private function initEditorStackUIs():void {
317                 editorStackTabNavigator = new SuperTabNavigator();
318                 editorStackTabNavigator.allowTabSqueezing=false;
319                 editorStackTabNavigator.minTabWidth=10;
320                 editorStackTabNavigator.closePolicy="close_never";
321                 editorStackTabNavigator.scrollSpeed=20;
322                 editorStackTabNavigator.creationPolicy="auto";
323                 editorStackTabNavigator.percentWidth=100;
324                 editorStackTabNavigator.percentHeight=100;
325
326                 editorStackAccordion = new Accordion();
327                 editorStackAccordion.percentWidth=100;
328                 editorStackAccordion.percentHeight=100;
329                 editorStackAccordion.creationPolicy="auto";
330
331                 setEditorStackUI(true);
332         }
333
334         private function setEditorStackUI(isTabbed:Boolean):void {
335                 var children:Array=[]; var i:uint;
336
337                 if (editorStack) {
338                         // blank existing component
339                         editorStack.removeEventListener("change",editorStackUIChange);
340                         editorStack.removeEventListener("updateComplete",editorStackUIUpdate);
341                         editorStack.removeAllChildren();
342                         editorContainer.removeChildAt(1);
343                 }
344
345                 // replace with new component
346                 editorStack = (isTabbed ? editorStackTabNavigator : editorStackAccordion) as Container;
347                 editorContainer.addChild(editorStack);
348
349                 // re-add children and listeners
350                 initialiseEditors();
351                 editorStack.addEventListener("change",editorStackUIChange);
352                 editorStack.addEventListener("updateComplete",editorStackUIUpdate);
353         }
354         
355         private function editorStackUIChange(event:Event):void {
356                 ensureEditorsPopulated(IndexChangedEvent(event).relatedObject as VBox);
357         }
358
359         private function editorStackUIUpdate(event:Event):void {
360                 if (editorStack is SuperTabNavigator) {
361                         var e:SuperTabNavigator = editorStack as SuperTabNavigator;
362                         if (e.selectedIndex<0) { return; }
363                         for (var i:uint=0; i<e.numChildren; i++) {
364                                 e.getTabAt(i).selected=(i==e.selectedIndex);
365                         }
366                 }
367         }
368
369
370         public function togglePanel():void {
371                 if (stack.selectedChild==editorContainer && selectedEntity!=null) {
372                         stack.selectedChild=advancedContainer;
373                 } else if (stack.selectedChild!=editorContainer) {
374                         stack.selectedChild=editorContainer;
375                 }
376         }
377
378       private function checkAdvanced():void {
379           if ( selectedEntity != null )
380              setupAdvanced(selectedEntity);
381       }
382
383       /**
384       * When you enter a new tag and press enter, the focus moves to the add button
385       * Adding an event listener to the button and listening for + means that you
386       * can add two consecutive tags with the same keypress. This relies on focus
387       * moving to the "add" button, though, so it's not robust to rearranging the UI.
388       */
389       private function advancedAddButtonKeyboardEvent(event:KeyboardEvent):void {
390           switch (event.keyCode) {
391             case 187:   addNewTag(); break;                          // + - add tag
392           }
393       }
394
395       private var listeningToRelations:Array = [];
396       
397       private function setupAdvanced(entity:Entity):void {
398                 advancedTagGrid.init(entity);
399         advancedAddButton.addEventListener(KeyboardEvent.KEY_DOWN, advancedAddButtonKeyboardEvent);
400
401                 if ( entity == null ) {
402                         advancedID.htmlText = "";
403                 } else {
404                         var entityText:String = "xx";
405                         if      ( entity is Node     ) entityText = "Node";
406                         else if ( entity is Way      ) entityText = "Way";
407                         else if ( entity is Relation ) entityText = "Relation";
408                         advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
409                 }
410
411                 removeRelationListeners();
412                 if ( selectedEntity != null ) {
413                         selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
414                         selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
415                 }
416
417                 if ( entity == null ) {
418                         relationsGrid.dataProvider = null;
419                 } else {
420                         resetRelationsGrid(entity);
421                         entity.addEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
422                         entity.addEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
423                 }
424       }
425
426           public function addNewTag():void {
427                 if (stack.selectedChild!=advancedContainer) { return; }
428                 advancedTagGrid.addNewTag();
429           }
430       
431       private function addedToRelation(event:RelationMemberEvent):void {
432          resetRelationsGrid(selectedEntity);
433       }
434
435       private function removedFromRelation(event:RelationMemberEvent):void {
436          resetRelationsGrid(selectedEntity);
437       }
438
439       private function removeRelationListeners():void {
440           for each( var rel:Relation in listeningToRelations ) {
441               rel.removeEventListener(Connection.TAG_CHANGED, relationTagChanged);
442               rel.removeEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
443               rel.removeEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
444           }
445           listeningToRelations = [];
446           relationsGrid.removeEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged);
447       }
448             
449       private function resetRelationsGrid(entity:Entity):void {
450           removeRelationListeners();
451           var relations:Array = [];
452           for each( var rel:Relation in entity.parentRelations ) {
453               for each( var memberIndex:int in rel.findEntityMemberIndexes(entity)) {
454                   var props:Object = {};
455                   props["relation"] = rel;
456                   props["id"] = rel.id;
457                   props["index"] = memberIndex;
458                   props["role"] = rel.getMember(memberIndex).role;
459                   props["description"] = rel.getDescription();
460                   props["id_idx"] = rel.id + "/"+memberIndex;
461
462                   relations.push(props);
463               }
464               
465               rel.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
466               rel.addEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
467               rel.addEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
468               listeningToRelations.push(rel);
469           }
470           relationsGrid.dataProvider = relations;
471           relationsGrid.addEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged, false, -100);
472       }
473       
474       private function relationRoleChanged(event:DataGridEvent):void {
475                 if (event.dataField != 'role') { return; }      // shouldn't really happen
476
477                 var relations:Array=event.currentTarget.dataProvider.toArray();
478                 var props:Object=relations[event.rowIndex];
479
480                 var relation:Relation=props['relation'];
481                 var index:uint=props['index'];
482                 var newRole:String=event.itemRenderer.data['role'];
483
484                 relation.setMember(index, new RelationMember(selectedEntity,newRole));
485       }
486
487       private function relationTagChanged(event:TagEvent):void {
488           resetRelationsGrid(selectedEntity);
489       }
490
491       private function entityRelationMemberChanged(event:RelationMemberEvent):void {
492           resetRelationsGrid(selectedEntity);
493       }
494       
495       private function checkMembers():void {
496           if (selectedEntity is Relation) {
497             setupMembers(selectedEntity as Relation);
498           }
499       }
500       
501       private function setupMembers(rel:Relation):void {
502           var members:Array = [];
503           for (var i:int=0 ; i<rel.length; i++) {
504             var props:Object = {};
505             var member:RelationMember = rel.getMember(i);
506             props["id"] = member.entity.id;
507             props["type"] = member.entity.getType();
508             props["role"] = member.role;
509             
510             members.push(props);
511           }
512           membersGrid.dataProvider = members;
513           membersGrid.dataProvider.addEventListener('collectionChange', membersChange); 
514       }
515       
516       private function membersChange(event:Event):void {
517           // Dropping all the members and re-adding them isn't exactly optimal
518           // but is at least robust for any kind of change.
519           // Figuring out a better way is someone else's FIXME
520           
521           var conn:Connection = Connection.getConnectionInstance();
522           var rel:Relation = selectedEntity as Relation
523           
524           // drop members
525           for (var i:int=rel.length-1 ; i>=0; i--) {
526             rel.removeMemberByIndex(i);
527           }
528           
529           // add members in new order
530           for each(var memberObject:Object in membersGrid.dataProvider) {
531             var e:Entity;
532             var id:Number = memberObject.id;
533             if(memberObject.type == 'node') {
534               e = conn.getNode(id);
535             } else if (memberObject.type == 'way') {
536               e = conn.getWay(id);
537             } else if (memberObject.type == 'relation') {
538               e = conn.getRelation(id);
539             }
540             rel.appendMember(new RelationMember(e, memberObject.role));
541           }
542       }
543       
544       private function editRelation(id:Number):void {
545           var panel:RelationEditorPanel = RelationEditorPanel(
546               PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
547           panel.setRelation(Connection.getConnectionInstance().getRelation(id));
548           PopUpManager.centerPopUp(panel);
549       }
550       
551       private function tagChanged(event:TagEvent):void {
552           refreshFeatureIcon();
553           // if the advancedTagGrid has already been set up, it needs to be refreshed.
554           // FIXME make this better, maybe advancedTagGrid should be event listening.
555           setupAdvanced(selectedEntity);
556       }
557
558       public function loadFeatures():void {
559           mapFeatures = MapFeatures.getInstance();
560           stack.removeChild(membersVBox); // remove by default, will be added if relation
561       }
562
563       public function openDescription():void {
564           if ( feature != null && feature.hasHelpURL() )
565               navigateToURL(new URLRequest(feature.helpURL), "potlatch_help");
566       }
567
568       public function openEntityPage():void {
569           if (selectedEntity != null && selectedEntity.id >= 0) {
570               // This is slightly hard-coded, but not drastically. The ../s could be changed for string manipulation of the apiBase
571               var urlBase:String = Connection.getConnectionInstance().apiBase + '../../browse/'
572               navigateToURL(new URLRequest(urlBase+selectedEntity.getType()+'/'+selectedEntity.id), "potlatch_browse");
573           }
574       }
575
576       public function addToRelation():void {
577           new RelationSelectPanel().init(selectedEntity,new Object());
578       }
579       
580       public function removeFromRelation(id:Number, index:int):void {
581           Connection.getConnectionInstance().getRelation(id).removeMemberByIndex(index);
582       }
583       
584       public function initFeatureBox():void {
585           tw = new CategorySelector();
586           tw.addEventListener("selectedType", changeFeatureType);
587           popupChange.popUp = tw;
588       }
589       
590       public function changeFeatureType(event:Event):void {
591           if ( selectedEntity == null )
592               return;
593
594           UIComponent.suspendBackgroundProcessing();
595           var newFeature:Feature = tw.selectedType;
596           var undoStack:Function = MainUndoStack.getGlobalStack().addAction;
597           var action:CompositeUndoableAction = new CompositeUndoableAction(
598                   "Set "+selectedEntity.getType()+" "+selectedEntity.id+" to "+newFeature.name);
599                   selectedEntity.suspend();
600
601           // remove tags from the current feature
602           if ( feature != null ) {
603               for each( var oldtag:Object in feature.tags ) {
604                   selectedEntity.setTag(oldtag["k"], null, action.push);
605               }
606           }
607           
608           // set tags for new feature
609           if ( newFeature != null ) {
610               for each( var newtag:Object in newFeature.tags ) {
611                   selectedEntity.setTag(newtag["k"], newtag["v"], action.push);
612               }
613           }
614           
615                   selectedEntity.resume();
616           undoStack(action);
617           popupChange.close();
618           initialiseEditors();
619           UIComponent.resumeBackgroundProcessing();
620       }
621       
622         private function resizePOIGrid(event:Event):void {
623                 var rows:Number=event.target.dataProvider.length/event.target.columnCount;
624                 if (rows!=Math.floor(rows)) { rows=Math.floor(rows+1); }
625                 event.target.height=rows*(event.target.rowHeight+1);
626         }
627   ]]></mx:Script>
628 </mx:VBox>
629