You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getting-started-scala.asciidoc 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. ---
  2. title: Using Vaadin with Scala
  3. order: 1000
  4. layout: page
  5. ---
  6. [[getting-started.scala]]
  7. = Using Vaadin with Scala
  8. You can use Vaadin with any JVM compatible language, such as Scala or Groovy.
  9. There are, however, some caveats related to libraries and project set-up. In the
  10. following, we give instructions for creating a Scala UI in Eclipse, with the
  11. Scala IDE for Eclipse and the Vaadin Plugin for Eclipse.
  12. . Install the link:http://scala-ide.org/[Scala IDE for Eclipse], either from an
  13. Eclipse update site or as a bundled Eclipse distribution.
  14. . Open an existing Vaadin Java project or create a new one as outlined in
  15. <<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project,"Creating
  16. and Running a Project with Eclipse">>. You can delete the UI class created by
  17. the wizard.
  18. . Switch to the Scala perspective by clicking the perspective in the upper-right
  19. corner of the Eclipse window.
  20. . Right-click on the project folder in [guilabel]#Project Explorer# and select
  21. "Configure > Add Scala Nature".
  22. . The web application needs [filename]#scala-library.jar# in its class path. If
  23. using Scala IDE, you can copy it from somewhere under your Eclipse installation
  24. to the class path of the web application, that is, either to the
  25. [filename]#WebContent/WEB-INF/lib# folder in the project or to the library path
  26. of the application server. If copying outside Eclipse to a project, refresh the
  27. project by selecting it and pressing kbd:[F5].
  28. +
  29. You could also get it with a Maven dependency, just make sure that the
  30. version is same as what the Scala IDE uses.
  31. You should now be able to create a Scala UI class, such as the following:
  32. [source, scala]
  33. ----
  34. @Theme("mytheme")
  35. class MyScalaUI extends UI {
  36. override def init(request: VaadinRequest) = {
  37. val content: VerticalLayout = new VerticalLayout
  38. setContent(content)
  39. val label: Label = new Label("Hello, world!")
  40. content addComponent label
  41. // Handle user interaction
  42. content addComponent new Button("Click Me!",
  43. new ClickListener {
  44. override def buttonClick(event: ClickEvent) =
  45. Notification.show("The time is " + new Date)
  46. })
  47. }
  48. }
  49. ----
  50. Eclipse and Scala IDE should be able to import the Vaadin classes automatically
  51. when you press kbd:[Ctrl+Shift+O].
  52. You need to define the Scala UI class either in a servlet class (in Servlet 3.0
  53. project) or in a [filename]#web.xml# deployment descriptor, just like described
  54. in
  55. <<dummy/../../../framework/getting-started/getting-started-first-project#getting-started.first-project.exploring,"Exploring
  56. the Project">> for Java UIs.
  57. ifdef::web[]
  58. The link:https://github.com/henrikerola/scaladin[Scaladin add-on] offers a more
  59. Scala-like API for Vaadin. A Vaadin 7 compatible version is under development.
  60. endif::web[]
  61. ifdef::web[]
  62. [[getting-started.scala.lambdas]]
  63. == Defining Listeners with Lambda Expressions
  64. Scala does not support use of lambda expressions for calling functional
  65. interfaces, like Java 8 does. Hence, we can't just use a lambda expression for
  66. the [interfacename]#ClickListener# in the example above. You can, however,
  67. define implicit conversions from lambda expressions to such interface
  68. implementations. For example, for click listeners:
  69. [source, scala]
  70. ----
  71. implicit def clickListener(f: ClickEvent => Unit) =
  72. new ClickListener {
  73. override def buttonClick(event: ClickEvent) {
  74. f(event)
  75. }
  76. }
  77. ----
  78. You could then use a lambda expression as follows:
  79. [source, scala]
  80. ----
  81. content addComponent new Button("Click Me!",
  82. (event: ClickEvent) =>
  83. Notification.show("The time is " + new Date))
  84. ----
  85. endif::web[]