summaryrefslogtreecommitdiffstats
path: root/documentation/getting-started/getting-started-scala.asciidoc
blob: f31a16b1e8e42bd8539d8b56e4f3446d04a7afb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
---
title: Using Vaadin with Scala
order: 1000
layout: page
---

[[getting-started.scala]]
= Using Vaadin with Scala

You can use Vaadin with any JVM compatible language, such as Scala or Groovy.
There are, however, some caveats related to libraries and project set-up. In the
following, we give instructions for creating a Scala UI in Eclipse, with the
Scala IDE for Eclipse and the Vaadin Plugin for Eclipse.

. Install the link:http://scala-ide.org/[Scala IDE for Eclipse], either from an
Eclipse update site or as a bundled Eclipse distribution.

. Open an existing Vaadin Java project or create a new one as outlined in
<<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project,"Creating
and Running a Project with Eclipse">>. You can delete the UI class created by
the wizard.

. Switch to the Scala perspective by clicking the perspective in the upper-right
corner of the Eclipse window.

. Right-click on the project folder in [guilabel]#Project Explorer# and select
"Configure > Add Scala Nature".

. The web application needs [filename]#scala-library.jar# in its class path. If
using Scala IDE, you can copy it from somewhere under your Eclipse installation
to the class path of the web application, that is, either to the
[filename]#WebContent/WEB-INF/lib# folder in the project or to the library path
of the application server. If copying outside Eclipse to a project, refresh the
project by selecting it and pressing kbd:[F5].

+
You could also get it with an Ivy or Maven dependency, just make sure that the
version is same as what the Scala IDE uses.


You should now be able to create a Scala UI class, such as the following:


[source, scala]
----
@Theme("mytheme")
class MyScalaUI extends UI {
  override def init(request: VaadinRequest) = {
    val content: VerticalLayout = new VerticalLayout
    setContent(content)

    val label: Label = new Label("Hello, world!")
    content addComponent label

    // Handle user interaction
    content addComponent new Button("Click Me!",
      new ClickListener {
        override def buttonClick(event: ClickEvent) =
          Notification.show("The time is " + new Date)
      })
  }
}
----

Eclipse and Scala IDE should be able to import the Vaadin classes automatically
when you press kbd:[Ctrl+Shift+O].

You need to define the Scala UI class either in a servlet class (in Servlet 3.0
project) or in a [filename]#web.xml# deployment descriptor, just like described
in
<<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project.exploring,"Exploring
the Project">> for Java UIs.

ifdef::web[]
The link:https://github.com/henrikerola/scaladin[Scaladin add-on] offers a more
Scala-like API for Vaadin. A Vaadin 7 compatible version is under development.
endif::web[]

ifdef::web[]
[[getting-started.scala.lambdas]]
== Defining Listeners with Lambda Expressions

Scala does not support use of lambda expressions for calling functional
interfaces, like Java 8 does. Hence, we can't just use a lambda expression for
the [interfacename]#ClickListener# in the example above. You can, however,
define implicit conversions from lambda expressions to such interface
implementations. For example, for click listeners:


[source, scala]
----
implicit def clickListener(f: ClickEvent => Unit) =
  new ClickListener {
    override def buttonClick(event: ClickEvent) {
      f(event)
    }
  }
----

You could then use a lambda expression as follows:


[source, scala]
----
content addComponent new Button("Click Me!",
  (event: ClickEvent) =>
      Notification.show("The time is " + new Date))
----

endif::web[]