Addictive Software ramblings of a chaotic mind

26Nov/091

JavaFX data binding to Java

Data binding in JavaFX is pretty straightforward.
You should think binding to Java Objects should be just as simple, well it is a little complicater then just doing

var someObject : bind myJavaclass.getProperty();

JavaFX needs to be aware of any changes to the bound variables to be able to update the related objects. It does this automagically for all javafx objects.
It seems JavaFX uses the Observer/Observable pattern for all its internal objects, well we can do the same to bind with JavaObjects.
To get myself familiar with JavaFX i decided to write a photoviewer/editor. This application has a java backend with in particular a Photo class which contains all the information of a certain picture, filename, path, tags, exif information etcetera

To get changes in the Java class to appear in the gui I create a PhotoAdapter javafx class that extends Observer and implemented the properties I need in my GUI.

public class PhotoAdapter extends Observer {
    public-read var filename: String;
    public-read var tags: String[];
    public-read var exif: String[];
 
    public-init var photo: Photo
         on replace {
            photo.addObserver(this);
            filename=photo.getFilename();
            tags = photo.getTags();
            exif = photo.getExif();
         };
 
    override function update(observable: Observable, arg: Object) {
        filename = photo.getFilename();
        tags = photo.getTags();
        exif = photo.getExif();
    }

after that I had my existing Java Photo class extend Observable and in every method that made changes I added the methods setChanged() and NotifyObservers() to inform the Observers about the change

public class Photo extends Observable {
    private Map<String, String> exifInformation; 
    ....
    public void setExifInformation(Map exifInfo) {
        this.exifInformation = exifInfo;
        setChanged();
        notifyObservers();
    }
}

in my main javafx class I can then use it as follows

    var photoAdapter = PhotoAdapter {
        photo : new Photo(startImage);
}

and use to fill a ListView swing object with for instance the tags:

    ListView {
        items:     bind photoAdapter.tags;
        width:     200;
        height:    300;
    }

et viola, thats all that there is to it

11Nov/090

.Net’s using() for Java

As I'm working as a Java developer in a microsoft oriented company this means I sometime have to do .Net programming.
As there are some things I don't like about .Net (no checked exceptions is one) there is one thing I find very good and useful.

In .Net an object that implements the IDisposable interface can be initiated with the Using command like this:

using (TextWriter w = File.CreateText("log.txt"))
    { w.WriteLine("This is line one");
}

After the block has executed, using will take care of closing the writer and freeing the resources.
This gives me solid readable code without memoryleaks.

Now looking at Java, closing resources usually involves putting a close() in a finally block, with another try-catch construction to catch exceptions that the close() can throw.
this is ugly, errorprone and can obfuscate the real exception that happened.

Java 7 comes to the rescue, one of the approved proposals for java 7 is "automatic resource management"
the following code block will demonstrate this

static void copy(String src, String dest) throws IOException {
   try (InputStream in = new FileInputStream(src); 
         OutputStream out = new FileOutputStream(dest)) {
 
        byte[] buf = new byte[8192];
        int n;
        while ((n = in.read(buf)) >= 0)
        out.write(buf, 0, n);
    }
    //optional catch block
    //optional finally block
}

In Java 6 or lower the above code would contain 2 try-finally blocks to handle closing the input and ouput streams, another 2 try-catch blocks to handle exceptions from the close() commands, and more catches for exceptions you would want to handle specifically

Off all the features in java 7 this is the one I'm looking forward the most, the forkjoin and invokeDynamic changes following close though.
here is a list of all the features in java 7
http://openjdk.java.net/projects/jdk7/features/

Tagged as: , No Comments