Scala Swing Wait - PullRequest
       16

Scala Swing Wait

1 голос
/ 16 сентября 2010
//Main.scala
/* imports */

object Main extends SimpleSwingApplication {
  lazy val ui = new TabbedPane {
    /* contents */
  }

  def top = new MainFrame {
    /* contents */
  }

  override def startup(args: Array[String]) {
    val t = top

    val loginStatus = new Login(t).status

    if (loginStatus == true) {
      if (t.size == new Dimension(0, 0)) t.pack
      t.visible = true
    } else
      quit
  }
}

//Login.scala
class Login(owner: Window) extends Dialog(owner) {
  import Login._

  var status = true
  contents = ui

  listenTo(login) //login is a Button
  reactions += {
    case ButtonClicked(login) => {
      if (/* login field is empty */)
        status = false
      else if (/* login info is correct */)
        status = true
      else /*login info is wrong*/
        status = false
    }
  }
}

Как я могу настроить Main для ожидания входа в систему, прежде чем отобразить себя?

1 Ответ

4 голосов
/ 16 сентября 2010

Используйте CountDownLatch:

//Main.scala
/* imports */

object Main extends SimpleSwingApplication {
  private val latch = new CountDownLatch(1)

  lazy val ui = new TabbedPane {
    /* contents */
  }

  def top = new MainFrame {
    /* contents */
  }

  override def startup(args: Array[String]) {
    val t = top

    val loginDialog = new Login(t, latch)
    latch.await

    val loginStatus = loginWindow.status

    if (loginStatus == true) {
      if (t.size == new Dimension(0, 0)) { t.pack }
      t.visible = true
    } else
      quit
  }
}

//Login.scala
class Login(owner: Window, latch: CountDownLatch) extends Dialog(owner) {
  import Login._

  var status = true
  contents = ui

  listenTo(login) //login is a Button
  reactions += {
    case ButtonClicked(login) => {
      latch.countdown

      if (/* login field is empty */)
        status = false
      else if (/* login info is correct */)
        status = true
      else /*login info is wrong*/
        status = false
    }
  }
}
...