 
 ;;; simple thing which can be moved around
 
-(defn add-thing-to-move-around [owner x y]
-  (let [segment (GSegment.)
+(defn add-node [scn x y]
+  (let [object (GObject.)
+	segment (GSegment.)
 	style (GStyle.)]
-    (.addSegment owner segment)
+    (.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 segment (ref {:position [x y]}))
-    segment))
+    (.setUserData object (ref {:type :node, :position [x y]}))
+    object))
+
+(defn connect-nodes [scn x y]
+  (let [[x1 x2] (:position @(.getUserData x))
+	[y1 y2] (:position @(.getUserData y))
+	c (GObject.)
+	line (GSegment.)]
+    (.add scn c)
+    (.addSegment c line)
+    (.toBack c)
+    (.setUserData c (ref {:type :connection}))
+    (.setGeometry line x1 x2 y1 y2)
+    (dosync
+     (alter (.getUserData x) assoc :upper (conj (:upper @(.getUserData x)) c))
+     (alter (.getUserData y) assoc :lower (conj (:lower @(.getUserData y)) c)))))
+
+(defn node? [thing]
+  (= (:type @(.getUserData thing)) :node))
+
+(defn connection? [thing]
+  (= (:type @(.getUserData thing)) :connection))
 
 (defn move-interaction []
   (let [interaction-obj (ref nil)

	    (ref-set interaction-obj nil))
	   nil)))))

(defn connect-nodes [scn x y]
  (let [[x1 x2] (:position @(.getUserData x))
	[y1 y2] (:position @(.getUserData y))
	c (GSegment.)]
    (.addSegment scn c)
    (.setGeometry c x1 x2 y1 y2)
    (dosync
     (alter (.getUserData x) assoc :upper #{c})
     (alter (.getUserData y) assoc :lower #{c}))))

(defn show-some-picture []
  (let [wnd (GWindow.)
	scn (GScene. wnd)

      (event [scn evt x y]
	(condp = evt
	   GWindow/BUTTON1_DOWN
	   (let [thing (.find scn x y)]
	     (when (node? thing)
	       (dosync
		(ref-set interaction-obj thing)
		(ref-set x0 x)
