Skip to content

Instantly share code, notes, and snippets.

@tomasherman
Created November 4, 2011 19:05
Show Gist options
  • Save tomasherman/1340196 to your computer and use it in GitHub Desktop.
Save tomasherman/1340196 to your computer and use it in GitHub Desktop.
Scala swing
package net.tomasherman.replayvault.client.gui
import swing._
import event.{EditDone, ButtonClicked}
import net.miginfocom.swing.MigLayout
import javax.swing.BorderFactory
import java.awt.Color
import java.util.Arrays
import akka.actor.Actor
class MigPanel(constraints: String, colConst: String, rowConst: String) extends Panel with LayoutContainer {
def this(constraints: String) = this (constraints, "", "")
type Constraints = String
def layoutManager = peer.getLayout.asInstanceOf[MigLayout]
override lazy val peer = new javax.swing.JPanel(new MigLayout(constraints, colConst, rowConst)) with SuperMixin
override def contents: MigContent = new MigContent
protected class MigContent extends Content {
def +=(c: Component, l: Constraints) = add(c, l)
}
protected def constraintsFor(comp: Component) =
layoutManager.getConstraintMap.get(comp.peer).toString
protected def areValid(c: Constraints): (Boolean, String) = (true, "")
protected def add(c: Component, l: Constraints) = peer.add(c.peer, l)
}
object LoginMVC {
//def init = { println("trying view");view.init; println("trying contriller");controller.init; this}
val view = new View
val controller = Actor.actorOf(new Controller(view)).start()
class View extends MigPanel("") {
def init = {println("initializing view")}
val emailL = new Label() {
text = "Email: "
}
val passwordL = new Label() {
text = "Password: "
}
val passwordRepL = new Label() {
text = "Repeat password: "
}
val emailI = new TextField()
val passwordI = new PasswordField()
val passwordRepI = new PasswordField()
val reset = new Button("Reset")
val bar = new ProgressBar {
indeterminate = true
visible = false
labelPainted = true
label = "Magic in progress"
}
val submit = new Button("Submit") {
enabled = false
}
List(emailL, emailI, passwordL, passwordI, passwordRepL, passwordRepI, reset, submit).zipWithIndex foreach {
x =>
contents +=(x._1, if (x._2 % 2 == 1) "wrap, grow" else "grow")
}
contents +=(bar, "span, grow")
border = Swing.EmptyBorder(10, 5, 10, 5)
}
class Controller(val view: View) extends Reactor with Actor{
protected def receive = null
import view._
val onSubmitValidation = List(validateEmail, validatePassword, passwordsMatch)
// these functions return an validation output and the guy who should be blamed(highlighted) should the validation fail
def validateEmail = () => (true, emailI)
def validatePassword = () => (passwordI.password.length >= 4, passwordI)
def passwordsMatch = () => (Arrays.equals(passwordI.password, passwordRepI.password), passwordRepI)
def testAndHighlight(test: () => (Boolean, TextField)) = {
val res = test()
res._2.border = if (!res._1) {
BorderFactory.createLineBorder(Color.red)
} else {
BorderFactory.createLineBorder(Color.black)
}
res._1
}
listenTo(passwordI, passwordRepI, emailI, reset, submit)
reactions += {
case ButtonClicked(b) if b == reset => {
println("buttonclicked")
passwordI.text = ""
passwordRepI.text = ""
emailI.text = ""
}
case ButtonClicked(b) if b == submit => {
bar.visible = true
}
case EditDone(x) => {
submit.enabled = (true /: onSubmitValidation) {(b, func) =>
b && testAndHighlight(func)
}
}
}
}
}
object Application extends SimpleSwingApplication {
def top = new MainFrame {
title = "Replaut Vault"
resizable = false
contents = LoginMVC.view
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment