Scala examples

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

It’s a very interesting idea, but I’m afraid that the Vaadin R&D team hasn’t officially adopted Scala as a supported language (yet!). Amongst other things this means that the official examples will be in Java only at this point.

What does the community think, should we adopt Scala as a officially supported language? One clear downside is that this would require a lot of effort, and it would definitely affect how fast we can release
other new stuff
.

Hi,

I am not sure to understand your answer because it is already a supported language, I mean nothing prevents me from using scala
to call vaadin api. Scala and java works with the same base technology after all (the java VM) so I don’t understand your point.
While I am at it, I suppose I could even use JRuby…

Yes, since Vaadin is a server side framework it works with any JVM language that offers sufficient Java interop (basically all of them).

What I meant was that our R&D team works only with Java at the moment, and we don’t do anything to support the other languages.

Hi Vincent R,

you can use the Intellij IDEA Scala Plugin to convert Java code to Scala code. Not perfect, but for the major part of lines it works.

Cheers,
Twistleton

If you are looking for more examples about Vaadin and Scala, there is the code for my Devoxx presentation. More info here:
https://vaadin.com/forum/-/message_boards/view_message/530126#_19_message_976577