gzz-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Gzz-commits] gzz/lava gzz/loom/Cursor.java gzz/loom/SimpleVi...


From: Vesa Kaihlavirta
Subject: [Gzz-commits] gzz/lava gzz/loom/Cursor.java gzz/loom/SimpleVi...
Date: Wed, 26 Feb 2003 11:55:37 -0500

CVSROOT:        /cvsroot/gzz
Module name:    gzz
Changes by:     Vesa Kaihlavirta <address@hidden>       03/02/26 11:55:37

Modified files:
        lava/gzz/loom  : Cursor.java SimpleView.java 
        lava/test/gzz/loom: Cursor.test SimpleView.test 

Log message:
        [tuukka,benja] Refactor Cursor

CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/loom/Cursor.java.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/gzz/loom/SimpleView.java.diff?tr1=1.23&tr2=1.24&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/loom/Cursor.test.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/gzz/gzz/lava/test/gzz/loom/SimpleView.test.diff?tr1=1.15&tr2=1.16&r1=text&r2=text

Patches:
Index: gzz/lava/gzz/loom/Cursor.java
diff -u gzz/lava/gzz/loom/Cursor.java:1.9 gzz/lava/gzz/loom/Cursor.java:1.10
--- gzz/lava/gzz/loom/Cursor.java:1.9   Wed Feb 26 08:38:24 2003
+++ gzz/lava/gzz/loom/Cursor.java       Wed Feb 26 11:55:36 2003
@@ -35,54 +35,61 @@
  */
 public class Cursor {
 
-    /** The focused node, usually shown in the middle of the screen. "Where 
you are."
+    /** The focused node, usually shown in the middle of the screen. 
+     *  "Where you are."
      */
     public Resource focus;
 
-    /** The 'selected' node, left/right of the focused node. "Where you go."
-     *  This can be either left or right of the focus.
+    /** Index of the selection, "where you go next". 0 is the index in the
+     *  middle, or one smaller if there are an even number of choices.
      */
-    public RDFNode rotation;
-
-    /** The direction of the rotation from the focus: Posward (+1) or negward 
(-1).
-     *  If there is no rotation (rotation == null), this is zero.
-     *  The idea is that the same node can be right and left of the focus,
-     *  and we need to know which one's the rotation we're speaking about.
-     */
-    public int dir;
+    public int rotation;
 
     /** The comparator used to order the nodes in the graph.
      *  The vertical order of nodes is determined by this Comparator.
      */
     public Comparator order;
 
+    /** Create a new cursor without giving it a position yet.
+     */
     public Cursor(Comparator order) {
        this.order = order;
     }
 
-    public Cursor(Comparator order, Resource focus, int dir, RDFNode rotation) 
{
+    /** Create a new cursor and give it a position.
+     */
+    public Cursor(Comparator order, Resource focus, int dir,
+                 RDFNode rotation) {
        this(order);
        set(focus, dir, rotation);
     }
 
+    /** Set the position of the cursor without setting a rotation.
+     */
+    public void set(Resource focus) {
+       this.focus = focus;
+       this.rotation = 0;
+    }
+
+    /** Set the position of the cursor.
+     *  Ambiguity of <code>rotation</code> (is it posward
+     *  or negward?) might be a problem?
+     */
     public void set(Resource focus, int dir, RDFNode rotation) {
        this.focus = focus;
-       this.dir = dir;
-       this.rotation = rotation;
+       SortedSet set = getConnections(dir);
+       if(!set.contains(rotation))
+           throw new NoSuchElementException("Rotation: "+rotation);
+
+       this.rotation = set.headSet(rotation).size()-(set.size()-1)/2;
     }
 
-    /** Get the index of <code>rotation</code> among the connections.
-     *  E.g., when <code>rotation</code> is the first of the connections,
-     *  then this returns 0; when the rotation is the third connection,
-     *  this returns 2.
-     */
-    public int getRotationIndex() {
-       if(rotation == null)
-           return 0;
-       else if(getConnections(dir).contains(rotation))
-           return getConnections(dir).headSet(rotation).size();
-       else
-           return 0;
+    /** Get <code>rotation</code> from the middle connection.
+     *  E.g., when there are 6 connections and the second one is selected,
+     *  <code>rotation</code> is -1.
+     */
+    public int getRotation() {
+       return rotation;
     }
 
     /** The posward or negward connections of <code>focus</code> to show,
@@ -113,63 +120,59 @@
     }
 
     /** Rotate the view up or down.
-     *  This moves the rotation of the cursor one step up or down. XXX
+     *  This moves the rotation of the cursor up or down.
      */
-    public void rotate(int rdir) {
+    public void rotate(int dir) {      
        SortedSet 
            neg = getConnections(-1),
            pos = getConnections(1);
 
-       if(rotation == null) {
-           if(pos.size() > 0) {
-               rotation = (RDFNode)pos.iterator().next();
-               dir = 1;
-           } else if(neg.size() > 0) {
-               rotation = (RDFNode)neg.iterator().next();
-               dir = -1;
-           } else
-               return;
-       }
-
-       int i = getRotationIndex();
+       int negn = neg.size();
+       int posn = pos.size();
 
-       if(rdir < 0) i--;
-       else i++;
+       int n = (negn>posn) ? negn : posn;
 
-       if(i < 0) return;
-       if(i >= neg.size() && i >= pos.size()) return;
+       int abs = (n-1)/2 + rotation + dir;
 
-       List l;
-       if(dir > 0) {
-           if(pos.size() > i)
-               l = new ArrayList(pos);
-           else {
-               l = new ArrayList(neg);
-               dir = -1;
-           }
-       } else {
-           if(neg.size() > i)
-               l = new ArrayList(neg);
-           else {
-               l = new ArrayList(pos);
-               dir = 1;
-           }
-       }
+       if(abs<0 || abs>=n)
+           return;
 
-       rotation = (RDFNode)l.get(i);
+       rotation += dir;
     }
 
-    /** Move left/right. XXX
+    /** Get the index of the rotation in the list of
+     *  left/right connections. Can be out-of-bounds in that
+     *  it is smaller than zero or larger than the number
+     *  of items in the list: e.g. if you have 11 nodes
+     *  connected to the right and 3 nodes connected
+     *  to the left, when the last of the 11 nodes is selected,
+     *  getRotationIndex(-1) will return +7. XXX
      */ 
-    public void move(int mdir) {
-       Iterator iter = getConnections(mdir).iterator();
-       int n = getRotationIndex();
+    public int getRotationIndex(int dir) {
+       int n = getConnections(dir).size();
+       int abs = (n-1)/2 + rotation;
+       return abs;
+    }
+
+    public RDFNode getRotationNode(int dir) {
+       Iterator iter = getConnections(dir).iterator();
+       int n = getRotationIndex(dir);
+       if(n < 0) 
+           // nothing there
+           return null;
        for(int i=0; i<n; i++) if(iter.hasNext()) iter.next();
        if(!iter.hasNext())
-           // cannot move there
-           return;
-       rotation = focus;
-       focus = (Resource)iter.next();
-       dir = -mdir;
+           // nothing there
+           return null;
+
+       return (RDFNode)iter.next();
+    }
+
+    /** Move left/right. XXX
+     */ 
+    public void move(int dir) {
+       RDFNode node = getRotationNode(dir);
+       if(node == null) return;
+       set((Resource)node, -dir, focus);
     }
 }
Index: gzz/lava/gzz/loom/SimpleView.java
diff -u gzz/lava/gzz/loom/SimpleView.java:1.23 
gzz/lava/gzz/loom/SimpleView.java:1.24
--- gzz/lava/gzz/loom/SimpleView.java:1.23      Sun Feb 23 16:11:34 2003
+++ gzz/lava/gzz/loom/SimpleView.java   Wed Feb 26 11:55:36 2003
@@ -63,7 +63,7 @@
                               1, 1, sizex, sizey);
        nodeView.render(sc, cs, c.focus);
        
-       int before = c.getRotationIndex();
+       int before = c.getRotationIndex(1);
        
        int x = midx + gapx + sizex/2, 
            y = midy - sizey/2 - before*(gapy+sizey);
@@ -76,6 +76,7 @@
            y += sizey + gapy;
        }
 
+       before = c.getRotationIndex(-1);
        x = midx - gapx - sizex - sizex/2; 
        y = midy - sizey/2 - before*(gapy+sizey);
 
Index: gzz/lava/test/gzz/loom/Cursor.test
diff -u gzz/lava/test/gzz/loom/Cursor.test:1.8 
gzz/lava/test/gzz/loom/Cursor.test:1.9
--- gzz/lava/test/gzz/loom/Cursor.test:1.8      Sun Feb 23 18:59:17 2003
+++ gzz/lava/test/gzz/loom/Cursor.test  Wed Feb 26 11:55:37 2003
@@ -37,6 +37,9 @@
     def compare(self, r1, r2):
         if (not r1) or (not r2):
             raise java.lang.NullPointerException("r1=%r, r2=%r" % (r1, r2))
+
+        # Nodes are compared by order of index:
+        # n[4] > n[2], n[7]  > n[4] etc.
         return n.index(r1) - n.index(r2)
 c = gzz.loom.Cursor(Cmp())
 
@@ -72,135 +75,151 @@
 
     c.set(n[2], 1, n[5])
     c.move(1)
-    assert c.focus == n[5] and c.rotation == n[2]
-    assert c.getRotationIndex() == 0 and c.dir == -1
+    assert c.focus == n[5] and c.getRotationNode(-1) == n[2]
+    assert c.getRotationIndex(-1) == 0
 
     c.rotate(1)
-    assert c.focus == n[5] and c.rotation == n[2]
-    assert c.getRotationIndex() == 1 and c.dir == 1
+    assert c.focus == n[5] and c.getRotationNode(1) == n[2]
+    assert c.getRotationIndex(1) == 1
 
     c.rotate(-1)
-    assert c.focus == n[5] and c.rotation == n[0]
-    assert c.getRotationIndex() == 0 and c.dir == 1
+    assert c.focus == n[5] and c.getRotationNode(1) == n[0]
+    assert c.getRotationIndex(1) == 0
 
 
 def testRotationIndex():
     c.set(n[1], 1, n[2])
-    assert c.getRotationIndex() == 0
+    assert c.rotation == 0
+    assert c.getRotationIndex(1) == 0
+    assert c.getRotationIndex(-1) == 0
 
     c.set(n[2], 1, n[5])
-    assert c.getRotationIndex() == 2
+    assert c.rotation == -1
+    assert c.getRotationIndex(1) == 2
+    assert c.getRotationIndex(-1) == -1
 
     c.set(n[2], 1, n[9])
-    assert c.getRotationIndex() == 6
-
-    c.set(n[4], 0, None)
-    assert c.getRotationIndex() == 0
-
+    assert c.rotation == 3
+    assert c.getRotationIndex(1) == 6
+    assert c.getRotationIndex(-1) == 3
+
+    c.set(n[4])
+    assert c.rotation == 0
+    assert c.getRotationIndex(1) == 0
+    assert c.getRotationIndex(-1) == 0
 
 def testRotateMoveLeft():
+    """
+    > the point in this test is,
+    > in the others we've had a tree
+    > n[0] connected to n[1], n[1] to n[2], n[2] to the others (n[3:])
+    > so we never had more than one backward conn
+    > in this test, n[5] has three backward conns, and that's what's tested
+    (- benja)
+    """
     n[4].addProperty(p, n[5])
     n[6].addProperty(p, n[5])
 
     c.set(n[5], -1, n[4])
 
-    assert c.getRotationIndex() == 1
+    assert c.getRotationIndex(-1) == 1
+    assert c.rotation == 0
     c.rotate(-1)
-    assert c.getRotationIndex() == 0
-    assert c.rotation == n[2]
+    assert c.getRotationIndex(-1) == 0
+    assert c.rotation == -1
+    assert c.getRotationNode(-1) == n[2]
     c.rotate(-1)
-    assert c.getRotationIndex() == 0
+    assert c.getRotationIndex(-1) == 0
     c.rotate(1)
-    assert c.getRotationIndex() == 1
+    assert c.getRotationIndex(-1) == 1
     c.rotate(1)
-    assert c.getRotationIndex() == 2
+    assert c.getRotationIndex(-1) == 2
     c.rotate(1)
-    assert c.getRotationIndex() == 2
-    assert c.rotation == n[6]
+    assert c.getRotationIndex(-1) == 2
+    assert c.getRotationNode(-1) == n[6]
 
-    c.set(n[5], 0, None)
+    c.set(n[5])
 
-    assert c.getRotationIndex() == 0
+    assert c.rotation == 0
+    assert c.getRotationIndex(-1) == 1
+    c.rotate(-1)
+    assert c.rotation == -1
+    assert c.getRotationIndex(-1) == 0
     c.rotate(-1)
-    assert c.getRotationIndex() == 0
+    assert c.rotation == -1
+    assert c.getRotationIndex(-1) == 0
     c.rotate(1)
-    assert c.getRotationIndex() == 1
-    assert c.rotation == n[4]
+    assert c.rotation == 0
+    assert c.getRotationIndex(-1) == 1
+    assert c.getRotationNode(-1) == n[4]
 
-    c.set(n[5], 0, None)
-    c.rotate(1)
-    assert c.getRotationIndex() == 1
-    assert c.rotation == n[4]
+    c.set(n[5])
+    assert c.getRotationIndex(-1) == 1
+    assert c.getRotationNode(-1) == n[4]
 
     c.move(-1)
-    assert c.focus == n[4] and c.rotation == n[5]
-    
+    assert c.focus == n[4] and c.getRotationNode(1) == n[5]
+
 
 def testRotateMove():
     c.set(n[2], 1, n[5])
     c.rotate(1)
-    assert c.focus == n[2] and c.rotation == n[6] and c.dir == 1
+    assert c.focus == n[2] and c.getRotationNode(1) == n[6]
     c.rotate(1)
-    assert c.focus == n[2] and c.rotation == n[7] and c.dir == 1
+    assert c.focus == n[2] and c.getRotationNode(1) == n[7]
     c.rotate(-1)
-    assert c.focus == n[2] and c.rotation == n[6] and c.dir == 1
+    assert c.focus == n[2] and c.getRotationNode(1) == n[6]
 
     c.set(n[2], 1, n[9])
     c.rotate(1)
-    assert c.focus == n[2] and c.rotation == n[9]
+    assert c.focus == n[2] and c.getRotationNode(1) == n[9]
     c.rotate(-1)
-    assert c.focus == n[2] and c.rotation == n[8]
+    assert c.focus == n[2] and c.getRotationNode(1) == n[8]
     c.rotate(1)
-    assert c.focus == n[2] and c.rotation == n[9]
+    assert c.focus == n[2] and c.getRotationNode(1) == n[9]
     c.rotate(1)
-    assert c.focus == n[2] and c.rotation == n[9]
+    assert c.focus == n[2] and c.getRotationNode(1) == n[9]
 
     c.set(n[2], -1, n[1])
-    assert c.getRotationIndex() == 0 and c.dir == -1
+    assert c.rotation == 0
+    assert c.getRotationIndex(-1) == 0
+    assert c.getRotationIndex(1) == 3
     c.rotate(1)
-    assert c.getRotationIndex() == 1 and c.dir == 1
+    assert c.rotation == 1
+    assert c.getRotationIndex(1) == 4
     c.rotate(1)
-    assert c.getRotationIndex() == 2 and c.dir == 1
+    assert c.rotation == 2
+    assert c.getRotationIndex(1) == 5
     c.move(-1)
-    assert c.focus == n[2] and c.getRotationIndex() == 2 and \
-           c.dir == 1
+    assert c.focus == n[2] and c.getRotationIndex(1) == 5
+    assert c.getRotationNode(1) == n[8]
     c.move(1)
-    assert c.focus == n[5] and c.rotation == n[2] and c.dir == -1
+    assert c.focus == n[8] and c.rotation == 0 and \
+           c.getRotationNode(-1) == n[2]
 
     c.set(n[2], 1, n[3])
     c.rotate(-1)
-    assert c.focus == n[2] and c.rotation == n[3] and c.dir == 1
+    assert c.focus == n[2] and c.getRotationNode(1) == n[3]
 
+    c.set(n[2], 1, n[6])
     c.move(-1)
-    assert c.focus == n[1] and c.rotation == n[2] and c.dir == 1
+    assert c.focus == n[1] and c.getRotationNode(1) == n[2]
     c.move(-1)
-    assert c.focus == n[0] and c.rotation == n[1] and c.dir == 1
+    assert c.focus == n[0] and c.getRotationNode(1) == n[1]
     c.move(1)
-    assert c.focus == n[1] and c.rotation == n[0] and c.dir == -1
+    assert c.focus == n[1] and c.getRotationNode(-1) == n[0]
+    assert c.getRotationNode(1) == n[2]
     c.move(1)
-    assert c.focus == n[2] and c.rotation == n[1] and c.dir == -1
+    assert c.focus == n[2] and c.getRotationNode(-1) == n[1]
+    assert c.getRotationNode(1) == n[6]
 
 
     # if we cannot move there, we remain where we are:
 
     c.set(n[2], 1, n[7])
     c.move(-1)
-    assert c.focus == n[2] and c.rotation == n[7] and c.dir == 1
+    assert c.focus == n[2] and c.getRotationNode(1) == n[7]
 
     c.set(n[0], 1, n[1])
     c.move(-1)
-    assert c.focus == n[0] and c.rotation == n[1] and c.dir == 1
-
-
-def testRotationChangesDir():
-    n[7].addProperty(p, n[1])
-
-    c.set(n[2], -1, n[1])
-    assert c.getRotationIndex() == 0 and c.dir == -1
-    c.rotate(1)
-    assert c.getRotationIndex() == 1 and c.dir == 1
-
-    c.set(n[1], 1, n[2])
-    assert c.getRotationIndex() == 0 and c.dir == 1
-    c.rotate(1)
-    assert c.getRotationIndex() == 1 and c.dir == -1
+    assert c.focus == n[0] and c.getRotationNode(1) == n[1]
Index: gzz/lava/test/gzz/loom/SimpleView.test
diff -u gzz/lava/test/gzz/loom/SimpleView.test:1.15 
gzz/lava/test/gzz/loom/SimpleView.test:1.16
--- gzz/lava/test/gzz/loom/SimpleView.test:1.15 Sun Feb 23 18:44:56 2003
+++ gzz/lava/test/gzz/loom/SimpleView.test      Wed Feb 26 11:55:37 2003
@@ -50,7 +50,8 @@
     r.addProperty(p, t)
 
     vs = getvs()
-    cursor = gzz.loom.Cursor(Cmp(), r, 0, None)
+    cursor = gzz.loom.Cursor(Cmp())
+    cursor.set(r)
     view.render(vs, 0, cursor)
 
     cs_r, cs_s, cs_t, cs_u, cs_v = [vs.matcher.getCS(0, x)
@@ -75,6 +76,7 @@
 
     vs = getvs()
     cursor = gzz.loom.Cursor(Cmp(), r, 1, s)
+    assert cursor.rotation == 0
     view.render(vs, 0, cursor)
 
     cs_r, cs_s, cs_t, cs_u, cs_v, cs_w = \
@@ -96,9 +98,9 @@
     # s,t,u,v should be same size and on a single vertical line
     assert ps.x == pt.x == pu.x == pv.x > pr.x > pw.x
 
-    # s and w should be approx. the same height as r,
+    # s and w should be the same height as r,
     # while t is above and v,u are below
-    assert pt.y == pw.y < pr.y == ps.y < pv.y < pu.y
+    assert pt.y < pw.y == pr.y == ps.y < pv.y < pu.y
 
     # distances on both sides should be equal
     # this works only if all nodes are same size... XXX




reply via email to

[Prev in Thread] Current Thread [Next in Thread]