Hi,
I have started my first vaadin project using scala by following the tutorials and I think it would be interesting to
propose on your website java and scala examples.
Would it be possible on your website to add a tab above each source code allowing to choose between scala or java.
Here is the scala code for address book example :
package com.vaadin.demo.simpleaddressbook
import com.vaadin.Application
import com.vaadin.data.Property
import com.vaadin.data.Property.ValueChangeEvent
import com.vaadin.data.util.IndexedContainer
import com.vaadin.ui._
import java.util.Date
// Static constants and methods
object SimpleAddressBook {
private val fields = Array(
"First Name", "Last Name", "Company",
"Mobile Phone", "Work Phone", "Home Phone", "Work Email",
"Home Email", "Street", "Zip", "City", "State", "Country" )
private val visibleCols = Array("Last Name", "First Name", "Company")
private def createDummyData(): IndexedContainer = {
val fnames = Array(
"Peter", "Alice", "Joshua", "Mike", "Olivia",
"Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene",
"Lisa", "Marge" )
val lnames = Array(
"Smith", "Gordon", "Simpson", "Brown", "Clavel",
"Simons", "Verne", "Scott", "Allison", "Gates", "Rowling",
"Barks", "Ross", "Schneider", "Tate" )
val ic = new IndexedContainer()
fields.foreach(p => ic.addContainerProperty(p, classOf[String]
, “”))
// Create dummy data by randomly combining first and last names
for (i <- 0 until 1000) {
val rnd = new scala.util.Random()
val id = ic.addItem()
ic.getContainerProperty(id, "First Name").setValue(fnames((fnames.length * rnd.nextFloat()).toInt))
ic.getContainerProperty(id, "Last Name").setValue(lnames((lnames.length * rnd.nextFloat()).toInt))
}
return ic
}
}
class SimpleAddressBook extends Application {
private var contactList = new Table()
private var contactEditor = new Form()
private var bottomLeftCorner = new HorizontalLayout()
private var contactRemovalButton = new Button()
private var addressBookData = SimpleAddressBook.createDummyData()
override def init(): Unit = {
initLayout()
initContactAddRemoveButtons()
initAddressList()
initFilteringControls()
}
def initLayout(): Unit = {
val splitPanel = new SplitPanel(SplitPanel.ORIENTATION_HORIZONTAL)
setMainWindow(new Window("Address Book", splitPanel))
val left = new VerticalLayout()
left.setSizeFull()
left.addComponent(contactList)
contactList.setSizeFull()
left.setExpandRatio(contactList, 1)
splitPanel.addComponent(left)
splitPanel.addComponent(contactEditor)
contactEditor.setCaption("Contact details editor")
contactEditor.setSizeFull()
contactEditor.getLayout().setMargin(true)
contactEditor.setImmediate(true)
bottomLeftCorner.setWidth("100%")
left.addComponent(bottomLeftCorner)
}
def initContactAddRemoveButtons(): Unit = {
// New item button
bottomLeftCorner.addComponent(
new Button("+",
new Button.ClickListener() {
def buttonClick(event: Button#ClickEvent) {
// Add new contact "John Doe" as the first item
val id = contactList.getContainerDataSource.asInstanceOf[IndexedContainer]
.addItemAt(0)
contactList.getItem(id).getItemProperty(“First Name”).setValue(“John”)
contactList.getItem(id).getItemProperty(“Last Name”).setValue(“Doe”)
// Select the newly added item and scroll to the item
contactList.setValue(id)
contactList.setCurrentPageFirstItemId(id)
}
}))
// Remove item button
contactRemovalButton = new Button("-",
new Button.ClickListener() {
def buttonClick(event: Button#ClickEvent) {
contactList.removeItem(contactList.getValue())
contactList.select(null)
}
})
contactRemovalButton.setVisible(false)
bottomLeftCorner.addComponent(contactRemovalButton)
}
def initAddressList(): Array[String]
= {
contactList.setContainerDataSource(addressBookData)
contactList.setVisibleColumns(SimpleAddressBook.visibleCols.asInstanceOf[Array[java.lang.Object]
])
contactList.setSelectable(true)
contactList.setImmediate(true)
contactList.addListener(new Property.ValueChangeListener() {
def valueChange(event: com.vaadin.data.Property.ValueChangeEvent) {
val id = contactList.getValue()
contactEditor.setItemDataSource(if (id != null) contactList.getItem(id) else null)
contactRemovalButton.setVisible(id != null)
}
})
return SimpleAddressBook.visibleCols : Array[String]
}
def initFilteringControls(): Unit = {
SimpleAddressBook.visibleCols.foreach(pn =>
{
var sf = new TextField()
bottomLeftCorner.addComponent(sf)
sf.setWidth("100%")
sf.setInputPrompt(pn)
sf.setImmediate(true)
bottomLeftCorner.setExpandRatio(sf, 1)
sf.addListener(new Property.ValueChangeListener() {
def valueChange(event: ValueChangeEvent) {
addressBookData.removeContainerFilters(pn)
if (sf.toString().length() > 0 && !pn.equals(sf.toString())) {
addressBookData.addContainerFilter(pn, sf.toString(),true, false)
}
getMainWindow().showNotification(
"" + addressBookData.size() + " matches found")
}
})
})
}
}
Thanks