User Tools

Site Tools


Sidebar

Dave Orme muses about agile and functional programming.

My current work emphasizes SOA applications using Scala, Kubernetes, and AWS with a React-based SPA front-end. I'm also interested in progressive web applications and developer tools.


Blog

Scala, Clojure, and FP

Agile

The Cloud

Data-First Development

Older work

Coconut Palm Software home


Donate Bitcoin:

1Ecnr9vtkC8b9FvmQjQaJ9ZsHB127UzVD6

Keywords:

Kubernetes, Docker, Streaming Data, Spark, Scala, Clojure, OSGi, Karaf, GCP, AWS, SQL

Disclaimer:

Everything I say here is my own opinion and not necessarily that of my employer.

blog:more_xscalawt

More XScalaWT

Here are some more Scala and XScalaWT examples, from recent experiments, for those who prefer simple code examples and don't want to read through the longer tutorial.

(As in the previous examples, I'll omit the package statement and imports.)

class LoginDialog(display : Display) extends Shell(display, SWT.NO_TRIM) {
  implicit def unboxText2String(t : Text) = t.getText() 
 
  var username : Text = null
  var password : Text = null
  var ok = false
 
  setMaximized(true)  // since we're running inside RAP...
 
  this.contains(
    _.setLayout(new GridLayout(1, false)),
    _.setBackground(WHITE),
 
    $[Banner](SWT.NULL)(
      _.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false))
    ),
 
    label("Username", _.setBackground(WHITE)),
    text(username=_),
 
    label("Password", _.setBackground(WHITE)),
    $[Text](SWT.BORDER | SWT.PASSWORD)(password=_),
 
    composite(
      _.setLayout(new GridLayout(2, true)),
      _.setLayoutData(new GridData(SWT.CENTER, SWT.NONE, false, false)),
 
      _.setBackground(WHITE),
      button("OK",
        _.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)),
        { e : SelectionEvent => ok=true; getShell().dispose() }
      ),
      button("Cancel",
        _.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_FILL)),
        { e : SelectionEvent => getShell().dispose() }
      )
    )
  )
 
  def getCredentials = if (ok) new UserSession(username, password) else null
}

The example above shows the generic syntax that lets you access all of SWT, plus any custom controls that you might write. For example:

$[Text](SWT.BORDER | SWT.PASSWORD)(password=_),

and

$[Banner](SWT.NULL)(
  _.setLayoutData(new GridData(SWT.FILL, SWT.NONE, true, false))
),

…which leads to the following:

class Banner(parent : Composite, style : Int) extends Composite(parent, style) {
 
  this.contains (
    _.setLayout(new GridLayout(3, true)),
    _.setBackground(WHITE),
 
    label(LOGO.image, _.setBackground(WHITE)),
    label(
      _.setLayoutData(new GridData(SWT.FILL, SWT.END, true, false)),
      _.setBackground(WHITE)
    )
  )
 
}

And for those interested, UserSession looks like this:

class UserSession(var userName : String, var password : String) {
 
  def getPasswordMD5 = try {
    val algorithm = MessageDigest.getInstance("MD5")
    algorithm.reset
    algorithm.update(password.getBytes())
    val messageDigest = algorithm.digest()
 
    val digestBuffer = messageDigest.foldLeft(new StringBuffer()) { 
      (stringBuffer, byte) =>
      stringBuffer.append(Integer.toHexString(0xFF & byte))
    }
    digestBuffer.toString()
  } catch {
    case e : NoSuchAlgorithmException => throw new RuntimeException(e)
  }
 
}

Because the “userName” and “password” parameters are declared as “var” parameters, Scala will automatically turn them into bean properties with proper getters and setters. No more typing all that getFoo(), setFoo() boilerplate!

The only thing I don't like about this code is that I'm constantly setting the background color to SWT.WHITE. Looks like I need to look into how to style RAP widgets. Or maybe look into the new E4 style sheet support for SWT…

~~LINKBACK~~ ~~DISCUSSION~~

blog/more_xscalawt.txt · Last modified: 2014/10/17 22:08 (external edit)