This year I had found chance to get my hands dirty with
Vaadin. Given the fact that this was the first time I
was exposed to a GWT-based web framework using Scala, I tried various coding
conventions, utility functions, shortcuts, etc. in the beginning. Over the
time, I came up with some common conventions that I employ throughout the code
base. In this post, I will share some of these custom tricks I developed along
this pursuit.

“Returning” the First Value

While creating a certain UI component, what a programmer occasionally performs
is to 1) instantiate the class, 2) set certain properties, and 3) return/use
the instance.

This pattern was so idiomatic and repetitive throughout the code base that I
thought having something similar to prog1 in Common
Lisp
would be really helpful for structuring similar components. Hence, I came up
with my own returning utility function as follows:

Reading Nullable Text Fields

While coding in Scala, I write my code as if there are no nulls. And
whenever there is an external API that I need to communicate and has potential
to return null, I wrap it in an Option. This practice also applies to
getValue method of TextFields in Vaadin. Hence, I purposed a common
function to read from text fields:

Per see, the namespace of the class gets polluted as you add more fields. That
is, in order to implement a form of 10 fields, you end up with at least
3x10=30 class variables/methods. In order to mitigate this problem, I wrote a
custom trait to group component accessors into a single field:

overridedefuploadFinished(event:FinishedEvent):Unit=confirmationDialog("Deploy Shelf Plan","""You are about to populate tables using the provided shelf plan.<br/> |Do you want to proceed?""".stripMargin,ok,cancel)

“Escapeable” Window

So you have a Window, that you want to be closeable using the ESCAPE
key. Fine, just extend from EscapeableWindow:

Boolean Combo Box

For plain boolean options, you can just simply go with a check box. But if you
have a nullable boolean field, you need a representation to encapsulate three
different states: 1) true, 2) false, and 3) null. For that purpose, I use a
combo box as follows:

Tables

When I first started working with tables in Vaadin, I – as probably everybody
else in this business – felt the urge to abstract away the repetitive
patterns while creating a table. And I came up with the following
CustomTableHeader and TableHelpers utility classes.