Skip to content
henrikerola edited this page Sep 7, 2012 · 2 revisions

This wiki page explains Scaladin 1.0 API. Scaladin 2.0 and newer versions have different API.


Constructors

Scaladin provides new constructors for all UI-components. They allow you to specify the most common component configuration options as named constructor parameters.

val panel = new Panel(caption = "Caption", width = 200 px, height = 300 px)

Component containers

All the ComponentContainers have an overridden add-method that returns the added component. With this and the named constructor parameters, you can create and configure layouts in a very concise way.

val layout = new VerticalLayout(caption = "Caption", width = 200 px, height = 300 px, spacing = true) {
    val header = add(new HtmlLabel(content = "<h1>Header</h1>", width = 100 percent))
    val list = add(component = new Table(selectable = false), ratio = 0.8)
}

If you need to, you can refer to the added components (in the same context) like this:

layout.list.setSelectable(true)

Listeners

One of the more obvious places where using Scala can improve the Vaadin experience is the usage of listeners. As a event-based framework Vaadin makes extensive use of listeners, and with the help from Scaladin using listeners is really easy.

button.addListener(_ => doSomething())
layout.addLayoutClickListener(event => handleLayoutClick(event))
table.addItemClickListener(event => tableItemClicked(event))

Or use a constructor parameter:

new Button(caption = "Does something", action = _ => doSomething())

Setting sizes

Scaladin contains implicit conversions for setting sizes. This helps to prevent typos as all units are checked by the compiler. For example:

label.setHeight(2 em)
new VerticalLayout(height = 500 px, width = 100 percent)

Filtering components

Scaladin defines the trait FilterableComponentContainer that implements filtering methods for ComponentContainers. filter filters the target ComponentContainer, while filterRecursive filters the whole component tree from the target. Both methods return a list of components. For example, to get all Label components:

layout filter(_.instanceOf[Label])

Or, to re-enable all disabled components from a hierarchy:

rootLayout filterRecursive(!_.isEnabled) forEach (_.setEnabled(true))

Containers, Items and Properties

Scaladin also defines some helpers and extensions for the data-classes in Vaadin. Containers, Items and Properties have their respective companion objects that define an apply method, much like the Scala collections. Container object creates IndexedContainers, Item creates PropertysetItems and Property ObjectProperties.

val property = Property("propertyValue")
val item = Item('propertyId1 -> "value1", 'propertyId2 -> "value2")
val container = Container('itemId1 -> List('propertyId1 -> "value1", 'propertyId2 -> "value2"), 'itemId2 -> List())

(The examples use Scala symbols as identifiers)

In addition to these convenient creators, Scaladin also has some implicit transformations from Containers and Items to filterable variants. You'll need to explicitly import them, but after that you can use the methods to extract properties like this:

import vaadin.scala.implicits._

val itemProperty: Property = item \ 'propertyId1
val itemProperties: List[Property] = item filterProperties     (_.getValue.asInstanceOf[String].startsWith("value"))
val containerProperty: Property = container \ 'itemId1 \ 'propertyId1
val containerProperties: List[Property] = container \\ 'propertyId1

Or just their values:

val itemPropertyValues: List[Any] = item filterProperties     (_.getValue.asInstanceOf[String].startsWith("value")) values
val containerPropertyValues: List[Any] = container \\ 'propertyId1 values

The related traits are FilterableContainer and FilterableItem.