Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package suereth.helper
- import scala.actors._
- /**
- * An Actor Scheduler which defers all processing to the Swing Event Queue.
- */
- object SwingEventQueueScheduler extends IScheduler {
- /** Submits a closure for execution.
- *
- * @param fun the closure to be executed
- */
- def execute(fun: => Unit): Unit = {
- execute(new Runnable {
- def run = fun
- })
- }
- /** Submits a <code>Runnable</code> for execution.
- *
- * @param task the task to be executed
- */
- def execute(task: Runnable): Unit = {
- //TODO - If we are in the event queue thread, can we be greedy and just process now?
- import javax.swing.SwingUtilities
- SwingUtilities.invokeLater(task)
- }
- /** Notifies the scheduler about activity of the
- * executing actor.
- *
- * @param a the active actor
- */
- def tick(a: Actor): Unit = {}
- /** Shuts down the scheduler.
- */
- def shutdown(): Unit = {}
- def onLockup(handler: () => Unit): Unit = {}
- def onLockup(millis: Int)(handler: () => Unit): Unit = {}
- def printActorDump: Unit = {}
- }
- /**
- * An actor which processes all of its messages on the swing event queue.
- */
- trait SwingActor extends Actor {
- override protected def scheduler = SwingEventQueueScheduler
- //TODO - Do we need to override start and remove the ActorGC junk?
- //We automatically start the actor, because we rely on normal GC, and our scheduler doesn't do anything special besides processing on the AWT-Thread.
- start
- }
- import scala.swing.Reactor
- import scala.swing.event.Event
- /** A marker class for messages passed into a component via the actors library */
- case class ExternalMessage[A](msg : A) extends Event
- /** This trait can be used with scala.swing Reactor compoennts to allow the processing of external messages via reactions*/
- trait ActorReactorBridge extends SwingActor with Reactor {
- def act() : Unit = react {
- //TODO - handle internal messages directly
- case msg =>
- reactions(ExternalMessage(msg))
- act()
- }
- }
- ---------------------------------------------------------------------------------------------
- package suereth.helper
- import scala.swing._
- import scala.actors.Actor._
- import scala.actors.Actor
- object TestActors {
- def main(args : Array[String]) {
- val text = new TextArea with ActorReactorBridge {
- text = "Initial Text"
- size = (300,300)
- var globalCount = 1
- reactions += {
- case ExternalMessage(x : String) =>
- text = (text + "\n" + "Global Msg #" + globalCount + ": " + x)
- globalCount += 1
- }
- }
- val frame = new MainFrame {
- frame =>
- size = (400, 400)
- title = "Test Actors"
- val panel = new Panel { panel =>
- _contents += text
- size = (400,400)
- }
- contents = panel
- }
- frame.visible = true
- def actorGenerator(id : Int) : Actor = actor {
- loop {
- react {
- case x : String =>
- Thread.sleep( (Math.random * 10).toLong)
- text ! ("Actor#" + id + ": " + x)
- }
- }
- }
- val actors = (1 to 9).map(actorGenerator).toList
- for(i <- 1 to 5; a <- actors) {
- a ! ("Msg#" + i)
- }
- ()
- //TODO - Wait for scala actors to wind down and check output
- }
- }
Add Comment
Please, Sign In to add comment