relation editing for multiple selections
[potlatch2.git] / net / systemeD / potlatch2 / TagViewer.mxml
index 8588d27092efbe2640d97f227c63d4397f43617d..f9b5891269a30fdf4cfd7c715653a4fb2ed59e3f 100644 (file)
@@ -10,6 +10,9 @@
     initialize="loadFeatures()">
 
 <mx:ViewStack id="sidebar" width="100%" height="100%" creationPolicy="all">
+
+  <!-- Drag & drop icon panel -->
+
   <mx:VBox id="dndPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
     <mx:Text id="dndPanelText" text="{dndPrompt}" width="100%" styleName="helpInfo" />
        <mx:Repeater id="dndRep" dataProvider="{MapFeatures.getInstance().getCategoriesForType('point')}" styleName="dndRepeater">
@@ -49,6 +52,8 @@
        </mx:Repeater>
   </mx:VBox>
 
+  <!-- Standard tagging panel -->
+
   <mx:VBox id="tagsPanel" width="100%" height="100%" creationPolicy="auto">
     <mx:ViewStack id="stack" width="100%" height="100%">
       <mx:VBox width="100%" height="100%" label="Simple" id="editorContainer" creationComplete="initEditorStackUIs()" styleName="dndPanelVbox">
          </mx:HBox>
   </mx:VBox>
 
+  <!-- Multiple selection -->
+
   <mx:VBox id="multiplePanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
     <potlatch2:TagGrid id="multiAdvancedTagGrid" width="100%" height="75%" />
        <mx:HBox horizontalAlign="right" width="100%">
          <mx:LinkButton label="Delete" click="multiAdvancedTagGrid.removeTag()" enabled="{multiAdvancedTagGrid.selectedItem != null? true : false}"/>
          <mx:LinkButton label="Add" click="multiAdvancedTagGrid.addNewTag()" />
        </mx:HBox>
+
+    <mx:DataGrid editable="true" width="100%" height="25%" id="multiRelationsGrid"
+        doubleClickEnabled="true"
+        itemDoubleClick="editRelation(multiRelationsGrid.selectedItem.id)"
+        doubleClick="if (event.target.parent==multiRelationsGrid) { addToRelation(); }">
+        <mx:columns>
+            <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:DataGridColumn>
+        </mx:columns>
+    </mx:DataGrid>
+    <mx:HBox horizontalAlign="right" width="100%">
+      <mx:LinkButton label="Remove from" click="removeFromRelation(multiRelationsGrid.selectedItem.id)"
+                      enabled="{multiRelationsGrid.selectedItem != null? true : false}"/>
+      <mx:LinkButton label="Add to" click="addToRelation()"/>
+    </mx:HBox>
+
   </mx:VBox>
 
+  <!-- Multiple selection but items cannot be edited -->
+
   <mx:VBox id="multipleInvalidPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
        <mx:Text id="multipleInvalidPanelText" text="You have selected multiple items." width="100%" styleName="helpInfo" />
   </mx:VBox>
 
+  <!-- Generic marker panel -->
+
   <mx:VBox id="markerPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
     <sidepanel:MarkerPanel id="markerPanelContents" width="100%"/>
   </mx:VBox>
 
+  <!-- Bug editing panel -->
+
   <mx:VBox id="bugPanel" width="100%" height="100%" horizontalScrollPolicy="off" styleName="dndPanelVbox">
     <sidepanel:BugPanel id="bugPanelContents" width="100%"/>
   </mx:VBox>
 
                if (selectedEntity!=firstSelected && selectedEntity!=null) {
                        selectedEntity.removeEventListener(Connection.TAG_CHANGED, tagChanged);
+                       selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
+                       selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
                }
 
                if (entities.length==0) {
                }
 
                removeRelationListeners();
-               if ( selectedEntity != null ) {
-                       selectedEntity.removeEventListener(Connection.ADDED_TO_RELATION, addedToRelation);
-                       selectedEntity.removeEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation);
-               }
 
                if ( entity == null ) {
                        relationsGrid.dataProvider = null;
 
          private function setupMultiAdvanced(entity:Entity):void {
                multiAdvancedTagGrid.init(entity);
+               resetRelationsGrid(entity);
+               entity.addEventListener(Connection.ADDED_TO_RELATION, addedToRelation, false, 0, true);
+               entity.addEventListener(Connection.REMOVED_FROM_RELATION, removedFromRelation, false, 0, true);
       }
 
          public function addNewTag():void {
               rel.removeEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
           }
           listeningToRelations = [];
-          relationsGrid.removeEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged);
+          if (relationsGrid) relationsGrid.removeEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged);
+          if (multiRelationsGrid) multiRelationsGrid.removeEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged);
       }
 
       private function resetRelationsGrid(entity:Entity):void {
           removeRelationListeners();
-          var relations:Array = [];
-          for each( var rel:Relation in entity.parentRelations ) {
-              for each( var memberIndex:int in rel.findEntityMemberIndexes(entity)) {
-                  var props:Object = {};
-                  props["relation"] = rel;
-                  props["id"] = rel.id;
-                  props["index"] = memberIndex;
-                  props["role"] = rel.getMember(memberIndex).role;
-                  props["description"] = rel.getDescription();
-                  props["id_idx"] = rel.id + "/"+memberIndex;
-
-                  relations.push(props);
-              }
 
-              rel.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
-              rel.addEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
-              rel.addEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
-              listeningToRelations.push(rel);
+          var instance:DataGrid=relationsGrid;
+          if (entity is EntityCollection) instance=multiRelationsGrid;
+          var memberships:Array = entity.getRelationMemberships();
+          for each (var m:Object in memberships) {
+              m.relation.addEventListener(Connection.TAG_CHANGED, relationTagChanged);
+              m.relation.addEventListener(Connection.RELATION_MEMBER_ADDED, entityRelationMemberChanged);
+              m.relation.addEventListener(Connection.RELATION_MEMBER_REMOVED, entityRelationMemberChanged);
+              listeningToRelations.push(m.relation);
           }
-          relationsGrid.dataProvider = relations;
-          relationsGrid.addEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged, false, -100);
+          instance.dataProvider = memberships;
+          instance.addEventListener(DataGridEvent.ITEM_EDIT_END, relationRoleChanged, false, -100);
       }
 
       private function relationRoleChanged(event:DataGridEvent):void {
                var props:Object=relations[event.rowIndex];
 
                var relation:Relation=props['relation'];
-               var index:uint=props['index'];
                var newRole:String=event.itemRenderer.data['role'];
 
-               relation.setMember(index, new RelationMember(selectedEntity,newRole));
+               if (selectedEntity is EntityCollection) {
+                       for each (var entity:Entity in EntityCollection(selectedEntity).entities) {
+                               var indexes:Array=relation.findEntityMemberIndexes(entity);
+                               for each (var index:int in indexes) {
+                                       relation.setMember(index, new RelationMember(entity,newRole));
+                               }
+                       }
+               } else {
+                       relation.setMember(props['index'], new RelationMember(selectedEntity,newRole));
+               }
       }
 
       private function relationTagChanged(event:TagEvent):void {
           new RelationSelectPanel().init(selectedEntity,new Object());
       }
 
-      private function removeFromRelation(id:Number, index:int):void {
-          Connection.getConnectionInstance().getRelation(id).removeMemberByIndex(index, MainUndoStack.getGlobalStack().addAction);
+      private function removeFromRelation(id:Number, index:int=-1):void {
+               var rel:Relation=Connection.getConnectionInstance().getRelation(id);
+               if (index>-1) {
+                       rel.removeMemberByIndex(index, MainUndoStack.getGlobalStack().addAction);
+               } else if (selectedEntity is EntityCollection) {
+                       var undo:CompositeUndoableAction=new CompositeUndoableAction("Remove selection from relations");
+                       for each (var e:Entity in EntityCollection(selectedEntity).entities) rel.removeMember(e,undo.push);
+                       MainUndoStack.getGlobalStack().addAction(undo);
+               }
       }
 
       public function initFeatureBox():void {