1
+ package vaadin .scala .example .mongo
2
+
3
+ import vaadin .scala ._
4
+ import com .mongodb .casbah .Imports ._
5
+ import com .novus .salat ._
6
+ import com .novus .salat .global ._
7
+ import scala .util .Random
8
+ import scala .reflect .BeanProperty
9
+
10
+ class MongoExampleMinimal extends Application (" Mongo & Vaadin, tied together with Scala" ) {
11
+
12
+ val registrations : MongoCollection = MongoConnection ()(" vaadin-scala-mongo-example" )(" registrations" )
13
+
14
+ def mapRegistrations : List [MinRegistration ] = registrations.map(grater[MinRegistration ].asObject(_)).toList
15
+
16
+ override val main : ComponentContainer = new HorizontalLayout {
17
+ sizeFull()
18
+ styleName = Reindeer .LAYOUT_WHITE
19
+
20
+ val tableLayout = new VerticalLayout {
21
+ size(50 pct, 50 pct)
22
+ spacing = true
23
+
24
+ val table = new Table {
25
+ sizeFull()
26
+ styleNames += (Reindeer .TABLE_BORDERLESS , Reindeer .TABLE_STRONG )
27
+ container = new BeanItemContainer [MinRegistration ](registrations.map(grater[MinRegistration ].asObject(_)).toList)
28
+ visibleColumns = Seq (" username" , " realName" )
29
+ }
30
+
31
+ val addButton : Button = Button (" Register" , showForm)
32
+
33
+ components += (table, addButton)
34
+ }
35
+
36
+ val form = new Form {
37
+ size(50 pct, 50 pct)
38
+ caption = " Registration"
39
+ formFieldFactory = createFormFieldFactory
40
+ }
41
+
42
+ form.footer = new HorizontalLayout {
43
+ components += Button (" Save" , showList)
44
+ }
45
+
46
+ components += tableLayout
47
+
48
+ alignment(tableLayout -> Alignment .MiddleCenter )
49
+
50
+ def showForm (): Unit = {
51
+ form.item = new BeanItem [MinRegistration ](MinRegistration ())
52
+ form.visibleItemProperties = Seq (" realName" , " username" , " password" )
53
+ replaceComponent(tableLayout, form)
54
+ alignment(form -> Alignment .MiddleCenter )
55
+ }
56
+
57
+ def showList (): Unit = {
58
+ form.commit
59
+ val bean = form.item.get.asInstanceOf [BeanItem [MinRegistration ]].bean
60
+ registrations.save(grater[MinRegistration ].asDBObject(bean))
61
+ tableLayout.table.container = new BeanItemContainer [MinRegistration ](mapRegistrations)
62
+ tableLayout.table.visibleColumns = Seq (" username" , " realName" )
63
+ replaceComponent(form, tableLayout)
64
+ alignment(tableLayout -> Alignment .MiddleCenter )
65
+ mainWindow.showNotification(" User %s registered" .format(bean.username))
66
+ }
67
+ }
68
+
69
+ def createFormFieldFactory = FormFieldFactory (_ match {
70
+ case FormFieldIngredients (_, " password" , _) =>
71
+ Some (new PasswordField {
72
+ caption = DefaultFieldFactory .createCaptionByPropertyId(" password" )
73
+ required = true
74
+ })
75
+
76
+ case otherIngredient => {
77
+ val field = DefaultFieldFactory .createField(otherIngredient)
78
+ field.foreach(_.required = true )
79
+ field
80
+ }
81
+ })
82
+ }
83
+
84
+ case class MinRegistration (
85
+ @ BeanProperty var username : String = " username" + Random .nextInt,
86
+ @ BeanProperty var password : String = " " ,
87
+ @ BeanProperty var realName : String = " Joe Tester" )
0 commit comments