SwingBuilder's binding revisited - No Fluff Just Stuff

SwingBuilder's binding revisited

Posted by: Andres Almiray on August 20, 2008

Looks like the previous SwingBuilder binding entry stroke a chord on Greg Bollinger. Greg writes at dzone links:
That's pretty cool but horribly impractical. What I'd like to see is how binding would work to a true domain object vs some "example only" worthy ObservableMap that no one really uses to store form data in.
What everyone needs is the ability to easily bind forms to POJO's and back. Just like all the wonderful web frameworks can do. Let's see those kinds of examples if you really want to impress.

Horribly impractical... true, nobody in their sound mind should use an observable-map-as-POGO-replacement instead of regular domain objects when it comes to production code, for prototyping and testing I would think otherwise. An example with true domain objects you say? you got it! As I said ObservableMap works like an observable POGO, it simplifies testing as you do not need to declare a new class for your observable POGO, you just use the behavior, after all Groovy has blurred the line between Maps and POGOs. So what does it take to make a POGO an observable one? following the JavaBean conventions it means
  • Each observable property must fire PropertyChangeEvent when its value changes.
  • The bean should expose register/unregister methods for PropertyChangeListener.
  • The observable behavior (firing events) may be implemented with PropertyChangeSupport.
Simple rules really, but they can turn your domain object into a jungle of code. Groovy already provides simplified POGOs, just declare a field and its type without an access modifier and you got yourself a property. Groovy 1.6-beta-1+ goes one step further with @Bindable and ASTTransformations. As @shemnon shows here, it is quite easy to build an observable POGO, so lets follow his code samples

That's it. Import the annotation, annotate the pertinent properties, bind to your hearts content. But wait there is more, it turns out there is a way to streamline the binding form textField.text to textModel.text. SwingBuilder includes a factory for building any node/bean you'd like, aptly named bean. If we use that factory and set textModel as it value then we can also use the short binding syntax

Much better. You may be wondering, what happened to the trigger code (PropertyChangeListener)? well that remains the same, it doesn't matter if you have an ObservableMap instance of an observable POGO, there is no short syntax for triggers for the time being.

Keep on Groovying!
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 »