Building Applications With Pure Scala

Henri Kerola Vaadin Expert at Vaadin Ltd

26. lokakuuta 12 26. lokakuuta 12 ? Vaadin is a UI framework for rich web applications

26. lokakuuta 12 26. lokakuuta 12 html

26. lokakuuta 12 26. lokakuuta 12 26. lokakuuta 12 26. lokakuuta 12 26. lokakuuta 12 26. lokakuuta 12 Internet Explorer Chrome Firefox Safari Opera iOS Android

26. lokakuuta 12 No browser plugins Nothing to install

26. lokakuuta 12 Scala

26. lokakuuta 12 Scala

A programming language created by Martin Odersky

Runs on JVM and fully compatible with Java

OO + FP

26. lokakuuta 12 Statically typed

Type inference

val hello = "Hello World!" // type is String val hello: String = "Hello World!"

var i = 3 // type is Int i = "hi!" // doesn’t work

26. lokakuuta 12 Compact

public class Person { class Person(val name: String, val age: Int) private final String name; private final int age;

public Person(String name, int age) { this.name = name; this.age = age; }

public String getName() { return name; }

public int getAge() { return age; } }

26. lokakuuta 12 Compact

List persons = ... val persons = ... List adults = new ArrayList(); val adults = persons.filter(_.age >= 18) for (Person person : persons) { if (person.getAge() >= 18) { adults.add(person); } }

26. lokakuuta 12 OK, you can use Vaadin Framework with Scala, but...

26. lokakuuta 12 class MyApplication extends Application { def init() { setMainWindow(new Window)

val button = new Button("Click", new Button.ClickListener { def buttonClick(event: Button#ClickEvent) { getMainWindow.showNotification("Button clicked!") } }) getMainWindow.addComponent(b) } }

26. lokakuuta 12 Java style API

Listeners as anonymous inner classes

Verbose

26. lokakuuta 12 Scaladin

26. lokakuuta 12 Pure and compact Scala API for Vaadin Framework

26. lokakuuta 12 By wrapping Vaadin API

26. lokakuuta 12 Wrapping

package vaadin.scala

class Button extends AbstractField {

val p = new com.vaadin.ui.Button

def disableOnClick: Boolean = p.isDisableOnClick def disableOnClick_=(disableOnClick: Boolean) = p.setDisableOnClick(disableOnClick)

}

val button = new Button button.disableOnClick = true val bisableOnClick = button.disableOnClick

26. lokakuuta 12 com.vaadin.ui vaadin.scala

AbstractComponent AbstractComponent

AbstractField AbstractField

AbstractSelect AbstractSelect

Table Table

26. lokakuuta 12 final Table table = new Table(); table.setSizeFull(); table.setImmediate(true); table.setSelectable(true); table.setContainerDataSource(createContainer()); table.addListener(new ValueChangeListener() { @Override public void valueChange(ValueChangeEvent e) { System.out.println("Table" + e.getProperty() + "clicked!"); } });

val table = new Table { table sizeFull().sizeFull() table immediate.immediate = true = true table selectionMode.selectionMode = SelectionMode. = SelectionMode.SingleSingle table container.valueChangeListeners = createContainer += { e => valueChangeListenersprintln("Table " + e. property+= { ee: => ValueChangeEvent+ " clicked!") => } println("Table "clicked!" + e.property) + " clicked!") } }

26. lokakuuta 12 val layout = new VerticalLayout { sizeFull()

add(new Table { sizeFull() valueChangeListeners += { edit.enabled = value.isDefined VerticalLayout layout = new VerticalLayout(); } layout.setSizeFull(); }, ratio = 1)

final Button edit = new Button("Edit row"); val edit = add(Button("Edit"), alignment = BottomRight) } final Table table = new Table(); table.setSizeFull();

table.addListener(new ValueChangeListener() { @Override public void valueChange(ValueChangeEvent event) { edit.setEnabled(table.getValue() != null); } }); layout.addComponent(table); layout.setExpandRatio(table, 1); layout.addComponent(edit); layout.setComponentAlignment(edit, BOTTOM_RIGHT);

26. lokakuuta 12 Example

26. lokakuuta 12 26. lokakuuta 12 26. lokakuuta 12 Tools

Scala 2.9 (and Java)

Eclipse and Scala IDE

Scaladin 2.1.0 “JDD” ja Vaadin 6.8.4

26. lokakuuta 12 Tools

SBT (Simple Build Tool)

Akka

26. lokakuuta 12 Questions?https://github.com/henrikerola/[email protected] Comments? @henrikerola

26. lokakuuta 12