Skip to content

Instantly share code, notes, and snippets.

@ripla
Created August 16, 2012 19:23
Show Gist options
  • Save ripla/3372871 to your computer and use it in GitHub Desktop.
Save ripla/3372871 to your computer and use it in GitHub Desktop.
Scaladin registration example 1-3: added application logic
import vaadin.scala._
import com.mongodb.casbah.Imports._
import com.novus.salat.grater
import com.novus.salat.global._
import scala.util.Random
import scala.reflect.BeanProperty
class MongoExample extends Application("Mongo & Vaadin, tied together with Scala") {
val registrations: MongoCollection = MongoConnection()("vaadin-scala-mongo-example")("registrations")
def mapRegistrations: List[Registration] = registrations.map(grater[Registration].asObject(_)).toList
def saveRegistration(registration: Registration): Unit = registrations += grater[Registration].asDBObject(registration)
override val main: ComponentContainer = new VerticalLayout {
sizeFull()
styleName = Reindeer.LAYOUT_WHITE
val tableLayout = new VerticalLayout {
size(50 pct, 50 pct)
spacing = true
val table = new Table {
sizeFull()
styleNames += (Reindeer.TABLE_BORDERLESS, Reindeer.TABLE_STRONG)
container = new BeanItemContainer(mapRegistrations)
visibleColumns = Seq("username", "realName")
}
val addButton = Button("Register", showForm)
components += (table, addButton)
}
lazy val form = new Form {
size(50 pct, 50 pct)
caption = "Registration"
footer = new HorizontalLayout {
components += Button("Save", showList)
}
}
components += tableLayout
alignment(tableLayout -> Alignment.MiddleCenter)
def showForm(): Unit = {
form.item = new BeanItem(Registration())
form.visibleItemProperties = Seq("realName", "username", "password")
replaceComponent(tableLayout, form)
alignment(form -> Alignment.MiddleCenter)
}
def showList(): Unit = {
if(form.commit.isValid) { //form handles error
val bean = form.item.get.asInstanceOf[BeanItem[Registration]].bean
saveRegistration(bean)
tableLayout.table.container = new BeanItemContainer(mapRegistrations)
tableLayout.table.visibleColumns = Seq("username", "realName")
replaceComponent(form, tableLayout)
alignment(tableLayout -> Alignment.MiddleCenter)
mainWindow.showNotification("User %s registered".format(bean.username))
}
}
}
case class Registration(
@BeanProperty var username: String = "username" + Random.nextInt,
@BeanProperty var password: String = "",
@BeanProperty var realName: String = "Joe Tester")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment