Scala #031: Reactor
|中級篇|
Java.use(better, src=Scala) # ソースコードの歩き方《中級篇》
ソースコードの歩き方《Scala2.8.0》
《著》小粒ちゃん@湘南組、小粒ちゃん@博多組《監修》小泉ひよ子とタマゴ倶楽部
第1版♪2010/07/14
|関連記事|
Scala #031: Reactor
》作業中です《
■ ボタンを選択する
30: for (e <- items.toArray) { 31: contents += new Button(e) { 32: Command.listenTo(this) 33: } 34: }
ボタンを選択(クリック)すると、Command が Button に呼応します。
50: object Command extends Publisher { 51: reactions += { 52: case ButtonClicked(source) => 53: val value = source.text 54: update(value) 55: } 56: }ボタンをクリックすると、イベント ButtonClicked が発生するので、それに反応する処理を記述します。
■ scala.swing.Reactor
16 trait Reactor {
17 /**
18 * All reactions of this reactor.
19 */
20 val reactions: Reactions = new Reactions.Impl
21 /**
22 * Listen to the given publisher as long as deafTo
isn't called for
23 * them.
24 */
25 def listenTo(ps: Publisher*) = for (p <- ps) p.subscribe(reactions)
26 /**
27 * Installed reaction won't receive events from the given publisher anylonger.
28 */
29 def deafTo(ps: Publisher*) = for (p <- ps) p.unsubscribe(reactions)
30 }