1 (ns treeline.graph_editor
4 (cytoscape.data Semantics)
5 (ding.view NodeContextMenuListener)
6 (csplugins.layout.algorithms.hierarchicalLayout HierarchicalLayoutAlgorithm)
7 (javax.swing JFrame JScrollPane JMenuItem JPanel)
9 (java.awt.event ActionListener KeyAdapter KeyEvent)))
13 `(ij.IJ/log (str ~@args)))
17 `(let [node# (Cytoscape/getCyNode ~title true)]
24 (dosync (alter id inc))))
27 "Create a list of maps, where each map contains a :node and potentially a :branch,
28 the latter containing another list of maps, etc."
30 ;(println "branch: "branch)
31 (let [points (.getPoints branch)
32 branches (if-let [bm (.getBranches branch)]
38 (let [m {:x x :y y :layer-id layer-id
39 :node (create-node net (str branch-id \- i))}]
41 (if-let [bs (branches i)]
42 (assoc m :branches (map
47 (range (count (get points 0)))
50 (seq (get points 2)))))
56 (Cytoscape/getCyEdge node1 node2 Semantics/INTERACTION node-type true)))
60 (loop [prev-node (first nodes)
61 next-nodes (next nodes)]
63 ;(println "prev-node is" prev-node)
64 (doseq [b (prev-node :branches)]
67 (.addEdge net (create-edge (prev-node :node)
69 (let [next-node (first next-nodes)]
73 (.addEdge net (create-edge (prev-node :node) (next-node :node)))
74 (recur next-node (next next-nodes))))))))
76 ; WRONG: first node doesn't get checked for branches
80 ; (.addEdge net (create-edge (m1 :node) (m2 :node)))
81 ; (doseq [b (m2 :branches)]
82 ; (println "going to use branch " b)
83 ; (.addEdge net (create-edge (m2 :node) ((first (create-edges net b)) :node))))))
89 (let [nodes (create-nodes net (.getRoot tline))]
90 (dorun (create-edges net nodes))
95 ; (.addEdge net (create-edge ((get v (dec (count v))) :node) (m :node)))
96 ; (if-let [bs (m :branches)]
98 ; (.addEdge net (create-edge (m :node) (first (create-tree b)))))))
99 ; (create-nodes net branch)))
102 "Creates a graph of cytoscape nodes that show all points of a Treeline."
104 (let [net (Cytoscape/createNetwork (.getTitle tline) false)
105 nodes (create-tree net tline)
106 view (Cytoscape/createNetworkView net (.getTitle tline) (HierarchicalLayoutAlgorithm.))]
110 "Takes any Component and shows it inside a JFrame."
112 (doto (JFrame. title)
118 "Takes a Component and returns a JScrollPane of dimensions w,h that shows it."
120 (doto (JScrollPane. component)
121 (.setPreferredSize (Dimension. w h))))
124 "Show the given treeline as a tree of nodes.
125 Add mouse event hooks to each node to show a contextual menu."
128 (let [[net nodes view] (create-graph tline)
129 frame (JFrame. (.getTitle tline))
130 panel (JScrollPane. (.getComponent view))]
132 (.setPreferredSize (Dimension. 500 500)))
133 (.. frame getContentPane (add panel))
139 (.addNodeContextMenuListener
140 (let [worker (agent nil)
141 items {(JMenuItem. "Fit graph") #(.fitContent view)
142 (JMenuItem. "Select in 2D") #(report "Selected not yet implemented")}
143 listener (proxy [ActionListener] []
144 (actionPerformed [evt]
148 (if-let [action (items (.getSource evt))]
150 (report "Action not found for " (.getCommand evt))))))))]
151 (doseq [item (keys items)] (.addActionListener item listener))
152 (proxy [NodeContextMenuListener] []
153 (addNodeContextMenuItems [nodeView menu]
155 (doseq [item (keys items)] (.add menu item)))))))
156 (doto (.getComponent view)
158 (proxy [KeyAdapter] []
161 (println "key pressed: " (.getKeyChar evt))
162 (if (= KeyEvent/VK_F (.getKeyCode evt))
164 (catch Exception e (.printStackTrace e)))))))
166 (report "Null Treeline!")))