Author: mes
Date: 2012-01-09 15:18:32 -0800 (Mon, 09 Jan 2012)
New Revision: 27955

Added:
   
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/GravityTracker.java
   core3/impl/trunk/editor-impl/src/test/
   core3/impl/trunk/editor-impl/src/test/java/
   core3/impl/trunk/editor-impl/src/test/java/org/
   core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/
   core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/
   core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/internal/
   
core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/internal/GravityTrackerTest.java
Modified:
   
core3/impl/trunk/ding-impl/ding-presentation-impl/src/main/java/org/cytoscape/ding/CyActivator.java
   core3/impl/trunk/editor-impl/pom.xml
   
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/CyActivator.java
   
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/gui/ShapePalette.java
Log:
added gravity based ordering to editor GraphicalEntities

Modified: 
core3/impl/trunk/ding-impl/ding-presentation-impl/src/main/java/org/cytoscape/ding/CyActivator.java
===================================================================
--- 
core3/impl/trunk/ding-impl/ding-presentation-impl/src/main/java/org/cytoscape/ding/CyActivator.java
 2012-01-09 21:53:23 UTC (rev 27954)
+++ 
core3/impl/trunk/ding-impl/ding-presentation-impl/src/main/java/org/cytoscape/ding/CyActivator.java
 2012-01-09 23:18:32 UTC (rev 27955)
@@ -161,11 +161,20 @@
                
registerService(bc,dingNetworkViewFactory,CyNetworkViewFactory.class, 
dingNetworkViewFactoryServiceProps);
 
 
-               
registerService(bc,imageGraphicalEntity,GraphicalEntity.class,new Properties());
-               
registerService(bc,shapeGraphicalEntity,GraphicalEntity.class,new Properties());
+               final Properties imageGraphicalEntityProps = new Properties();
+               imageGraphicalEntityProps.setProperty("editorGravity","12.0"); 
+               
registerService(bc,imageGraphicalEntity,GraphicalEntity.class,imageGraphicalEntityProps);
+
+               final Properties shapeGraphicalEntityProps = new Properties();
+               shapeGraphicalEntityProps.setProperty("editorGravity","10.0"); 
+               
registerService(bc,shapeGraphicalEntity,GraphicalEntity.class,shapeGraphicalEntityProps);
+
+               final Properties textGraphicalEntityProps = new Properties();
+               textGraphicalEntityProps.setProperty("editorGravity","11.0"); 
+               
registerService(bc,textGraphicalEntity,GraphicalEntity.class,textGraphicalEntityProps);
+
+//             
registerService(bc,boundedGraphicalEntity,GraphicalEntity.class,new 
Properties());
 //             
registerService(bc,arrowGraphicalEntity,GraphicalEntity.class,new Properties());
-               
registerService(bc,textGraphicalEntity,GraphicalEntity.class,new Properties());
-//             
registerService(bc,boundedGraphicalEntity,GraphicalEntity.class,new 
Properties());
 
                Properties dropImageTaskFactoryProps = new Properties();
                
dropImageTaskFactoryProps.setProperty("preferredAction","Image");

Modified: core3/impl/trunk/editor-impl/pom.xml
===================================================================
--- core3/impl/trunk/editor-impl/pom.xml        2012-01-09 21:53:23 UTC (rev 
27954)
+++ core3/impl/trunk/editor-impl/pom.xml        2012-01-09 23:18:32 UTC (rev 
27955)
@@ -107,6 +107,11 @@
                        <groupId>org.cytoscape</groupId>
                        <artifactId>core-task-api</artifactId>
                </dependency>
+               <dependency>
+                       <groupId>junit</groupId>
+                       <artifactId>junit</artifactId>
+                       <scope>test</scope>
+               </dependency>
 
        </dependencies>
 </project>

Modified: 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/CyActivator.java
===================================================================
--- 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/CyActivator.java
   2012-01-09 21:53:23 UTC (rev 27954)
+++ 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/CyActivator.java
   2012-01-09 23:18:32 UTC (rev 27955)
@@ -115,10 +115,18 @@
                
registerService(bc,currentNetworkViewListener,SetCurrentNetworkViewListener.class,
 new Properties());
                
registerService(bc,editorPanelSelectedListener,CytoPanelComponentSelectedListener.class,
 new Properties());
 
-               registerService(bc,nodeEntity,GraphicalEntity.class, new 
Properties());
-               registerService(bc,edgeEntity,GraphicalEntity.class, new 
Properties());
-               registerService(bc,netEntity,GraphicalEntity.class, new 
Properties());
+               Properties nodeEntityProps = new Properties();
+               nodeEntityProps.setProperty("editorGravity","1.0");
+               registerService(bc,nodeEntity,GraphicalEntity.class, 
nodeEntityProps);
 
+               Properties edgeEntityProps = new Properties();
+               edgeEntityProps.setProperty("editorGravity","2.0");
+               registerService(bc,edgeEntity,GraphicalEntity.class, 
edgeEntityProps);
+
+               Properties netEntityProps = new Properties();
+               netEntityProps.setProperty("editorGravity","3.0");
+               registerService(bc,netEntity,GraphicalEntity.class, 
netEntityProps);
+
                
registerServiceListener(bc,shapePalette,"addGraphicalEntity","removeGraphicalEntity",GraphicalEntity.class);
 
        }

Added: 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/GravityTracker.java
===================================================================
--- 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/GravityTracker.java
                                (rev 0)
+++ 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/GravityTracker.java
        2012-01-09 23:18:32 UTC (rev 27955)
@@ -0,0 +1,55 @@
+package org.cytoscape.editor.internal;
+
+import java.util.Map;
+import java.util.HashMap;
+import java.util.List;
+import java.util.ArrayList;
+
+public class GravityTracker<S> {
+
+       private final Map<S,Double> gravityMap = new HashMap<S,Double>();
+       private final List<S> sortedKeys = new ArrayList<S>(); 
+
+       public int add(S key, double value) {
+               if ( key == null )
+                       throw new NullPointerException("key cannot be null");
+
+               if ( gravityMap.containsKey(key) )
+                       throw new IllegalArgumentException("key: '" + 
key.toString() + "' already exists - duplicate keys not allowed");
+               gravityMap.put(key,value);
+               int index = getIndex(key); 
+               sortedKeys.add(index,key);
+               return index; 
+       }
+
+       public int getIndex(S key) {
+               if ( key == null )
+                       throw new NullPointerException("key cannot be null");
+
+               Double gravity = gravityMap.get(key);
+               if ( gravity == null )
+                       return sortedKeys.size();
+
+               double newGravity = gravity.doubleValue();
+
+               int i = 0;
+
+               for (S k : sortedKeys ) {
+                       if ( k.equals(key) )
+                               break;
+                       if ( newGravity < gravityMap.get(k) )
+                               break;
+                       i++;
+               }
+
+               return i;
+       }
+
+       public void remove(S key) {
+               Double d = gravityMap.remove(key);
+
+               if ( d != null )
+                       sortedKeys.remove(key);
+       }
+}
+

Modified: 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/gui/ShapePalette.java
===================================================================
--- 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/gui/ShapePalette.java
      2012-01-09 21:53:23 UTC (rev 27954)
+++ 
core3/impl/trunk/editor-impl/src/main/java/org/cytoscape/editor/internal/gui/ShapePalette.java
      2012-01-09 23:18:32 UTC (rev 27955)
@@ -77,6 +77,8 @@
 import org.cytoscape.application.swing.CySwingApplication;
 import org.cytoscape.dnd.GraphicalEntity;
 
+import org.cytoscape.editor.internal.GravityTracker;
+
 /**
  *
  * The <b>ShapePalette</b> class implements a palette from which the user 
drags and drops shapes onto the canvas
@@ -111,6 +113,8 @@
 
        private final CySwingApplication app;
 
+       private final GravityTracker<BasicCytoShapeEntity> gravityTracker = new 
GravityTracker<BasicCytoShapeEntity>();
+
        /**
         * Creates a new ShapePalette object.
         */
@@ -218,12 +222,30 @@
        }
 
        public void addGraphicalEntity(GraphicalEntity cytoShape, Map props) {
-        shapeMap.put(cytoShape.getTitle(), new 
BasicCytoShapeEntity(app,cytoShape));
-        shapePane.add( shapeMap.get( cytoShape.getTitle() ) );
+               BasicCytoShapeEntity shape = new 
BasicCytoShapeEntity(app,cytoShape);
+        shapeMap.put(cytoShape.getTitle(), shape);
+               int index = gravityTracker.add( shape, 
getDouble((String)(props.get("editorGravity"))) );
+               System.out.println("adding " + cytoShape.getTitle() + " at 
index: " + index + " with gravity " + props.get("editorGravity"));
+        shapePane.add( shape, index );
     }
 
+       private double getDouble(String s) {
+               if ( s == null )
+                       return 100.0;
+
+               double d;
+               try {
+                       d = Double.parseDouble(s);
+               } catch (Exception e) {
+                       d = 100.0;
+               }
+               return d;
+       }
+
        public void removeGraphicalEntity(GraphicalEntity cytoShape, Map props) 
{
-               shapePane.remove( shapeMap.remove(cytoShape.getTitle()) );
+               BasicCytoShapeEntity shape = 
shapeMap.remove(cytoShape.getTitle());
+               shapePane.remove( shape );
+               gravityTracker.remove( shape );
     }
 
        /**

Added: 
core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/internal/GravityTrackerTest.java
===================================================================
--- 
core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/internal/GravityTrackerTest.java
                            (rev 0)
+++ 
core3/impl/trunk/editor-impl/src/test/java/org/cytoscape/editor/internal/GravityTrackerTest.java
    2012-01-09 23:18:32 UTC (rev 27955)
@@ -0,0 +1,168 @@
+
+
+/*
+ Copyright (c) 2008, The Cytoscape Consortium (www.cytoscape.org)
+
+ The Cytoscape Consortium is:
+ - Institute for Systems Biology
+ - University of California San Diego
+ - Memorial Sloan-Kettering Cancer Center
+ - Institut Pasteur
+ - Agilent Technologies
+
+ This library is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as published
+ by the Free Software Foundation; either version 2.1 of the License, or
+ any later version.
+
+ This library is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF
+ MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  The software and
+ documentation provided hereunder is on an "as is" basis, and the
+ Institute for Systems Biology and the Whitehead Institute
+ have no obligations to provide maintenance, support,
+ updates, enhancements or modifications.  In no event shall the
+ Institute for Systems Biology and the Whitehead Institute
+ be liable to any party for direct, indirect, special,
+ incidental or consequential damages, including lost profits, arising
+ out of the use of this software and its documentation, even if the
+ Institute for Systems Biology and the Whitehead Institute
+ have been advised of the possibility of such damage.  See
+ the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public License
+ along with this library; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+*/
+
+package org.cytoscape.editor.internal;
+
+
+import org.junit.Test;
+import static org.junit.Assert.*;
+
+
+public class GravityTrackerTest { 
+
+       @Test
+       public void testEmpty() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               int index = gt.add("homer",1.0);
+               assertEquals(0,index);
+               assertEquals(0,gt.getIndex("homer"));
+       }
+
+       @Test
+       public void testNonExistent() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               assertEquals(0,gt.getIndex("mr. burns"));
+       }
+
+       @Test
+       public void testNonExistent2() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               assertEquals(2,gt.getIndex("mr. burns"));
+       }
+
+       @Test
+       public void testEnd() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               gt.add("bart",3.0);
+               gt.add("lisa",4.0);
+               int index = gt.add("maggie",5.0);
+
+               assertEquals(4,index);
+               assertEquals(4,gt.getIndex("maggie"));
+       }
+
+       @Test
+       public void testBegin() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("marge",2.0);
+               gt.add("bart",3.0);
+               gt.add("lisa",4.0);
+               gt.add("maggie",5.0);
+
+               int index = gt.add("homer",1.0);
+
+               assertEquals(0,index);
+               assertEquals(0,gt.getIndex("homer"));
+       }
+
+       @Test
+       public void testMiddle() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               gt.add("lisa",4.0);
+               gt.add("maggie",5.0);
+
+               int index = gt.add("bart",3.0);
+
+               assertEquals(2,index);
+               assertEquals(2,gt.getIndex("bart"));
+       }
+
+       @Test
+       public void testEqual() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               gt.add("bart",3.0);
+               gt.add("lisa",4.0);
+               gt.add("maggie",5.0);
+
+               int index = gt.add("smithers",3.0);
+
+               assertEquals(3,index);
+               assertEquals(3,gt.getIndex("smithers"));
+       }
+
+       @Test(expected=IllegalArgumentException.class)
+       public void testEqual2() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               gt.add("bart",3.0);
+               gt.add("lisa",4.0);
+               gt.add("maggie",5.0);
+
+               gt.add("bart",15.0);
+       }
+
+       @Test
+       public void testRemove() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add("homer",1.0);
+               gt.add("marge",2.0);
+               gt.add("bart",3.0);
+               gt.add("lisa",4.0);
+               gt.add("maggie",5.0);
+
+               assertEquals(4,gt.getIndex("maggie"));
+
+               gt.remove("homer");
+
+               assertEquals(3,gt.getIndex("maggie"));
+
+               int index = gt.add("lenny",6.0);
+               assertEquals(4,index);
+               assertEquals(4,gt.getIndex("lenny"));
+       }
+
+       @Test(expected=NullPointerException.class)
+       public void testNullAdd() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.add(null,1.0);
+       }
+
+       @Test(expected=NullPointerException.class)
+       public void testNullGetIndex() {
+               GravityTracker<String> gt = new GravityTracker<String>();
+               gt.getIndex(null);
+       }
+}

-- 
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