Thursday, December 11, 2008

Creating Applets with Scala

Upon the recently release of JavaFX 1.0, I became interested in exploring Scala's Swing cababilities.
I pefer a classical client-server application, as opposed to the ever popular web-based apps using AJAX as a rich content enabler.
So this is right up my alley.



JavaFX Script (the language used to create GUI applictions in JavaFX) seems nicer than many of the XML-base GUI DSLs (Microsoft Silverlight),
but I'm in the mist of learning Scala and this seems like a good problem to solve with Scala's DSL making features.



Fortunatly, someone has already done the heavy lifting and created a small, but useful layer on to top of Swing. It was recently
included in the lastest release of Scala (2.7.2), So here I go.



I stared with a generated maven archetype from the scala-tool.org repository





1 mvn archetype:generate -DarchetypeCatalog=http://scala-tools.org



Then I add the folowing code the the App.scala file generated by the archetype:





1 package org.samples.gui
2
3 import scala.swing._
4 import scala.swing.event._
5
6 class App extends Applet {
7
8 object ui extends UI with Reactor {
9
10 def init() = {
11
12 val button = new Button {
13 text = "Click me"
14 }
15
16 val label = new Label {
17 text = "Welcome to Hello, World Applet"
18 }
19
20 contents = new BoxPanel(Orientation.Vertical) {
21 contents += button
22 contents += label
23 border = Swing.EmptyBorder(30, 30, 10, 30)
24 }
25
26 listenTo(button)
27
28 reactions += {
29 case ButtonClicked(b) => label.text = "Hello, World"
30 }
31 }
32 }
33 }



Next, I added an HTML file to my project (src/main/resources/App.html)





1 <h1>Hello, World Applet</h1>
2
3 <div>
4
5 <applet width = "300" height = "100" code = "org.samples.gui.App.class"
6
7 archive = "/home/erick/projects/samples-scala-gui/target/samples-scala-gui-1.0-SNAPSHOT.jar,
8 /home/erick/.m2/repository/org/scala-lang/scala-library/2.7.2/scala-library-2.7.2.jar,
9 /home/erick/.m2/repository/org/scala-lang/scala-swing/2.7.2/scala-swing-2.7.2.jar">
10
11 <strong>this browser does not understand java.</strong>
12
13 </applet>
14
15 </div>

Thursday, November 20, 2008

Custom constructs in Scala

I ran accross a disscussion in the Scala list today, related to zero-parameter-function-values. One of the answers pointed to an interesting wiki enty about by-name parameters. Which walked through creating a home-made while loop in Scala (very DSL like). I had read about this (or seen it somewhere) but had not experimented with it, until now.




1 def myWhile(condition: => Boolean)(body: => Unit): Unit = {
2
3 if (condition) {
4
5 body;
6 myWhile(cond)(body)
7 }
8 }
9
10 var x = 0;
11
12 myWhile(x < 5) {
13
14 println(x)
15 x += 1
16 }


This is pretty cool, but I'm not sure at this point what I can do with a custom while loop (Scala has a perfectly useful one already). Then, I remembered something about creating constructs similar to the using statement in C#, for calling the dispose method of disposable types.




1 trait Disposable {
2 def dispose();
3 }
4
5 def using(resource: Disposable)(body: => Unit): Unit = {
6 body
7 resource.dispose()
8 }
9
10
11 class MyResource extends Disposable {
12
13 def foo() {
14 println("doing something with my resource")
15 }
16
17 override def dispose() {
18 println("closing resource")
19 }
20 }
21
22 val myResource = new MyResource
23
24 using(myResource) {
25 myResource.foo();
26 }


Now that is pretty cool. There might already be something in scala do to this "using" statement and I'm not quite sure where I can using this now, but it's nice to be able to create statements and constructs for increased readability.

Sunday, October 5, 2008

Drop Down Lists in the Lift framework

Ok, I need to add a select box to a web page. This should be farely easy in any modern web framework, but I'm using Lift, a new framework written in Scala.

Anyway, Lift has a singleton object (a common concept in Scala) with numerous utility functions for generating HTML elements (it's SHtml). This object has the select, selectObj, and select_* methods, which can generate a simple select box.

The first one was easy, it is designed to display name/value pairs of strings. The second one was much more difficult (due to my inexperience with Scala).

As I discovered, scala has a type called Option which allows for a an optional argument or value without having to check for null or deal with null exceptions.

Lift has it's own option like type called Can. As far as I can see, this provides similar functionality and is used throughout the framework.

In this particular instance the selectObj method was expecting a list of objects/values; a default value which is actually a Can holding the value or Empty; and a function to execute when the form is submitted.

After researching (David Pollak and Programming In Scala) I finally discovered how to use the selectObj method and more importantly learned about the Option type in scala and the Can type in Lift.

Adding a dop down list with objects and having that object returned to you is not so easy in most frameworks that I have delt with in the past (Java and .NET). Here is the code that I ended up writing, which is super cool:


1 def displayCourseList():NodeSeq = {
2
3 val c1 = new Course("111", "item 1")
4 val c2 = new Course("222", "item 2")
5
6 val courses = List((c1, c1.name), (c2, c2.name))
7
8 <span>
9 { SHtml.selectObj[Course](courses, Full(course1), course => print(course.number) }
10 </span>
11
12 }

Tuesday, February 26, 2008

Initial Entry

This is my first blog entry in Blogger. I've crossed over into the realm of google data (ouch!!!). changed

I will be disabling my hosted server this week, to save money during difficult times, in hopes to be self contained once again.

Total Pageviews

Labels