Quick TreeModels with OGB II - No Fluff Just Stuff

Quick TreeModels with OGB II

Posted by: Andres Almiray on February 6, 2008

The previous tip shows how easy is to setup ObjectGraphBuilder to build a hierarchical structure of beans even when said beans do not adhere to the JavaBeans conventions, but it wasn't quite Groovy enough. The problem is that it is quite obvious that I want node names to be both the name and data so wrapping them with a node() node is a bit redundant, wouldn't it be great if the node name be treated as the default userObject value of the TreeNode? it turns out you can do it, by registering another custom resolver.

This case requires you to register a NewInstanceResolver, which will take care of instantiating the nodes, we will use a handy variable exposed by the builder, currentName, to set the node name as the userObject, like this

Ahh much better.

Keep on Groovying!

Update: removed extra single quotes on the node names, they are only needed if the node name contains non-identifier chars.
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »