Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Vaadin and Scala

Henri Muurimaa
1 decade ago Jun 20, 2011 12:25pm
Henri Muurimaa
1 decade ago Jun 22, 2011 10:38am

To throw a few possible ideas out there:

  • Provide a method that can be used to configure a component without having to spread the different options (caption, size, immediateness, ...) across several lines.
  • Provide a clean way to supply anonymous functions to component event listeners.
  • Provide methods to ComponentContainers to add and configure the properties (alignment, expand ratio, ...) of child components.
  • Provide a clean way to access and manipulate child component sets of ComponentContainers.

These can be implemented in several ways, eg. as traits or per component basis with subclasses.

What else?

Last updated on Jun, 22nd 2011
Steve Thompson
1 decade ago Jun 22, 2011 8:02pm

Another area where Scala's features might be taken advantage of is its support for XML. Here is a simple example:

addComponent(new Label(<span>This is a <em>label</em> with a <b>HTML</b> caption</span>))

I'll try to add more comprehensive support for this to the library that I've begun on github

Best regards as always,

Steve

Last updated on Jun, 22nd 2011
Fredrik Jonsson
1 decade ago Jun 22, 2011 10:22pm
Steve Thompson
1 decade ago Jun 23, 2011 2:38am
Fredrik Jonsson
1 decade ago Jun 23, 2011 7:45am
Henri Muurimaa
1 decade ago Jun 23, 2011 7:50am
Henri Muurimaa
1 decade ago Jun 23, 2011 8:05am
Steve Thompson
1 decade ago Jun 23, 2011 11:43am
Guillaume Belrose
1 decade ago Jun 27, 2011 2:48pm
Henri Muurimaa
1 decade ago Jul 01, 2011 11:46am
Guillaume Belrose
1 decade ago Jul 04, 2011 1:01pm
Henri Kerola
1 decade ago Jul 05, 2011 2:33pm
Oleg Aleshko
1 decade ago Jul 05, 2011 3:06pm
Henri Kerola
1 decade ago Jul 06, 2011 6:49am
Oleg Aleshko
1 decade ago Jul 08, 2011 1:56pm
Guillaume Belrose
1 decade ago Jul 12, 2011 8:07am
Henri Muurimaa
1 decade ago Jul 12, 2011 12:10pm
Guillaume Belrose
1 decade ago Jul 13, 2011 8:22am
Risto Yrjänä
1 decade ago Sep 11, 2011 6:50pm

I created an implementation based on that selector idea,. Basically it allows you to do stuff like

val list = layout \ (c => c.getStyleName == "foo")

which selects matching components from the layout (the \\ -method would select also from it's children).

Last updated on Sep, 11th 2011
Henri Muurimaa
1 decade ago Oct 03, 2011 10:21am
Henri Kerola
1 decade ago Nov 14, 2011 8:58am

Just released version 0.1 of scala-wrappers in Vaadin Directory!

Last updated on Nov, 14th 2011
Steve Thompson
1 decade ago Nov 23, 2011 12:57pm
Oleg Aleshko
1 decade ago Nov 27, 2011 6:41pm
Henri Muurimaa
1 decade ago Nov 28, 2011 6:47am
Henri Kerola
1 decade ago Nov 28, 2011 10:11am
Henri Kerola
1 decade ago Nov 28, 2011 1:56pm

Henri Kerola: That's a good idea. I'll move the source codes to github.

scala-wrappers can be found from github now: https://github.com/henrikerola/scala-wrappers!

Last updated on Nov, 28th 2011
Henri Muurimaa
1 decade ago Dec 12, 2011 7:47am
Henri Muurimaa
1 decade ago Dec 12, 2011 1:49pm
Łukasz Krenski
1 decade ago Feb 15, 2012 8:29pm
Henri Muurimaa
1 decade ago Feb 16, 2012 12:51pm
Helder Batista
1 decade ago Mar 27, 2012 10:31pm

Following the discussion on this StackOverflow question, specifically Henri's answer (hezamu), here's what I'm doing on my current Vaadin + Scala project.

I'm not using scala-wrapper because when I started it was still in a very early stage, and also I wanted to improve my Scala by making my own wrapper.

My goal has been to create the MINIMUM amount of code to make Vaadin nicer to use in Scala.
Also, I'm relying ONLY on implicit conversions (aka pimp-my-library pattern), thus avoiding sub-classing Vaadin's components.

Main implicits: defining listeners. Here's a sample:

//button click listener
 class PimpedButton[B <: {def addListener(listener:Button.ClickListener):Unit} ](btn:B ){
      def onClick(fn: Button#ClickEvent  => Unit):B = {
        btn.addListener( new Button.ClickListener(){
	       def buttonClick(event:Button#ClickEvent) = fn(event)
	     })
	  btn
      }
	def onClick(fn: => Unit):B = {
	  btn.addListener( new Button.ClickListener(){
	       def buttonClick(event:Button#ClickEvent) = fn
	     })
	  btn
	}
 }
   
 implicit def pimpButton[B <: {def addListener(listener:Button.ClickListener):Unit}](btn:B) = new PimpedButton(btn)

Using it:

val btn = new Button("do stuff") onClick {
  //do some fancy logic here
}

Notes:
- I'm using duck typing, thus pimping any classes that define an addListener that accepts a ClickListener. This way I don't have to keep track of what classes need this pimp.
- I'm returning the object on the onClick methods, this makes it more convenient to use directly on object creation
- I'm defining two onClick variants, one that receives the event object, and another without it, this is just for code simplicity when I don't need the event object (which is most of the time)

I'm defining similar implicits for types that have:

addListener(listener : Property.ValueChangeListener)
addListener(listener : ItemClickListener)
addListener(listener : TextChangeListener)
addListener(listener : MouseEvents.ClickListener)
addListener(listener : FragmentChangedListener)

Another nice pimp, which I dubbed "anti-repetition":

class PimpedAddComponent(obj:{def addComponent(c:Component):Unit}) {
      def addComponents(comps:Component*) = comps foreach {c=>obj.addComponent(c)}
   }
   
   implicit def pimpAddComponent(obj:{def addComponent(c:Component):Unit}) = new PimpedAddComponent(obj)

The idea is to avoid this kind of boring repetition:

class SomeComponent {
  addComponent(x)
  addComponent(y)
  addComponent(z)
} 

and replace it with:

class SomeComponent {
  addComponents(x,y,z)
}

much nicer :)

And adding collections of components comes for free, for example:

val l = List("label text 1","label text 2","label text 3")
addComponents(l map {new Label(_)} : _*)

I've created an implicit like this also for types defining:

addItem(a:Any)

That's basically it (there's a couple other things, like the Upload component listeners). It's not much, but it's enough to remove the most annoying boilerplate.

PS: maybe I should have put this up on GitHub or something, but I didn't think it was worthy of such :)

Last updated on Mar, 27th 2012
Henri Muurimaa
1 decade ago Mar 28, 2012 10:01am
Helder Batista
1 decade ago Mar 28, 2012 11:28am
Joonas Lehtinen
1 decade ago Mar 30, 2012 8:24am
Oleg Aleshko
1 decade ago Mar 31, 2012 11:20pm
Risto Yrjänä
1 decade ago Aug 22, 2012 3:59pm

I posted a tutorial + example app on Vaadin, Scala and MongoDB integration: Tutorial & app @ GitHub. The example app that the tutorial uses is just ~100 lines of code in a single file, but the repo also contains a more fully fledged example which is properly broken into different classes in different files. Note that the tutorial uses the API of the brand new Scaladin 2.0.

I also updated the Giter8 template to use Scaladin 2.0.

Last updated on Aug, 22nd 2012
King Chiu
1 decade ago Aug 31, 2012 6:39am
Henri Kerola
1 decade ago Aug 31, 2012 7:52am
Matti Heinola
1 decade ago Sep 26, 2012 10:29am