Posts

Showing posts from 2012

groovy and javafx 2.2: MapChangeListener

javafx does have groovy support through groovyfx (or something like that). But here's another small way to help tame some of the large API base in javafx with some groovy help.

Take listening to a map. Since I store alot of resources in the Node's maps, I need to know whey are changing. Here's a simple class that helps you with detecting changes and firing off some closures:




/**
* Map change listener that takes closures. By default, it does nothing when a change occurs in the map.
* The closures are called with the tuple {@code (map, key, value-added/removed)}.
* @author Mr. Java
*
*/
class SimpleMapChangeListener implements MapChangeListener {

Closure added
Closure removed

public Closure getAdded() {
return added;
}


public Closure getRemoved() {
return removed;
}


public void setAdded(Closure added) {
this.added = added;
}


public void setRemoved(Closure removed) {
this.removed = removed;
}

public SimpleMapChangeListener(Closure added= {}, Closure removed={}) {
this.added…

javafx 2.x

Javafx is looking pretty good. I still have concerns about its innovativeness especially when it comes to programming productivity.

Regardless, some notes for programmers....

When trying to locate resources, it is often necessary to search the node tree, which I think is the logical node tree, for resources. A couple of functions, similar to what you find in WPF are useful.


/**
* Find a resource. Throw an illegal argument exception if its not found.
*/
public static Object findResource(Node start, Object key) {
if (start == null)
return null;
if (start.getProperties().containsKey(key)) {
return start.getProperties().get(key);
}
if (start.getParent() != null)
return findResource(start.getParent(), key);
throw new IllegalArgumentException("Could not find resource with key "
+ key + " starting at node " + start);
}

/**
* Try and find a resource. Do NOT throw an exception if its not found.
*
* @param key
* @param start
* @return
*/
public static Obj…