Author: pwang
Date: 2010-09-24 14:45:17 -0700 (Fri, 24 Sep 2010)
New Revision: 22051

Added:
   
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayoutTask.java
Modified:
   
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayout.java
Log:
Refactored

Modified: 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayout.java
===================================================================
--- 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayout.java
       2010-09-24 21:24:43 UTC (rev 22050)
+++ 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayout.java
       2010-09-24 21:45:17 UTC (rev 22051)
@@ -40,17 +40,20 @@
 import java.util.Map;
 import java.util.Set;
 
-import org.cytoscape.view.layout.AbstractGraphPartition;
+//import org.cytoscape.view.layout.AbstractGraphPartition;
+import org.cytoscape.view.layout.AbstractLayout;
 import org.cytoscape.view.layout.LayoutNode;
-import org.cytoscape.view.layout.LayoutPartition;
+//import org.cytoscape.view.layout.LayoutPartition;
+import org.cytoscape.work.TaskIterator;
 import org.cytoscape.work.Tunable;
+import org.cytoscape.work.TunableValidator;
 import org.cytoscape.work.undo.UndoSupport;
 
 
 /**
  *
  */
-public class AttributeCircleLayout extends AbstractGraphPartition {
+public class AttributeCircleLayout extends AbstractLayout implements 
TunableValidator {
        @Tunable(description="The attribute to use for the layout")
        public String attribute = null;
        @Tunable(description="The attribute namespace to use for the layout")
@@ -77,6 +80,21 @@
                initialize(true);
        }
 
+       
+
+       //TODO 
+       public boolean tunablesAreValid(final Appendable errMsg) {
+               return true;
+       }
+
+       public TaskIterator getTaskIterator() {
+               
+               return new TaskIterator(new 
AttributeCircleLayoutTask(networkView, getName(), selectedOnly, staticNodes,
+                               
attribute,namespace,spacing,supportNodeAttributes));
+       }
+
+       
+       
        /**
         *  DOCUMENT ME!
         *
@@ -158,88 +176,4 @@
                        return "attribute-circle";
        }
 
-       /**
-        *  DOCUMENT ME!
-        *
-        * @param partition DOCUMENT ME!
-        */
-       public void layoutPartion(LayoutPartition partition) {
-               // just add the unlocked nodes
-               List<LayoutNode> nodes = new ArrayList<LayoutNode>();
-               for ( LayoutNode ln : partition.getNodeList() ) {
-                       if ( !ln.isLocked() ) {
-                               nodes.add(ln);
-                       }
-               }
-
-               int count = nodes.size(); 
-               int r = (int) Math.sqrt(count);
-               r *= spacing;
-
-               if (this.attribute != null && count > 0) {
-                       Class<?> klass = 
nodes.get(0).getNode().getCyRow(namespace).contains(attribute); 
-                       if (klass != null && 
Comparable.class.isAssignableFrom(klass)){
-                               // FIXME: I assume this would be better, but 
get type errors if I try:
-                               //Class<Comparable<?>> kasted = 
(Class<Comparable<?>>) klass;
-                               //Collections.sort(nodes, new 
AttributeComparator<Comparable<?>>(kasted));
-                               Collections.sort(nodes, new 
AttributeComparator(klass));
-                       } else {
-                               /* FIXME Error! */
-                       }
-               }
-
-               // Compute angle step
-               double phi = (2 * Math.PI) / count; 
-
-               partition.resetNodes(); // We want to figure out our mins & 
maxes anew
-                                       // Arrange vertices in a circle
-
-               for (int i = 0; i < count; i++) {
-                       LayoutNode node = (LayoutNode) nodes.get(i);
-                       double x = r + (r * Math.sin(i * phi));
-                       double y = r + (r * Math.cos(i * phi));
-                       node.setX(x);
-                       node.setY(y);
-                       partition.moveNodeToLocation(node);
-               }
-       }
-       private class AttributeComparator<T extends Comparable<T>> implements 
Comparator<LayoutNode> {
-               Class<T> klass;
-               private AttributeComparator(Class<T> klass) {
-                       this.klass = klass;
-               }
-               
-               public int compare(LayoutNode o1, LayoutNode o2) {
-                       T v1 = o1.getNode().getCyRow(namespace).get(attribute, 
klass);
-                       T v2 = o2.getNode().getCyRow(namespace).get(attribute, 
klass);
-                       if (String.class.isAssignableFrom(klass)){ // i.e. if 
klass _is_ String.class
-                               String s1 = String.class.cast(v1);
-                               String s2 = String.class.cast(v2);
-                               if ((s1 != null) && (s2 != null))
-                                       return s1.compareToIgnoreCase(s2);
-                               else if ((s1 == null) && (s2 != null))
-                                       return -1;
-                               else if ((s1 == null) && (s2 == null))
-                                       return 0;
-                               else if ((s1 != null) && (s2 == null))
-                                       return 1;
-                               
-                       } else {
-                               return compareEvenIfNull(v1, v2);
-                       }
-
-                       return 0; // can't happen anyway
-               }
-
-               public int compareEvenIfNull(T v1, T v2){
-                       if ((v1 != null) && (v2 != null))
-                               return v1.compareTo(v2);
-                       else if ((v1 == null) && (v2 != null))
-                               return -1;
-                       else if ((v1 == null) && (v2 == null))
-                               return 0;
-                       else // if ((v1 != null) && (v2 == null)) // this is 
the only possibility
-                               return 1;
-               }
-       }
 }

Added: 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayoutTask.java
===================================================================
--- 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayoutTask.java
                           (rev 0)
+++ 
core3/layout-cytoscape-impl/trunk/src/main/java/csplugins/layout/algorithms/graphPartition/AttributeCircleLayoutTask.java
   2010-09-24 21:45:17 UTC (rev 22051)
@@ -0,0 +1,128 @@
+package csplugins.layout.algorithms.graphPartition;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.Set;
+
+import org.cytoscape.model.CyNode;
+import org.cytoscape.view.layout.AbstractGraphPartition;
+import org.cytoscape.view.layout.LayoutNode;
+import org.cytoscape.view.layout.LayoutPartition;
+import org.cytoscape.view.model.CyNetworkView;
+import org.cytoscape.view.model.View;
+import org.cytoscape.work.Tunable;
+
+public class AttributeCircleLayoutTask extends AbstractGraphPartition {
+
+       public String attribute = null;
+       public String namespace = null;
+       public double spacing = 100.0;
+       boolean supportNodeAttributes = true;
+
+       
+       /**
+        * Creates a new ForceDirectedLayout object.
+        */
+       public AttributeCircleLayoutTask(final CyNetworkView networkView, final 
String name,
+                                 final boolean selectedOnly, final 
Set<View<CyNode>> staticNodes,
+                                 final String attribute,final String 
namespace,final double spacing,final boolean supportNodeAttributes)
+       {
+       
+               super(networkView, name, selectedOnly, staticNodes);
+               
+               this.attribute = attribute;
+               this.namespace = namespace;
+               this.spacing = spacing;
+               this.supportNodeAttributes = supportNodeAttributes;
+       }
+       
+       
+       /**
+        *  DOCUMENT ME!
+        *
+        * @param partition DOCUMENT ME!
+        */
+       public void layoutPartion(LayoutPartition partition) {
+               // just add the unlocked nodes
+               List<LayoutNode> nodes = new ArrayList<LayoutNode>();
+               for ( LayoutNode ln : partition.getNodeList() ) {
+                       if ( !ln.isLocked() ) {
+                               nodes.add(ln);
+                       }
+               }
+
+               int count = nodes.size(); 
+               int r = (int) Math.sqrt(count);
+               r *= spacing;
+
+               if (this.attribute != null && count > 0) {
+                       Class<?> klass = 
nodes.get(0).getNode().getCyRow(namespace).contains(attribute); 
+                       if (klass != null && 
Comparable.class.isAssignableFrom(klass)){
+                               // FIXME: I assume this would be better, but 
get type errors if I try:
+                               //Class<Comparable<?>> kasted = 
(Class<Comparable<?>>) klass;
+                               //Collections.sort(nodes, new 
AttributeComparator<Comparable<?>>(kasted));
+                               Collections.sort(nodes, new 
AttributeComparator(klass));
+                       } else {
+                               /* FIXME Error! */
+                       }
+               }
+
+               // Compute angle step
+               double phi = (2 * Math.PI) / count; 
+
+               partition.resetNodes(); // We want to figure out our mins & 
maxes anew
+                                       // Arrange vertices in a circle
+
+               for (int i = 0; i < count; i++) {
+                       LayoutNode node = (LayoutNode) nodes.get(i);
+                       double x = r + (r * Math.sin(i * phi));
+                       double y = r + (r * Math.cos(i * phi));
+                       node.setX(x);
+                       node.setY(y);
+                       partition.moveNodeToLocation(node);
+               }
+       }
+       
+       private class AttributeComparator<T extends Comparable<T>> implements 
Comparator<LayoutNode> {
+               Class<T> klass;
+               private AttributeComparator(Class<T> klass) {
+                       this.klass = klass;
+               }
+               
+               public int compare(LayoutNode o1, LayoutNode o2) {
+                       T v1 = o1.getNode().getCyRow(namespace).get(attribute, 
klass);
+                       T v2 = o2.getNode().getCyRow(namespace).get(attribute, 
klass);
+                       if (String.class.isAssignableFrom(klass)){ // i.e. if 
klass _is_ String.class
+                               String s1 = String.class.cast(v1);
+                               String s2 = String.class.cast(v2);
+                               if ((s1 != null) && (s2 != null))
+                                       return s1.compareToIgnoreCase(s2);
+                               else if ((s1 == null) && (s2 != null))
+                                       return -1;
+                               else if ((s1 == null) && (s2 == null))
+                                       return 0;
+                               else if ((s1 != null) && (s2 == null))
+                                       return 1;
+                               
+                       } else {
+                               return compareEvenIfNull(v1, v2);
+                       }
+
+                       return 0; // can't happen anyway
+               }
+
+               public int compareEvenIfNull(T v1, T v2){
+                       if ((v1 != null) && (v2 != null))
+                               return v1.compareTo(v2);
+                       else if ((v1 == null) && (v2 != null))
+                               return -1;
+                       else if ((v1 == null) && (v2 == null))
+                               return 0;
+                       else // if ((v1 != null) && (v2 == null)) // this is 
the only possibility
+                               return 1;
+               }
+       }
+
+}

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