« November 2007 | Main | January 2008 »

December 2007

December 30, 2007

Happy New Year - Formatting in Compiled JavaFX Script

I just want to wish you a Happy New Year, and to show you an example of date formatting in compiled JavaFX Script.  First, here are the screenshots of a few invocations of the program, each one taken after having changed to a different locale on my computer.  Of course, your computer is probably already set to your desired locale, so when you run this program, you should see the word "Happy", followed by the day of the week in your language, followed by the date in your language (of the first day of 2008).

Happynewyearenglishus

Happynewyearchinese

Happynewyearspanish

Happynewyeardutch

Here's the program, followed by a brief explanation of its formatting aspects:

/*
*  HappyNewYearFormatting.fx - Example of using formatting
*                              in compiled JavaFX Script
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/
import javafx.ui.*;
import javafx.ui.canvas.*;
import java.lang.System;
import java.text.DateFormat;
import java.util.GregorianCalendar;
 
Frame {
  title: "Happy New Year!"
  width: 500
  height: 100
  background: Color.WHITE
  visible: true
  content:
    Canvas {
      var cal = new GregorianCalendar(2008, 0, 1)
      var df = DateFormat.getDateInstance(DateFormat.LONG)
      content:
        Text {
          font:
            Font {
              face: FontFace.SANSSERIF
              style: FontStyle.PLAIN
              size: 24
            }
          stroke: Color.RED
          fill: Color.RED
          x: 15
          y: 25
          content: bind "Happy {%tA cal}, {df.format(cal.getTime())}"
        }
    }
}

Formatting in Compiled JavaFX Script

You can control how numbers and dates are converted to character strings by providing an additional formatting prefix in a String expression. This prefix follows the specification of the Java Formatter class.  In the example above, I'm using the %tA formatting prefix to convert the date to the day of the week in the default locale.  I'm also using the capabilities of the Java DateFormat class to show the month, day and year in the language and order of the default locale.

Please have a happy, healthy, and prosperous 2008,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

December 28, 2007

No Need for Setters and Constructors in Compiled JavaFX Script

To support its simple, declarative programming model, compiled JavaFX Script has some constructs to eliminate the need for setter and constructor methods.  Please take a look at the following example and its console output, and I'll follow up with an explanation of two of these constructs.

/*
*  TriggersAndPostinit.fx - Example of using a replace trigger
*                           to eliminate the need for a setter,
*                           and a postinit block for some
*                           constructor-related functionality
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/

import java.lang.System;

class Vehicle {
  postinit {
    if (numWheels == 18 and sporty) {
      System.out.println("Veto sporty for 18 wheeler!");
      sporty = false;
    }
  }
  attribute model:String;
  attribute numWheels:Integer = 4
    on replace (oldValue) {
      if (numWheels <= 0) {
        System.out.println("{numWheels} wheels not valid, changing to {oldValue}!");
        numWheels = oldValue;
      }
    }
  attribute sporty:Boolean;
 
  public function toString():String {
    return "Model: {model}, Number of wheels: {numWheels}, Sporty: {sporty}"
  }
}

var corvette =
  Vehicle {
    model: "Corvette"
    numWheels: 4
    sporty: true
  };

System.out.println("{corvette}
");

corvette.numWheels = 0;

System.out.println("{corvette}
");

var mackTruck =
  Vehicle {
    model: "Mack truck"
    numWheels: 18
    sporty: true
  };

System.out.println("{mackTruck}
");

Please see previous posts like this one for how to build and run compiled JavaFX Script programs.  Here's the console output when running this program:

Model: Corvette, Number of wheels: 4, Sporty: true

0 wheels not valid, changing to 4!
Model: Corvette, Number of wheels: 4, Sporty: true

Veto sporty for 18 wheeler!
Model: Mack truck, Number of wheels: 18, Sporty: false

Eliminating Setter (Mutator) Methods

In this example, a class named Vehicle is defined, from which we're going to create instances.  One very important role of a setter method is to protect the integrity of the object.  In this example, we don't want the number of wheels on a vehicle to be set to 0 or less.  Traditionally, a setNumWheels() method would be created in which an invalid value would be rejected.  To support the declarative model, compiled JavaFX Script has an on replace trigger that gets invoked whenever the value of an attribute is replaced.  In the example above, note that the on replace block rejects the new value and sets the attribute value back to the former value.  Note that oldValue is an arbitrarily named variable that is local to the on replace block.

Providing Constructor-Related Functionality

After after a new object has been created, and all of its attributes have been assigned their default or explicitly set values, the postinit block (if present) is executed.  This gives you the opportunity to perform functionality, including protecting the integrity of the object, when all of the attribute values are known.  In the example above, an 18-wheeler truck can't be sporty as well (although I've seen some pretty sporty semi-trucks on the road).

Please also note the toString() function in this example.  When you use an object reference in a String, the toString() function is automatically called.  Here, I've overridden the default toString() function to make it easy to print its contents in the desired format.

Oh, by the way, I'm hearing more voices.  Check out this post on Maxa Blog.

As always, if you have any questions, please post a comment.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

December 26, 2007

Slides from JavaPolis 2007 JavaFX "Conference Presentation"

In a previous post I gave you a link to the slides from my "JavaFX in Action University Session" at JavaPolis 2007.  Now the slides from my "JavaFX" conference presentation are available in PDF.  This was a shorter, one-hour presentation, that was essentially a subset of the three-hour "University Session".Javapolis_javafx_header_4
   

Shortly after this session I was interviewed by Aaron Huston (head of the Java Champions and Java Users Group programs at Sun). I'll notify you when the streaming videos for these sessions become available.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

December 25, 2007

Spotting JavaFX Apps in the Wild - Santa's Super Status System

There's been a Santa sighting!  Check out this JavaFX Script application over at Joshua Marinacci's blog.

Santassuperstatussystem

By the way, I received a nice Christmas present yesterday.  My JavaFX Script book has been available as an eBook download since early October, but the printed book had been delayed.  When I got home last night on Christmas Eve, I almost tripped over the box on the porch that contained the printed book.  I went online and found that Amazon.com and Apress has them in stock and is currently shipping them (finally!)

Merry Christmas,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
Immediate eBook (PDF) download available at the book's Apress site

December 23, 2007

Happy Holidays - Compiled JavaFX Script Applet

To wish you Happy Holidays (and if you'll permit me, a very merry Christmas) I wrote this Compiled JavaFX Script Applet (that executes in a browser). 

Happyholidays

For fast deployment I used the latest version of Java SE 6 Update 10 (formerly known as Java SE Update N, formerly known as Consumer JRE, formerly known as Prince :-D).  I also used the latest beta version of Firefox (Minefield 3.0 beta), as this is necessary if you want to use the Java SE Update 10 browser plug-in with Firefox.  Check out this video blog by Robert Eckstein for some great information about Java SE Update 10.

The JavaFX Script compiler team is working as we speak on a simple, elegant, way to create JavaFX Applets.  The technique that I used is a short term approach that should not be used as a model for creating JavaFX Script applets beyond just playing around. 

Update March 16, 2008: There are two ways to create JavaFX Script applets, described in detail in the following posts:

Happy Holidays,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site

December 21, 2007

Slides from JavaPolis 2007 JavaFX Script in Action "University Session"

Presenting at JavaPolis 2007 in Antwerp, Belgium was a wonderful experience.  I really appreciated the friendly atmosphere, the top-notch organization, and the fact that the sessions were in very comfortable movie theaters! 

Javafxpresentationpic001

The slides for my "JavaFX in Action University Session" are available in PDF now.  The video of the session will be available in the near future, and I'll notify you when that occurs.

Javapolis2007_pres_slide1 Javapolis2007_pres_slide2

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site

December 19, 2007

A Compiled JavaFX Script Example with GridPanel and Various Borders - And Leopard Java 6 Preview

Today I'd like to show you an example from my JavaFX Script book that I've ported to compiled JavaFX Script.  It demonstrates the borders available in JavaFX Script, as well as how to use the GridPanel layout widget.  Here's a screenshot of the program's UI:

Bordersexamplecompiled

See a previous post for instructions on obtaining the compiler, keeping in mind that you may want to download the JavaFX Script compiler as well so that you can look at the source code.  This is especially useful for finding constants, for example EtchType.LOWERED in today's example:

/*
*  BordersExample.fx - An example of the borders available in compiled JavaFX Script
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc.com)
*/
package jfx_book;

import javafx.ui.*;
import javafx.ui.canvas.*;

Frame {
  title: "JavaFX Borders"
  width: 500
  height: 500
  visible: true
  content:
    GridPanel {
      rows: 3
      columns: 3
      vgap: 5
      hgap: 5
      cells: [
        Button {
          text: "BevelBorder"
          border:
            BevelBorder {
            }
        },
        Button {
          text: "EmptyBorder"
          border:
            EmptyBorder {
              top: 20
              left: 20
              bottom: 20
              right: 20
            }
        },
        Button {
          text: "EtchedBorder"
          border:
            EtchedBorder {
              style: EtchType.LOWERED
            }
        },
        Button {
          text: "LineBorder"
          border:
            LineBorder {
              thickness: 4
              lineColor: Color.PURPLE
              roundedCorners: true
            }
        },
        Button {
          text: "MatteBorder"
          border:
            MatteBorder {
              matteColor: Color.CORNFLOWERBLUE
              top: 10
              left: 10
              bottom: 10
              right: 10
            }
        },
        Button {
          text: "ShadowedBorder"
          border:
            ShadowedBorder {
            }
        },
        Button {
          text: "SoftBevelBorder"
          border:
            SoftBevelBorder {
              style: BevelType.LOWERED
            }
        },
        Button {
          text: "TitledBorder"
          border:
            TitledBorder {
              title: "Title"
              titlePosition: TitledBorderPosition.BOTTOM
              titleJustification: TitledBorderJustification.CENTER
              titleColor: Color.DARKMAGENTA
            }
        },
        Button {
          text: "CompoundBorder"
          border:
            CompoundBorder {
              borders: [
                MatteBorder {
                  matteColor: Color.DARKGREEN
                  top: 5
                  left: 5
                  bottom: 5
                  right: 5
                },
                TitledBorder {
                  title: "Title"
                  titleColor: Color.INDIGO
                }
              ]
            }
        },
      ]
    }
}

Please take this example for a spin, and use it to help you write and submit a compiled JavaFX Script program as instructed in a previous post.

By the way, Apple Java 6 Preview for Leopard is out: http://developer.apple.com/java/

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site

December 18, 2007

Creating a Compiled JavaFX Script Program with Multiple FX Source Files

Earlier in this weblog, I showed you an example from my JavaFX Script book that contains multiple FX source files.  Yesterday the JavaFX Script compiler team added the capabilities required to port that example to compiled JavaFX Script.  Please take some time to look at the original example and its explanation at the link provided above.  Then take a look at this ported example, followed by an explanation of how to compile and run it:

HelloJFXModel.fx

/*
*  HelloJFXModel.fx - The model behind a JavaFX Script "Hello World" style
*                     example of binding.
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*/
package jfx_book;

/**
* This class serves as the model behind the user interface
*/
class HelloJFXModel {
  attribute greeting:String;
}

HelloJFXBind2.fx

/*
*  HelloJFXBind2.fx - A Compiled JavaFX Script "Hello World" style example
*                     binding to a model that is located in its own file.
*
*  Developed 2007 by James L. Weaver (jim.weaver at lat-inc dot com)
*/
package jfx_book;

import javafx.ui.*;
import javafx.ui.canvas.*;

/**
* This is a JavaFX Script that binds to data from the model.
*/
Frame {
  var hellojfxModel =
    HelloJFXModel {
      greeting: "Howdy JavaFX Script Developer!"
    }
  title: "JavaFX Script example that binds to a model"
  height: 100
  width: 400
  background: Color.WHITE
  content:
    Canvas {
      content: [
        Text {
          font:
            Font {
              faceName: "Sans Serif"
              // Example of an attribute with a collection of values
              style: [
                FontStyle.BOLD,
                FontStyle.ITALIC
              ]
              size: 24
            }
          // Put some color into the app
          stroke: Color.RED
          fill: Color.RED
          x: 10
          y: 10
          content: bind  hellojfxModel.greeting
        }
      ]
    }
  visible: true
}

Because these source files are in the jfx_book package, place these files in that directory.  Then navigate to that directory and run the following command:

javafxc HelloJFXModel.fx HelloJFXBind2.fx

Alternatively, you could  use a wildcard:

javafxc *.fx 

After successfully compiling the source files, navigate to the directory in which the top node of the package is located and run the javafx command as usual to execute the program:

javafx jfx_book.HelloJFXBind2

The output of the program should look similar to the interpreted version:

Hellojfxbind2compiled

By the way, this and upcoming posts are going to be code-intensive so that you'll feel comfortable writing your own compiled JavaFX Script program in response to the challenge that I gave you yesterday.

Enjoy,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site

December 17, 2007

Now It's Your Turn to Write a Compiled JavaFX Script Program

In previous posts, I've shown you how to write compiled JavaFX Script programs (for example, The Elephant is Through the Door).  I've also shown you how to obtain the JavaFX Script compiler in order to compile and run your program.  Now I'd like you to have some fun by thinking of an idea for a compiled JavaFX Script program, developing it, and sending it to me so that I can post it for everyone to see.  Here are some guidelines:

  • Keep in mind that compiled JavaFX Script is still in development, so you may want to keep your program fairly simple.
  • Comment your code well so that when I post the listing, the reader will understand what the code is doing.
  • Your program must successfully compile before submitting it.
  • In your email, if you'd like your program and source code posted, you'll need to give me permission to do so.
  • Of course, please use good judgment regarding the program's content.  I won't post anything that is objectionable (in poor taste, crude, etc.)
  • If you have questions while you're developing your program, please post a comment.

Please submit your compiled JavaFX Script program's source to jim.weaver at lat-inc.com, and I'll take care of making it available via Java Web Start and posting the source code.

Good luck,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site

December 16, 2007

JavaFX Mobile Addressed in JavaPolis 2007 Keynotes

It's time for this weblog to broach the JavaFX Mobile subject, and this is the perfect opportunity, because it was part of the JavaPolis 2007 keynote sessions on Wednesday and Thursday.  James Gosling addressed JavaFX Mobile in his keynote entitled The State of the Java Universe, and Tim Cramer (Sun's Executive Director of Consumer Solutions) gave a keynote address entitled Java in the Client, in which he spent some of the time talking about JavaFX Mobile.  Here are some of the JavaFX Mobile-related points that they discussed, with the help of Sun Java "Evangelist" Angela Caicedo.

JavaFX Mobile (when available) will support Java Micro Edition (ME) applications.  To illustrate this, I'd like to refer to The Big Picture slide from the Sun Developer Network JavaFX site:

Ig_javafx_architecture

The bottom layer (white wedge) of these stacks are the underlying Java capabilities on the device.  In the case of more powerful mobile devices, as shown above in the column labeled JavaFX Mobile, CDC (Connected Device Configuration) is a Java ME term that specifies the underlying Java framework and capability.  On top of that in the orange JavaFX Framework layer is a set of APIs that provide various services, in this case the AGUI (Advanced Graphics and User Interface) platform, also known as JSR-209.

An API that James Gosling and Tim Cramer both mentioned was the MSA (Mobile Services Architecture) platform, also known as JSR-248.  This runs on the CLDC (Connected Limited Device Configuration) and is meant for less capable phones than ones that have the CDC framework.

The top layer represents the interactive content of an application, written in JavaFX Script, running on the device, and using the services offered by the underlying frameworks.  An unknown at this point is when JavaFX Script (compiled) will be ready to run on each of these configurations (CLDC and CDC).

My company (LAT) is involved in a mobile phone application development project, and will be targeting the CDC.  Our strategy will be to use the UI tools and technologies available today to develop prototypes on the phones, and to simultaneously develop compiled JavaFX Script prototypes on desktop machines.  When JavaFX Script is available for the CDC, then we'll replace the UI code with the JavaFX Script code that we'll have developed by that time.  This brings me to the next point: Java ME development tools available today.

Cool Java ME Development Tools are Available Now

The newly-released NetBeans 6.0 Mobility version available as an option on the NetBeans IDE 6.0 Download page has some very nice mobile application development tools, including the Visual Mobile Designer and the Mobile Game Builder.  Angela demonstrated both of them, but spent more time on the Mobile Game Builder because the Java Evangelist job requires playing with the coolest stuff and having fun.

So, the subject has been broached.  I'll bring you more on JavaFX Mobile as it becomes available.

Regards,
Jim Weaver
JavaFX Script: Dynamic Java Scripting for Rich Internet/Client-side Applications
eBook (PDF) download immediately available at the book's Apress site