Author: clopes
Date: 2011-09-15 12:48:25 -0700 (Thu, 15 Sep 2011)
New Revision: 26816

Modified:
   cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Groups.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/ExternalMediator.as
   cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/GraphMediator.as
Log:
Initial implementation of #2462: Add multiple nodes/edges.

Modified: cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js    
2011-09-15 19:46:23 UTC (rev 26815)
+++ cytoscapeweb/trunk/cytoscapeweb/html-template/js/cytoscapeweb.js    
2011-09-15 19:48:25 UTC (rev 26816)
@@ -651,6 +651,33 @@
         },
         
         /**
+         * TODO: DOCUMENT ME
+         * 
+         * @example
+         * // 1. Add two nodes with no data (IDs will be created 
automatically):
+         * var nodesArray = [ { group: "nodes", x: 10, y: 35 },
+         *                    { group: "nodes", x: 20, y: 70 } ];
+         * var nodes = vis.addElements(nodesArray, true);
+         * 
+         * // 2. Add edges and nodes altogether:
+         * var array = [ { group: "nodes", x: 10, y: 35, data: { id: "n01" } },
+         *               { group: "nodes", x: 20, y: 70, data: { id: "n02" } },
+         *               { group: "edges", data: { source: "n01", target: 
"n02" } } ];
+         * vis.addElements(array, true);
+         */
+        addElements: function(/*items, updateVisualMappers*/) {
+            var items, updateVisualMappers = false;
+            if (arguments.length > 0 && this._typeof(arguments[0]) === 
"array") {
+               items = arguments[0];
+            }
+               if (items == null) { throw("The 'items' object is mandatory."); 
}
+            if (arguments.length > 1 && typeof arguments[1] === "boolean") {
+               updateVisualMappers = arguments[1];
+            }
+            return this.swf().addElements(items, updateVisualMappers);
+        },
+        
+        /**
          * <p>Create a new node and add it to the network view.<p>
          * <p>If the node <code>id</code> is not specified, Cytoscape Web 
creates a new one automatically.</p>
          * <p>If you try to add data attributes that have not been previously 
defined,

Modified: cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Groups.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Groups.as 
2011-09-15 19:46:23 UTC (rev 26815)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/util/Groups.as 
2011-09-15 19:48:25 UTC (rev 26816)
@@ -32,8 +32,10 @@
     import flare.vis.data.EdgeSprite;
     import flare.vis.data.NodeSprite;
     
+    import mx.utils.StringUtil;
     
     
+    
     /**
      * Abstract utility class defining constants for the groups of network 
elements.
      */
@@ -70,6 +72,14 @@
             return gr;
         }
         
+        public static function parse(gr:String):String {
+            if (gr != null) gr = StringUtil.trim(gr.toLowerCase());
+            if (gr === NODES) return NODES;
+            if (gr === EDGES) return EDGES;
+            
+            return null;
+        }
+        
         // ========[ PRIVATE METHODS 
]==============================================================
 
     }

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/ExternalMediator.as
===================================================================
--- 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/ExternalMediator.as   
    2011-09-15 19:46:23 UTC (rev 26815)
+++ 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/ExternalMediator.as   
    2011-09-15 19:48:25 UTC (rev 26816)
@@ -32,6 +32,7 @@
     
     import flare.data.DataSchema;
     import flare.data.DataSet;
+    import flare.vis.data.DataSprite;
     import flare.vis.data.EdgeSprite;
     import flare.vis.data.NodeSprite;
     
@@ -47,6 +48,7 @@
     import org.cytoscapeweb.model.data.GraphicsDataTable;
     import org.cytoscapeweb.model.data.VisualStyleBypassVO;
     import org.cytoscapeweb.model.data.VisualStyleVO;
+    import org.cytoscapeweb.model.error.CWError;
     import org.cytoscapeweb.model.methods.error;
     import org.cytoscapeweb.util.ExternalFunctions;
     import org.cytoscapeweb.util.Groups;
@@ -355,6 +357,70 @@
             return JSON.encode(obj);
         }
         
+        private function addElements(items:Array, 
updateVisualMappers:Boolean=false):Array {
+               var newAll:Array = [], newNodes:Array = [], newEdges:Array = 
[], ret:Array = [];
+               var edgesToAdd:Array = [];
+               var gr:String, newElement:DataSprite, o:Object;
+               
+               try {                
+                // Create element:
+                for each (o in items) {
+                       gr = Groups.parse(o.group);
+                       
+                       if (gr == null)
+                          throw new CWError("The 'group' field of the new 
element  must be either '" + 
+                                            Groups.NODES + "' or '" + 
Groups.EDGES + "'.");
+                       
+                       if (gr === Groups.NODES) {
+                               // Create nodes first!
+                           newElement = graphProxy.addNode(o.data);
+                               // Position it:
+                               var p:Point = new Point(o.x, o.y);
+                               p = graphMediator.vis.globalToLocal(p);
+                               newElement.x = p.x;
+                               newElement.y = p.y;
+                               
+                               newNodes.push(newElement);
+                           } else {
+                               // Just store the edge,
+                               // so it can be added after all new nodes have 
been created first:
+                               edgesToAdd.push(o);
+                           }
+                }
+                
+                // Now it is safe to add the edges:
+                for each (o in edgesToAdd) {
+                       newElement = graphProxy.addEdge(o.data);
+                       newEdges.push(newElement);
+                }
+                
+                // Set listeners, styles, etc:
+                graphMediator.initialize(Groups.NODES, newNodes);
+                graphMediator.initialize(Groups.EDGES, newEdges);
+                
+                // Add the edges to the array of all new elements:
+                newAll = newNodes.concat(newEdges);
+                
+                // Do it before converting the Nodes/Edges to plain objects,
+                // in order to get the rendered visual properties:
+                if (updateVisualMappers) 
sendNotification(ApplicationFacade.GRAPH_DATA_CHANGED);
+                
+                // Finally convert the items to a plain objects that can be 
returned:
+                for each (newElement in newAll) {
+                    o = ExternalObjectConverter.toExtElement(newElement);
+                    ret.push(o);
+                }
+            } catch (err:Error) {
+                trace("[ERROR]: addElements: " + err.getStackTrace());
+                error(err);
+            } finally {
+               // Rollback--delete any new item:
+               // TODO: 
+            }
+               
+               return ret;
+        }
+        
         private function addNode(x:Number, y:Number, 
                                  data:Object, 
updateVisualMappers:Boolean=false):Object {
             var o:Object;
@@ -485,7 +551,7 @@
                                         "getLayout", "applyLayout", 
                                         "setVisualStyle", "getVisualStyle", 
                                         "getVisualStyleBypass", 
"setVisualStyleBypass",
-                                        "addNode", "addEdge", "removeElements",
+                                        "addElements", "addNode", "addEdge", 
"removeElements",
                                         "getDataSchema", "addDataField", 
"removeDataField", "updateData",
                                         "getNetworkModel", "getNetworkAsText", 
"getNetworkAsImage", 
                                         "exportNetwork" ];

Modified: 
cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/GraphMediator.as
===================================================================
--- cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/GraphMediator.as  
2011-09-15 19:46:23 UTC (rev 26815)
+++ cytoscapeweb/trunk/cytoscapeweb/src/org/cytoscapeweb/view/GraphMediator.as  
2011-09-15 19:48:25 UTC (rev 26816)
@@ -50,6 +50,7 @@
     import org.cytoscapeweb.events.GraphViewEvent;
     import org.cytoscapeweb.model.data.VisualStyleBypassVO;
     import org.cytoscapeweb.model.data.VisualStyleVO;
+    import org.cytoscapeweb.model.error.CWError;
     import org.cytoscapeweb.util.Edges;
     import org.cytoscapeweb.util.ExternalFunctions;
     import org.cytoscapeweb.util.Groups;
@@ -273,8 +274,11 @@
             else if (ds is EdgeSprite) graphView.resetEdge(EdgeSprite(ds));
         }
         
-        public function initialize(gr:String, items:Array):void { 
+        public function initialize(gr:String, items:Array):void {
             // Set properties:
+            if (gr !== Groups.NODES && gr !== Groups.EDGES)
+                throw new CWError("'gr' must be '" + Groups.NODES + "' or '" + 
Groups.EDGES + "' only.");
+            
             var props:Object = gr === Groups.NODES ? Nodes.properties : 
Edges.properties;
             
             for (var name:String in props) {

-- 
You received this message because you are subscribed to the Google Groups 
"cytoscape-cvs" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/cytoscape-cvs?hl=en.

Reply via email to