Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with...

33
Henri Kerola Vaadin Expert at Vaadin Ltd Building Vaadin Applications With Pure Scala 26. lokakuuta 12

Transcript of Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with...

Page 1: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Henri KerolaVaadin Expert at Vaadin Ltd

Building Vaadin Applications With

Pure Scala

26. lokakuuta 12

Page 2: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

? 26. lokakuuta 12

Page 3: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Vaadin is a UI framework

for rich web applications

26. lokakuuta 12

Page 4: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

26. lokakuuta 12

Page 5: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

htmljava

26. lokakuuta 12

Page 7: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

26. lokakuuta 12

Page 8: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

26. lokakuuta 12

Page 11: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Internet ExplorerChromeFirefoxSafariOpera

iOSAndroid

26. lokakuuta 12

Page 12: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Nobrowserplugins

Nothing toinstall

26. lokakuuta 12

Page 13: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Scala

26. lokakuuta 12

Page 14: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Scala

OO + FP

Runs on JVM and fully compatible with Java

A programming language created by Martin Odersky

26. lokakuuta 12

Page 15: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Type inference

Statically typed

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

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

26. lokakuuta 12

Page 16: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Compact

public class Person { 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; }}

class Person(val name: String, val age: Int)

26. lokakuuta 12

Page 17: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Compact

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

val persons = ...val adults = persons.filter(_.age >= 18)

26. lokakuuta 12

Page 18: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

OK, you can use Vaadin Framework

with Scala, but...

26. lokakuuta 12

Page 19: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 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

Page 20: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Verbose

Listeners as anonymous inner classes

Java style API

26. lokakuuta 12

Page 21: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Scaladin

26. lokakuuta 12

Page 22: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Pure and compact Scala API for Vaadin Framework

26. lokakuuta 12

Page 23: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

By wrapping Vaadin API

26. lokakuuta 12

Page 24: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Wrappingpackage 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 Buttonbutton.disableOnClick = trueval bisableOnClick = button.disableOnClick

26. lokakuuta 12

Page 25: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Table

AbstractSelect

AbstractField

AbstractComponent

com.vaadin.ui

Table

AbstractSelect

AbstractField

AbstractComponent

vaadin.scala

26. lokakuuta 12

Page 26: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

final Table table = new Table();table.setSizeFull();table.setImmediate(true);table.setSelectable(true);table.setContainerDataSource(createContainer());table.addListener(new ValueChangeListener() {

@Overridepublic void valueChange(ValueChangeEvent e) {

System.out.println("Table" + e.getProperty() + "clicked!"); }

});

val table = new Tabletable.sizeFull()table.immediate = truetable.selectionMode = SelectionMode.Singletable.valueChangeListeners += { e => println("Table " + e.property + " clicked!") }

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

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

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

val table = new Table { sizeFull() immediate = true selectionMode = SelectionMode.Single container = createContainer valueChangeListeners += { println("Table clicked!") }}

26. lokakuuta 12

Page 27: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

VerticalLayout layout = new VerticalLayout();layout.setSizeFull(); final Button edit = new Button("Edit row");

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);

val layout = new VerticalLayout { sizeFull() add(new Table { sizeFull() valueChangeListeners += { edit.enabled = value.isDefined } }, ratio = 1) val edit = add(Button("Edit"), alignment = BottomRight)}

26. lokakuuta 12

Page 28: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Example

26. lokakuuta 12

Page 29: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

26. lokakuuta 12

Page 30: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

26. lokakuuta 12

Page 31: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Tools

Scaladin 2.1.0 “JDD” ja Vaadin 6.8.4

Eclipse and Scala IDE

Scala 2.9 (and Java)

26. lokakuuta 12

Page 32: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

Tools

Akka

SBT (Simple Build Tool)

26. lokakuuta 12

Page 33: Building Vaadin Applications With Pure Scala · Scala OO + FP Runs on JVM and fully compatible with Java A programming language created by Martin Odersky 26. lokakuuta 12

https://github.com/henrikerola/scaladinQuestions?Comments?

[email protected] @henrikerola

26. lokakuuta 12