-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReadmeExample.scala
66 lines (56 loc) · 2 KB
/
ReadmeExample.scala
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
package async
import java.net.URL
import java.util.concurrent.Executors
import java.util.concurrent.ThreadFactory
import scala.io.Codec.charset2codec
import scala.io.Codec
import scala.io.Source
import scala.swing.event.ButtonClicked
import scala.swing.Button
import scala.swing.FlowPanel
import scala.swing.Label
import scala.swing.MainFrame
import scala.swing.SimpleSwingApplication
import scala.swing.TextField
import Async.await
import Async.async
import akka.dispatch.ExecutionContext
import akka.dispatch.ExecutionContexts
import akka.dispatch.Future
import javax.swing.SwingUtilities
/** Painless asynchronous programming with Swing. */
object ReadmeExample extends SimpleSwingApplication {
val dlButton = new Button("Download")
val charCount = new TextField { text = "0"; columns = 5 }
def top = new MainFrame {
title = "Scala-Async Test"
contents = new FlowPanel(dlButton, new Label("# Chars"), charCount)
}
listenTo(dlButton)
reactions += {
case ButtonClicked(`dlButton`) =>
charCount.text = ""
async {
// No blocking is involved here!
val akka = await { downloadAsync("http://www.akka.io/") }
val scala = await { downloadAsync("http://www.scala-lang.org/") }
// Access swing components directly
charCount.text = (akka.length + scala.length).toString
}
}
/** Downloads the content of the given url asynchronously. */
def downloadAsync(url: String): Future[String] = Future {
Source.fromURL(new URL(url))(Codec.ISO8859).getLines.mkString
} (threadPool)
/** Swing execution context */
implicit val swingCtx = new ExecutionContext {
def execute(runnable: Runnable): Unit = SwingUtilities.invokeLater(runnable)
def reportFailure(t: Throwable): Unit = sys.error(t.getMessage())
}
/** Worker thread pool */
val threadPool = ExecutionContexts.fromExecutor(Executors.newFixedThreadPool(2,
new ThreadFactory {
def newThread(r: Runnable) = { val t = new Thread(r); t.setDaemon(true); t }
}
))
}