Merge commit '2.0' into history
[potlatch2.git] / net / systemeD / potlatch2 / RelationSelectPanel.mxml
index 93193768833fa9474ccf5697d6e1114949faaf80..37a184f7a85c97af9f2b9e2d231e1d7600de0c39 100644 (file)
         private var conn:Connection;
         private var relationList:Array;
         private var entity:Entity;
-               private var tags:Object;
+               private var defaulttags:Object;
 
+        /** Initialise panel used to add and remove relations from an entity.
+        *   @param e Entity that is being edited.
+        *   @param t Object containing tag match patterns as properties, for the kind of relation sought.
+        */
         public function init(e:Entity, t:Object):void {
           entity = e;
-                 tags = t;
+          defaulttags = {};
+          for (var k:String in t) {  
+            //tagmatches[k]=t[k];// deep copy match pattern tags
+            defaulttags[k]=t[k][0];  // route=[hiking,foot] -> route=hiking
+          }    
           PopUpManager.addPopUp(this, Application(Application.application), true);
           PopUpManager.centerPopUp(this);
     
           var titles:Array = [];
           conn = Connection.getConnectionInstance();
-          relationList = conn.getMatchingRelationIDs(tags);
+          relationList = conn.getMatchingRelationIDs(t);
           if (relationList.length == 0) {
             warning.text = "No relations available";
           } else {
         }
         
         public function updateEntityAndClose():void {
-          var relation:Relation = conn.getRelation(relationSelector.selectedItem.relId);
-          relation.appendMember(new RelationMember(entity, ''));
-          PopUpManager.removePopUp(this);
+                       var undo:CompositeUndoableAction = new CompositeUndoableAction("Add to relation");
+                       for each (var relObject:Object in relationSelector.selectedItems) {
+                               var relation:Relation = conn.getRelation(relObject.relId);
+                               if (entity is EntityCollection) {
+                                       for each (var e:Entity in EntityCollection(entity).entities) {
+                                               if (relation.findEntityMemberIndex(e)==-1) {
+                                                       relation.appendMember(new RelationMember(e, ''), undo.push);
+                                               }
+                                       }
+                               } else {
+                                       relation.appendMember(new RelationMember(entity, ''), undo.push);
+                               }
+                       }
+                       MainUndoStack.getGlobalStack().addAction(undo);
+                       PopUpManager.removePopUp(this);
         }
         
         public function closeAndNewRelation():void {
-          var relation:Relation = conn.createRelation(tags, [new RelationMember(entity, '')],
-              MainUndoStack.getGlobalStack().addAction)
-          PopUpManager.removePopUp(this);
-          var panel:RelationEditorPanel = RelationEditorPanel(
-              PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
-          panel.setRelation(relation);
-          PopUpManager.centerPopUp(panel);
+                       var members:Array=[];
+                       if (entity is EntityCollection) {
+                               for each (var e:Entity in EntityCollection(entity).entities) {
+                                       members.push(new RelationMember(e, ''));
+                               }
+                       } else members.push(new RelationMember(entity, ''));
+                       var relation:Relation = conn.createRelation(defaulttags, members, MainUndoStack.getGlobalStack().addAction);
+
+                       PopUpManager.removePopUp(this);
+                       var panel:RelationEditorPanel = RelationEditorPanel(
+                           PopUpManager.createPopUp(Application(Application.application), RelationEditorPanel, true));
+                       panel.setRelation(relation);
+                       PopUpManager.centerPopUp(panel);
         }
         
     ]]></mx:Script> 
     <mx:Text id="warning" text="" />
-    <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on"> 
+    <mx:List width="100%" height="100%" id="relationSelector" verticalScrollPolicy="on" allowMultipleSelection="true" 
     </mx:List>
     <mx:ControlBar>
-      <mx:Button label="New Relation..." click="closeAndNewRelation();"/>
+      <mx:Button label="New Relation..." click="closeAndNewRelation();" styleName="titleWindowButton" />
       <mx:Spacer width="100%"/>
-      <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItem != null? true : false}"/>
+      <mx:Button label="Select" click="updateEntityAndClose();" enabled="{relationSelector.selectedItems.length>0}" styleName="titleWindowButton" />
     </mx:ControlBar>
 </mx:TitleWindow>