User Tools

Site Tools


blog:more_xscalawt

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
blog:more_xscalawt [2009/02/17 20:52]
djo Clarified
blog:more_xscalawt [2014/10/17 22:08] (current)
Line 1: Line 1:
 +====== 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 [[Simplifying SWT with Scala|tutorial]].
 +
 +(As in the previous examples, I'll omit the package statement and imports.)
 +
 +<code java>
 +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
 +}
 +</​code>​
 +
 +The example above shows the generic syntax that lets you access all of SWT, plus any custom controls that you might write. ​ For example:
 +
 +<code java>
 +$[Text](SWT.BORDER | SWT.PASSWORD)(password=_),​
 +</​code>​
 +
 +and
 +
 +<code java>
 +$[Banner](SWT.NULL)(
 +  _.setLayoutData(new GridData(SWT.FILL,​ SWT.NONE, true, false))
 +),
 +</​code>​
 +
 +...which leads to the following:
 +
 +<code java>
 +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)
 +    )
 +  )
 +
 +}
 +</​code>​
 +
 +And for those interested, UserSession looks like this:
 +
 +<code java>
 +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)
 +  }
 +  ​
 +}
 +</​code>​
 +
 +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~~