jdk 1.5 annotations (2)
Why? I develop a pet project which is a lot about editing an object model. With JDK 1.4 I was forced to have an editor panel class for each data class I needed to edit. Each panel can be reused between "Create..." and "Edit..." dialogs, but still I had to spend significant time to:
a) initially develop my data editor panels
b) keep editor panels in sync with the data model as the later evolves
Since I don't have much time for my pet project - I have to work for a living like almost everyone else - I was very keen to find a solution to help me speed up my developments and simplify maintenance. This is when I had the idea: I could use annotations to decorate my data classes with hints on how to build a user interface and have an automated tool that would use reflection to find these decoration at runtime and construct the editor panel accordingly. And since we're at it, the UI could also use reflection to apply editor values to my data objects and the other way around.
The logic behind it is quite simple: to construct a panel I was always repeating the same code sequence:
for each attribute, I
a) instantiate an editor element (JTextField, JCheckBox, etc)
b) set GridBagConstraints for editor element
c) instantiate a JLabel
d) set GridBagConstraints for the label
e) eventually add a separator
All this can very easily be automated, so I quickly coded a prototype and it worked. The advantage is
obvious: no more out of sync between data classes and editor classes:
- if you add an attribute to your data class, you also immediately decorate it with hints for grid bag layout
- if you remove an attribute, you also have remove its decorations
- if you rename an attribute or even a super class, you don't even need to touch your UI
Having everything in one file simplifies things quite a lot.
The associated code was a fragment of my development tree, but I decided to detach it release it as open source, under the Apache V2 license.
Current functionality is limited to automated UI construction for classes containing canonic attributes, but I plan to add automated data integrity checks and support for complex attributes, and many others.
There is a quick and dirty demo in the com.thekirschners.uitoys.autoui.samples.demo package.
Of course, this is just an initial post. I'll continue to release more code and documentation as the project moves along. One important thing to point out is that this project is mainly driven by my pet project's needs (and will continue to be so). However, if I receive feature requests which are of general interest, I'll do my best to include it in the code base. Like in any open source initiative, any contribution to the project, in any form - feature requests, bug reports, feedback, code contributions and/or fixes - is highly welcomed. Please don't hesitate to post your impressions on this blog.
download the source package at: