 (defn position [node]
   (:position @(.getUserData node)))
 
-(defn upper-neighbors [node]
+(defn lower-node [conn]
+  (:lower @(.getUserData conn)))
+
+(defn upper-node [conn]
+  (:upper @(.getUserData conn)))
+
+(defn upper-connections [node]
   (:upper @(.getUserData node)))
 
-(defn lower-neighbors [node]
+(defn lower-connections [node]
   (:lower @(.getUserData node)))
 
-(defn start-of-connection [conn]
-  (let [segment (.getSegment conn)]
-    [(aget (.getX segment) 0) (aget (.getY segment) 0)]))
+(defn upper-neighbors [node]
+  (map upper-node (upper-connections node)))
 
-(defn end-of-connection [conn]
-  (let [segment (.getSegment conn)]
-    [(aget (.getX segment) 1) (aget (.getY segment) 1)]))
+(defn lower-neighbors [node]
+  (map lower-node (lower-connections node)))
 
 
 ;;; create and connect nodes
 
 (defn add-node [scn x y]
-  (let [object (GObject.)
-	segment (GSegment.)
+  (let [segment (GSegment.)
+	object (proxy [GObject] []
+		 (draw []
+		   (let [[x y] (position this)]
+		     (.setGeometryXy segment (Geometry/createCircle (double x) (double y) 10.0)))))
 	style (GStyle.)]
     (.addSegment object segment)
-    (.add scn object)
     (doto style
       (.setForegroundColor (Color.  6  6 86))
       (.setBackgroundColor (Color. 23 11 84)))
     (.setStyle segment style)
-    (.setGeometryXy segment (Geometry/createCircle (double x) (double y) 10.0))
-    (.setUserData object (ref {:type :node, :position [x y]}))
+    (.setUserData object (ref {:type :node,
+			       :position [x y]}))
+    (.setName object (str [x y]))
+    (.add scn object)
     object))
 
 (defn connect-nodes [scn x y]
-  (let [[x1 x2] (:position @(.getUserData x))
-	[y1 y2] (:position @(.getUserData y))
-	c (GObject.)
-	line (GSegment.)]
+  (let [line (GSegment.)
+	c       (proxy [GObject] []
+		  (draw []
+		    (let [[x1 y1] (position (lower-node this))
+			  [x2 y2] (position (upper-node this))]
+		      (.setGeometry line (double x1) (double y1) (double x2) (double y2)))))]
     (.add scn c)
     (.addSegment c line)
     (.toBack c)
-    (.setUserData c (ref {:type :connection}))
-    (.setGeometry line (double x1) (double x2) (double y1) (double y2))
+    (.setUserData c (ref {:type :connection,
+			  :lower x,
+			  :upper y}))
     (dosync
      (alter (.getUserData x) update-in [:upper] conj c)
      (alter (.getUserData y) update-in [:lower] conj c))))

	   [no.geosoft.cc.graphics GWindow GScene GObject ZoomInteraction GSegment GStyle
	                           GText GPosition GScene GInteraction]))

;;; nodes and connections

(defn node? [thing]

				   (when (node? thing)
				     (reset! interaction-obj thing)))
	   GWindow/BUTTON1_DRAG  (when @interaction-obj
				   (let [[x y] (device-to-world scn x y)]
				     (move-node-to @interaction-obj x y)
				     (.refresh scn)))
	   GWindow/BUTTON1_UP    (reset! interaction-obj nil)
	   nil)))))

