/
ex_dialog.clj
89 lines (75 loc) · 3.2 KB
/
ex_dialog.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
(ns reactor.ex-dialog
(:require [reactor.core :as r]
[reactor.swing-support :as s])
(:import [javax.swing DefaultListModel JButton JLabel JList JPanel JTextField]))
;; Demonstrates a master-detail UI scenario
;; - functions opening a window return a stream to deliver the results
;; - these functions are invoked using flatmap
;; This example is very rudimentary,
;; for example there is no means to deactivate
;; the master window when the detail is shown
;; (start) starts the program
(defn remove-by-index
[items i]
(vec (concat
(take i items)
(drop (inc i) items))))
(defn detail-frame
[items]
(let [detail (s/frame "Detail" (doto (s/panel "fillx, wrap 2" "[|grow]" "[|]")
(.add (JLabel. "Name"))
(.add (JTextField. "") "growx")
(.add (doto (s/panel "ins 0" "[||grow]" "[]")
(.add (JButton. "OK"))
(.add (JButton. "Cancel")))
"span"))
200 100)
text-b (->> (s/get-component detail [1])
(s/text-behavior))
actions (->> (r/merge (->> (s/get-component detail [2 0]) (s/action-events))
(->> (s/get-component detail [2 1]) (s/action-events)))
(r/map :value))]
(->> actions
(r/subscribe (fn [_]
(s/close detail)
(r/complete! actions)))
(r/map #(case %
"Cancel" items
"OK" (conj items @text-b))))))
(defn master-action
[[items sel action]]
(case action
"Add" (detail-frame items)
"Remove" (r/just (remove-by-index items sel))
(r/just items)))
(defn master-frame
[]
(let [master (s/frame "Master" (doto (s/panel "flowy, fill" "[]" "[|grow|]")
(.add (JLabel. "Items"))
(.add (JList. (DefaultListModel.)) "grow")
(.add (doto (s/panel "ins 0" "[||grow]" "[]")
(.add (JButton. "Add"))
(.add (JButton. "Remove"))
(.add (JButton. "Close")))))
600 400)
listbox (s/get-component master [1])
items-b (s/listmodel-behavior listbox)
sel-b (s/listselection-behavior listbox)
actions (->> (r/merge (->> (s/get-component master [2 0]) (s/action-events))
(->> (s/get-component master [2 1]) (s/action-events))
(->> (s/get-component master [2 2]) (s/action-events)))
(r/map :value))]
(->> actions
(r/map vector items-b sel-b)
(r/flatmap master-action)
(r/into items-b))
(->> actions
(r/filter #{"Close"})
(r/snapshot items-b)
(r/subscribe (fn [_]
(s/close master)
(r/complete! actions))))))
(defn start
[]
(->> (master-frame)
(r/subscribe println)))