Merge branch 'master' into history
[potlatch2.git] / net / systemeD / potlatch2 / TagViewer.mxml
index efbf61a..dd1fefb 100644 (file)
@@ -1,6 +1,13 @@
 <?xml version="1.0" encoding="utf-8"?>
+<!---
+  The TagViewer is the container for the entire sidebar. It has a number of components, and subpanels, in a ViewStack that change
+  depending on what is selected, i.e. the ControllerState.
+
+  @see net.systemeD.potlatch2.controller.ControllerState
+    -->
 <mx:VBox
-       xmlns:mx="http://www.adobe.com/2006/mxml"
+    xmlns:fx="http://ns.adobe.com/mxml/2009"
+       xmlns:mx="library://ns.adobe.com/flex/mx"
        xmlns:flexlib="flexlib.containers.*"
        xmlns:controls="net.systemeD.controls.*"
        xmlns:potlatch2="net.systemeD.potlatch2.*"
                <mx:TileList dataProvider="{dndRep.currentItem.getFeaturesForType('point', true)}" width="100%" height="1"
                                     rowHeight="32" columnWidth="32" updateComplete="resizePOIGrid(event)" styleName="dndPanelTileList">
                        <mx:itemRenderer>
-                               <mx:Component>
+                               <fx:Component>
                                        <mx:VBox toolTip="{data.name}">
-                                               <mx:Script><![CDATA[
+                                               <fx:Script><![CDATA[
                                                        import mx.events.DragEvent;
                                                        import mx.managers.DragManager;
                                                        import mx.core.DragSource;
 
                                                        private function dragPOI(event:MouseEvent, tags:Array):void {
+                                                               if (outerDocument.controller.map.dragstate==outerDocument.controller.map.DRAGGING) return;
+
                                                                // Get the drag initiator component from the event object.
                                                                var dragInitiator:Image = event.currentTarget as Image;
                                                                var dragSource:DragSource = new DragSource();
                                                                dragProxy.height = dragInitiator.height; // for non-embedded images
                                                                DragManager.doDrag(dragInitiator, dragSource, event, dragProxy);
                                                        }
-                                               ]]></mx:Script>
+                                               ]]></fx:Script>
                                                <mx:Image id="foo" source="{data.dndimage}" height="24" width="24" mouseMove="dragPOI(event, data.tags)" toolTip="{data.name}" />
                                        </mx:VBox>
-                               </mx:Component>
+                               </fx:Component>
                        </mx:itemRenderer>
                </mx:TileList>
        </mx:Repeater>
@@ -54,8 +63,8 @@
 
   <!-- Standard tagging panel -->
 
-  <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto">
-    <mx:ViewStack id="stack" width="100%" height="100%">
+  <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto" tabChildren="true">
+    <mx:ViewStack id="stack" width="100%" height="100%" change="tagsPanel.tabChildren=(event.newIndex==0)">
       <mx:VBox width="100%" height="100%" label="Simple" id="editorContainer" creationComplete="initEditorStackUIs()" styleName="dndPanelVbox">
         <mx:VBox width="100%" verticalGap="1" styleName="dndTagHeader">
           <mx:HBox width="100%" id="iconContainer" styleName="featureSelector">
@@ -69,7 +78,7 @@
         </mx:VBox>
       </mx:VBox>
       <mx:VBox width="100%" height="100%" label="Advanced" id="advancedContainer" initialize="checkAdvanced()" verticalGap="1">
-        <mx:Label id="advancedID" click="openEntityPage()">
+        <mx:Label id="advancedID" click="new HistoryDialog().init(selectedEntity);">
           <mx:htmlText><![CDATA[<i>No Selection</i>]]></mx:htmlText>
         </mx:Label>
 
                 <mx:DataGridColumn editable="false" dataField="description" headerText="Relation"/>
                 <mx:DataGridColumn editable="false" dataField="id_idx" headerText="ID"/>
                 <mx:DataGridColumn editable="true" dataField="role" headerText="Role">
-                    <mx:itemEditor><mx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></mx:Component></mx:itemEditor>
+                    <mx:itemEditor><fx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></fx:Component></mx:itemEditor>
                 </mx:DataGridColumn>
                                <mx:DataGridColumn width="20" editable="false">
                                        <mx:itemRenderer>
-                                               <mx:Component>
+                                               <fx:Component>
                                    <mx:HBox horizontalAlign="center" verticalAlign="middle">
                                                                <mx:PopUpButton arrowButtonWidth="12" paddingLeft="0" paddingRight="0" width="12" height="12" 
                                                                                open="{outerDocument.updateRelationMenu(event);}" 
                                                                                creationComplete="{outerDocument.createRelationMenu(PopUpButton(event.target));}"/>
                                                        </mx:HBox>
-                                               </mx:Component>
+                                               </fx:Component>
                                        </mx:itemRenderer>
                                </mx:DataGridColumn>
             </mx:columns>
             <mx:DataGridColumn editable="false" dataField="type" headerText="Type"/>
             <mx:DataGridColumn editable="false" dataField="id" headerText="ID"/>
             <mx:DataGridColumn editable="true" dataField="role" headerText="Role">
-                               <mx:itemEditor><mx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></mx:Component></mx:itemEditor>
+                               <mx:itemEditor><fx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></fx:Component></mx:itemEditor>
                        </mx:DataGridColumn>
           </mx:columns>
         </mx:DataGrid>
                        disabledIcon="@Embed('../../../embedded/view_accordion_disabled.png')"
                        click="setEditorStackUI(false)" id="accordionLabel" paddingTop="6"
                        toolTip="Show in sliding windows"
-                       enabled="{editorStack is SuperTabNavigator &amp;&amp; stack.selectedIndex==0}" />
+                       enabled="{editorStack is TabNavigator &amp;&amp; stack.selectedIndex==0}" />
          </mx:HBox>
   </mx:VBox>
 
             <mx:DataGridColumn editable="false" dataField="description" headerText="Relation"/>
             <mx:DataGridColumn editable="false" dataField="id_idx" headerText="ID"/>
             <mx:DataGridColumn editable="true" dataField="role" headerText="Role">
-                <mx:itemEditor><mx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></mx:Component></mx:itemEditor>
+                <mx:itemEditor><fx:Component><mx:TextInput restrict="&#x0020;-&#x10FFFF;" /></fx:Component></mx:itemEditor>
             </mx:DataGridColumn>
                        <mx:DataGridColumn width="20" editable="false">
                                <mx:itemRenderer>
-                                       <mx:Component>
+                                       <fx:Component>
                            <mx:HBox horizontalAlign="center" verticalAlign="middle">
                                                        <mx:PopUpButton arrowButtonWidth="12" paddingLeft="0" paddingRight="0" width="12" height="12" 
                                                                        open="{outerDocument.updateRelationMenu(event);}" 
                                                                        creationComplete="{outerDocument.createRelationMenu(PopUpButton(event.target));}"/>
                                                </mx:HBox>
-                                       </mx:Component>
+                                       </fx:Component>
                                </mx:itemRenderer>
                        </mx:DataGridColumn>
         </mx:columns>
     <sidepanel:BugPanel id="bugPanelContents" width="100%"/>
   </mx:VBox>
 
+  <!-- Background layer selection -->
+
+  <mx:VBox id="backgroundPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
+    <sidepanel:BackgroundPanel id="backgroundPanelContents" width="100%"/>
+  </mx:VBox>
+
+  <!-- merge tags from background layer -->
+
+  <mx:VBox id="backgroundMergePanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
+    <sidepanel:BackgroundMergePanel id="backgroundMergePanelContents" width="100%" />
+  </mx:VBox>
+
 </mx:ViewStack>
 
-  <mx:Script><![CDATA[
+  <fx:Script><![CDATA[
       import net.systemeD.halcyon.connection.*;
-      import net.systemeD.halcyon.VectorLayer;
+      import net.systemeD.halcyon.MapPaint;
+      import net.systemeD.potlatch2.EditController;
       import net.systemeD.potlatch2.mapfeatures.*;
+      import net.systemeD.potlatch2.history.HistoryDialog;
+      import net.systemeD.potlatch2.mapfeatures.editors.*;
+      import net.systemeD.potlatch2.utils.*;
+      import net.systemeD.controls.CollapsiblePanel;
 
       import mx.collections.*;
-      import mx.containers.*;
+      import mx.containers.VBox;
+      import mx.containers.HBox;
+      import mx.containers.TabNavigator;
+      import mx.containers.Accordion;
       import mx.events.*;
       import mx.core.*;
       import mx.managers.PopUpManager;
       import mx.controls.Menu;
+      import mx.controls.Alert;
       import flash.geom.Point;
       import flash.net.*;
       import mx.events.DragEvent;
       import mx.managers.DragManager;
       import mx.core.DragSource;
-      import flexlib.containers.SuperTabNavigator;
-
-      private static const dndPrompt:String="Add new points by dragging them onto the map";
-
-      private var editorStackTabNavigator:SuperTabNavigator;
+      import mx.controls.TabBar;
+      import spark.components.Form;
+      import spark.layouts.FormLayout;
+//    import flexlib.containers.SuperTabNavigator;
+
+      [Bindable] [Embed(source="../../../embedded/tab_basic.png"       )] private var tabIconBasic:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_address.png"     )] private var tabIconAddress:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_cycle.png"       )] private var tabIconCycle:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_details.png"     )] private var tabIconDetails:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_restrictions.png")] private var tabIconRestrictions:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_transport.png"   )] private var tabIconTransport:Class;
+      [Bindable] [Embed(source="../../../embedded/tab_walk.png"        )] private var tabIconWalk:Class;
+      private var tabIcons:Object= { Basic:tabIconBasic, Details:tabIconDetails, Address:tabIconAddress, Walk:tabIconWalk, Cycle:tabIconCycle, 
+                                        Transport:tabIconTransport, Restrictions:tabIconRestrictions};
+
+      [Bindable]
+      public var dndPrompt:String="Add new points by dragging them onto the map";
+
+      private var editorStackTabNavigator:TabNavigator;
       private var editorStackAccordion:Accordion;
       [Bindable] private var editorStack:Container;
 
       public var mapFeatures:MapFeatures;
+      public var controller:EditController;
       private var selectedEntity:Entity;
+      private var connection:Connection;
       private var currentCategorySelector:CategorySelector;
       private var categorySelectors:Object = {};       // hash of categorySelectors for each limitType
       private var feature:Feature = null;
 
       private var rowData:Object;              // relation membership reference, needed so it's accessible from relation actions menu
 
-         public function setEntity(entities:Array, layer:VectorLayer=null):void {
+         public function setEntity(entities:Array, layer:MapPaint=null):void {
                UIComponent.suspendBackgroundProcessing();
+               connection=null;
 
                var firstSelected:Entity=null;
                if (entities.length==1) { firstSelected=entities[0]; }
                        selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
                        selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
                        selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
+                       if (selectedEntity is EntityCollection) EntityCollection(selectedEntity).releaseListeners();
                }
 
                if (entities.length==0) {
                } else if (entities.length==1) {
                        // Single entity selected, so show tag panel
                        selectedEntity=firstSelected;
+                       connection=firstSelected.connection;
                        if (selectedEntity!=null) { selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged); }
                        if (advancedID!=null) { setupAdvanced(firstSelected); }
                        if (firstSelected is Relation) { stack.addChild(membersVBox); }
-            if (selectedEntity is Marker && layer is BugLayer) {
-              bugPanelContents.init(selectedEntity, layer);
+            if (selectedEntity is Marker && connection is BugConnection) {
+              bugPanelContents.init(selectedEntity, BugConnection(connection));
               sidebar.selectedChild = bugPanel;
             } else if (selectedEntity is Marker) {
               markerPanelContents.init(selectedEntity, layer);
               sidebar.selectedChild = markerPanel;
+            } else if (connection is SnapshotConnection) {
+              backgroundPanelContents.init(selectedEntity);
+              sidebar.selectedChild = backgroundPanel;
             } else {
               refreshFeatureIcon();
               initialiseEditors();
               sidebar.selectedChild = tagsPanel;
             }
 
+        } else if (entities.length==2
+                      && xor(entities[0].connection is SnapshotConnection, entities[1].connection is SnapshotConnection)
+                      && xor(!controller.map.getLayerForEntity(entities[0]).isBackground, !controller.map.getLayerForEntity(entities[1]).isBackground) ) {
+            backgroundMergePanelContents.init(entities);
+            sidebar.selectedChild = backgroundMergePanel;
+
                } else if(isMultipleEditable(entities)) {
                        selectedEntity = new EntityCollection(entities);
                        selectedEntity.addEventListener(Connection.TAG_CHANGED, tagChanged);
             sidebar.selectedChild = multiplePanel;
                        setupMultiAdvanced(selectedEntity);
-
+                       connection=entities[0].connection;
+                       
                } else {
                        //The selection contains elements which can't be edited all together.
                        sidebar.selectedChild = multipleInvalidPanel;
                UIComponent.resumeBackgroundProcessing();
       }
 
+      private function xor(a:Boolean, b:Boolean):Boolean {
+          return ( a || b) && !(a && b);
+      }
+
       private function refreshFeatureIcon():void {
           var oldFeature:Feature = feature;
           feature = selectedEntity == null ? null : mapFeatures.findMatchingFeature(selectedEntity);
       }
 
       private var tabComponents:Object = {};
+      private var subpanelComponents:Object = {};
 
       private function initialiseEditors():void {
-          editorStack.removeAllChildren();
-          if ( selectedEntity == null || feature == null )
-              return;
+               editorStack.removeAllChildren();
+               if ( selectedEntity == null || feature == null )
+                       return;
+
+               var basicEditorBox:VBox = createEditorBox();
+               basicEditorBox.label = "Basic";
+               basicEditorBox.icon=tabIconBasic;
+               editorStack.addChild(basicEditorBox);
+
+               var tabs:Object = {};
+               var tabList:Array = [];
+               tabComponents = {};
+               var subpanels:Object = {};
+               subpanelComponents = {};
+
+               // First create the tabs
+               for each (var factory:EditorFactory in feature.editors) {
+                       var category:String = factory.category;
+                       if (category!='') {
+                               var tab:VBox = tabs[category];
+                               if ( tab == null) {
+                                       tab = createEditorBox();
+                                       tab.label = category;
+                                       if (tabIcons[category]) tab.icon=tabIcons[category];
+                                       tabs[category] = tab;
+                                       tabList.push(tab);      
+                               }
+                       }
+               }
 
-          var editorBox:VBox = createEditorBox();
-          editorBox.label = "Basic";
-          editorStack.addChild(editorBox);
+               // Put the tabs on-screen in order
+               tabList.sort(sortCategories,16);
+               for each (tab in tabList) {
+                       editorStack.addChild(tab);
+                       tabComponents[tab] = [];
+               }
 
-          var tabs:Object = {};
-          tabComponents = {};
+               // Then add the individual editors to them
+               for each (factory in feature.editors) {
 
-          for each (var factory:EditorFactory in feature.editors) {
-              if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
-                  var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
-                  if ( editor != null )
-                      editorBox.addChild(editor);
-              }
-              var category:String = factory.category;
-              if (category!='') {
-                     var tab:VBox = tabs[category];
-                     if ( tab == null) {
-                         tab = createEditorBox();
-                         tab.label = category;
-                         editorStack.addChild(tab);
-                         tabs[category] = tab;
-                         tabComponents[tab] = [];
-                     }
-                     var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
-                     if ( catEditor != null )
-                         tabComponents[tab].push(catEditor);
-                     //    tab.addChild(catEditor);
-              }
-          }
-      }
+                       // Add to basic editor box first
+                       if ( factory.presence.isEditorPresent(factory, selectedEntity, null) ) {
+                               var editor:DisplayObject = factory.createEditorInstance(selectedEntity);
+                               if (editor) Form(basicEditorBox.getChildByName("form")).addElement(UIComponent(editor));
+                       }
 
-      private function createEditorBox():VBox {
-          var box:VBox = new VBox();
-          box.percentWidth = 100;
-          box.percentHeight = 100;
-          box.styleName = "dndEditorContainer";
-          return box;
+                       // Then prepare to add to category panel
+                       category=factory.category;
+                       if (factory.category=='') continue;
+                       var catEditor:DisplayObject = factory.createEditorInstance(selectedEntity);
+                       if (!catEditor) continue;
+                       tab=tabs[category];
+                       
+                       // Create subcategory panel if needed
+                       if (factory.subcategory) {
+                               var subcategory:String = factory.subcategory;
+                               if (!subpanels[category]) { subpanels[category]={}; }
+                               var subpanel:CollapsiblePanel = subpanels[category][subcategory];
+                               if (!subpanel) {
+                                       subpanel=new CollapsiblePanel(false);
+                                       subpanel.percentWidth=100;
+                                       subpanel.styleName="subcategoryPanel";
+                                       subpanel.title=subcategory;
+                                       subpanels[category][subcategory]=subpanel;
+                                       addConstrainedForm(subpanel);
+                                       tabComponents[tab].push(subpanel);
+                               }
+                               Form(subpanel.getChildByName("form")).addElement(UIComponent(catEditor));
+                       } else {
+                               tabComponents[tab].push(catEditor);
+                       }
+               }
       }
 
-      private function ensureEditorsPopulated(tab:VBox):void {
-          var components:Array = tabComponents[tab];
-          if ( components == null || tab == null || tab.numChildren >= components.length )
-              return;
-          for each (var component:DisplayObject in components ) {
-              tab.addChild(component);
-          }
-      }
+       // ** FIXME: Order probably shouldn't be hard-coded, but configurable
+       private static var categoryOrder:Array=["Restrictions","Transport","Cycle","Walk","Address","Details"]; // reverse order
+       private function sortCategories(a:VBox,b:VBox):int {
+               var a1:int=categoryOrder.indexOf(a.label);
+               var a2:int=categoryOrder.indexOf(b.label);
+               if (a1<a2) { return 1; }
+               else if (a1>a2) { return -1; }
+               return 0;
+       }
+       
+       private function createEditorBox():VBox {
+               var box:VBox = new VBox();
+               box.percentWidth = 100;
+               box.percentHeight = 100;
+               box.styleName = "dndEditorContainer";
+               addConstrainedForm(box);
+               return box;
+       }
+       
+       private function addConstrainedForm(parentObject:DisplayObjectContainer):void {
+               var form:Form = new Form();
+               form.name = "form";
+               form.percentWidth=100;
+               form.addEventListener(mx.events.ResizeEvent.RESIZE, formResizeHandler, false, 0, true);
+               parentObject.addChild(form);
+               if (parentObject.width>0) form.maxWidth=parentObject.width;
+       }
+       
+       private function formResizeHandler(e:Event):void {
+               var form:Form=Form(e.target);
+               if (form.parent.width>0) form.maxWidth=form.parent.width;
+       }
+
+       private function ensureEditorsPopulated(tab:VBox):void {
+               var components:Array = tabComponents[tab];
+               var form:Form=Form(tab.getChildByName("form"));
+               if ( components == null || tab == null || form.numElements >= components.length ) return;
+               for each (var component:DisplayObject in components ) {
+                       form.addElement(UIComponent(component));
+               }
+       }
 
        private function initEditorStackUIs():void {
-               editorStackTabNavigator = new SuperTabNavigator();
-               editorStackTabNavigator.allowTabSqueezing=false;
-               editorStackTabNavigator.minTabWidth=10;
-               editorStackTabNavigator.closePolicy="close_never";
-               editorStackTabNavigator.scrollSpeed=20;
+               editorStackTabNavigator = new TabNavigator();
+//             editorStackTabNavigator.allowTabSqueezing=false;
+//             editorStackTabNavigator.minTabWidth=10;
+//             editorStackTabNavigator.closePolicy="close_never";
+//             editorStackTabNavigator.scrollSpeed=20;
                editorStackTabNavigator.creationPolicy="auto";
                editorStackTabNavigator.percentWidth=100;
                editorStackTabNavigator.percentHeight=100;
                editorStackTabNavigator.styleName="dndStackTab";
-               editorStackTabNavigator.popUpButtonPolicy="off"
+//             editorStackTabNavigator.popUpButtonPolicy="off"
 
 
                editorStackAccordion = new Accordion();
                editorStackAccordion.percentHeight=100;
                editorStackAccordion.creationPolicy="auto";
                editorStackAccordion.styleName="dndStackAccordion";
+               /* FIXME: the accordion icons should be right-aligned. See:
+               http://www.kristoferjoseph.com/blog/2008/11/06/positioning-the-flex-accordion-header-icon
+               http://blog.flexexamples.com/2007/09/13/changing-text-alignment-in-an-flex-accordion-header/
+               */
 
                setEditorStackUI(true);
        }
        }
 
        private function editorStackUIUpdate(event:Event):void {
-               if (editorStack is SuperTabNavigator) {
-                       var e:SuperTabNavigator = editorStack as SuperTabNavigator;
+               if (editorStack is TabNavigator) {
+                       var e:TabNavigator = editorStack as TabNavigator;
                        if (e.selectedIndex<0) { return; }
                        for (var i:uint=0; i<e.numChildren; i++) {
                                e.getTabAt(i).selected=(i==e.selectedIndex);
                        if      ( entity is Node     ) entityText = "Node";
                        else if ( entity is Way      ) entityText = "Way";
                        else if ( entity is Relation ) entityText = "Relation";
-                       advancedID.htmlText = entityText+": <b>"+entity.id+"</b>";
+                       advancedID.htmlText = entityText+": <b>"+entity.id+"</b> "+(entity.status ? entity.status : '');
                }
 
                removeRelationListeners();
       }
 
          public function addNewTag():void {
-               if (stack.selectedChild!=advancedContainer) { return; }
-               advancedTagGrid.addNewTag();
+               if (sidebar.selectedChild==multiplePanel) { multiAdvancedTagGrid.addNewTag(); }
+               else if (stack.selectedChild==advancedContainer) { advancedTagGrid.addNewTag(); }
          }
 
       private function addedToRelation(event:RelationMemberEvent):void {
           // but is at least robust for any kind of change.
           // Figuring out a better way is someone else's FIXME
 
-          var conn:Connection = Connection.getConnectionInstance();
-          var rel:Relation = selectedEntity as Relation
+          var rel:Relation = selectedEntity as Relation;
           var action:CompositeUndoableAction = new CompositeUndoableAction("Rearrange relation members for "+rel);
 
           // drop members
           for each(var memberObject:Object in membersGrid.dataProvider) {
             var e:Entity;
             var id:Number = memberObject.id;
-            if(memberObject.type == 'node') {
-              e = conn.getNode(id);
-            } else if (memberObject.type == 'way') {
-              e = conn.getWay(id);
-            } else if (memberObject.type == 'relation') {
-              e = conn.getRelation(id);
+                       switch (memberObject.type) {
+                               case 'node':            e = connection.getNode(id); break;
+               case 'way':                     e = connection.getWay(id); break;
+                               case 'relation':        e = connection.getRelation(id); break;
             }
             rel.appendMember(new RelationMember(e, memberObject.role), action.push);
           }
 
       private function editRelation(id:Number):void {
           var panel:RelationEditorPanel = RelationEditorPanel(
-              PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
-          panel.setRelation(Connection.getConnectionInstance().getRelation(id));
+              PopUpManager.createPopUp(Application(FlexGlobals.topLevelApplication), RelationEditorPanel, true));
+          panel.setRelation(connection.getRelation(id));
           PopUpManager.centerPopUp(panel);
       }
 
                var menu:Menu = new Menu(); 
                var dp:Object = [ {label: "Select all members"}, 
                                                  {label: "Deselect all members"}, 
-                                                 {label: "Add selection to this relation", enabled: false} ]; 
+                                                 {label: "Add selection to this relation", enabled: false},
+                                                 {label: "Delete relation"} ]; 
                menu.dataProvider = dp; 
                menu.addEventListener("itemClick", selectRelationMenu); 
                button.popUp = menu;
        public function selectRelationMenu(event:MenuEvent):void {
                var rel:Relation=rowData.relation;
                var entities:Array;
-               var controller:EditController=Application.application.theController;
+               var controller:EditController=FlexGlobals.topLevelApplication.theController;
                switch (event.index) {
                        case 0: // Select all members
                                entities=selectedEntity.entities.concat(rel.memberEntities);
                                }
                                MainUndoStack.getGlobalStack().addAction(undo);
                                break;
+                       
+                       case 3: // Delete relation
+                               var warning:String="This relation has # members. Deleting it will affect all of them and erase this $. Are you really sure?";
+                               warning=warning.replace("#",rel.length).replace("$",rel.getRelationType());
+                               Alert.show(warning,"Are you sure?",Alert.YES | Alert.CANCEL,null,
+                                       function(event:CloseEvent):void { 
+                                               if (event.detail==Alert.CANCEL) return;
+                                               rel.remove(MainUndoStack.getGlobalStack().addAction);
+                                       } , null, Alert.CANCEL);
+                               break;
                }
        }
 
               navigateToURL(new URLRequest(feature.helpURL), "potlatch_help");
       }
 
-      /** Open up a new browser page showing OSM's view of the current entity. */
-      public function openEntityPage():void {
-          if (selectedEntity != null && selectedEntity.id >= 0) {
-              // This is slightly hard-coded, but not drastically. The ../s could be changed for string manipulation of the apiBase
-              var urlBase:String = Connection.getConnectionInstance().apiBase + '../../browse/'
-              navigateToURL(new URLRequest(urlBase+selectedEntity.getType()+'/'+selectedEntity.id), "potlatch_browse");
-          }
-      }
-
       public function addToRelation():void {
           new RelationSelectPanel().init(selectedEntity,new Object());
       }
 
       private function removeFromRelation(id:Number, index:int=-1):void {
-               var rel:Relation=Connection.getConnectionInstance().getRelation(id);
+               var rel:Relation=connection.getRelation(id);
                if (index>-1) {
                        rel.removeMemberByIndex(index, MainUndoStack.getGlobalStack().addAction);
                } else if (selectedEntity is EntityCollection) {
           var action:CompositeUndoableAction = new CompositeUndoableAction(
                   "Set "+selectedEntity.getType()+" "+selectedEntity.id+" to "+newFeature.name);
                  selectedEntity.suspend();
+          
+          // build a list of tags that are editable in new feature
+          var editableTags:Array = new Array();
+          for each( var editor:EditorFactory in newFeature.editors ) {
+              if ( editor is SingleTagEditorFactory ) {
+                  var singleTagEditor:SingleTagEditorFactory = editor as SingleTagEditorFactory;
+                  editableTags.push(singleTagEditor.key);
+              }
+          }
 
           // remove tags from the current feature
           if ( feature != null ) {
               for each( var oldtag:Object in feature.tags ) {
-                  selectedEntity.setTag(oldtag["k"], null, action.push);
+                  if ( editableTags.indexOf(oldtag["k"]) < 0 ) {
+                      selectedEntity.setTag(oldtag["k"], null, action.push);
+                  }
               }
           }
 
                if (rows!=Math.floor(rows)) { rows=Math.floor(rows+1); }
                event.target.height=rows*(event.target.rowHeight+1);
        }
-  ]]></mx:Script>
+
+  ]]></fx:Script>
 </mx:VBox>