> Is there a pattern/way to make the coupling/bounding less restricted,
> or maybe a way to remove the coupling all the way?
You have already mentioned a pattern for data binding
that access properties and copies data back and forth.
The great thing about this approach is that most people
can will understand it quickly and can work with it
and bind new properties. Also, this pattern provides
a means to buffer data that won't be written back to
the domain if the user clicks Cancel.
On the other hand it's quite difficult to synchronize
data for multiple views.
Another approach is to adapt the domain properties,
here Name and Age, to the model interfaces as required
by the views. The brute for method would require an
adapter for every property. Instead you can use a
reusable adapter that converts domain properties into
a generic format and can use a set of generic adapters
that convert this format into something usable as
models for your views. For example you can convert
bound bean properties into a generic model, let's say
ValueModel (that can just get and set a value)
and use a DocumentAdapter, ToggleButtonAdapter, etc.
to implement Document and ToggleButton for use in
Swing components.
You can then bind a bean property to UI components.
But you still need to create the converter instance
from bean to ValueModel, a converter from ValueModel
to view model implementation, and create a view
using the view model. For example:
Person person = new Person();
ValueModel pa = new PropertyAdapter(person, "Name");
DocumentAdapter da = new DocumentAdapter(pa);
JTextComponent tc = new JTextField();
tc.setModel(da);
Using a binding and component factory you can
abbreviate the above to:
Person person = new Person();
JTextComponent tc = Factory.createBoundField(person, "Name");
The advantage of such a binding is the ability
to perfom complex operations on the value model layer,
for example buffering, caching, multiplexing, etc.
On the other hand, it's more difficult to understand.
If you are short of time, I recommend to use a
simple pattern. If you aim to build many resuable
models and want to present them with multiple views
you may consider using a more complex and powerful pattern.
I'm satisfied with a binding that decouples models
from the views. So I can define almost all event handling
as operations on the model layer. The views just listen
to model changes and present them. I don't need to care
how many views present my data.
Hope this helps,
Karsten Lentzsch
Joop - 02 Feb 2004 20:06 GMT
>> Is there a pattern/way to make the coupling/bounding less restricted,
>> or maybe a way to remove the coupling all the way?
[quoted text clipped - 56 lines]
>to model changes and present them. I don't need to care
>how many views present my data.
Hello Karsten,
That's sounds like a great structure to me, but i was kind of baffled
by the new terms you mention so i did research. I now know what a
Adapter and a ValueModel pattern is and i also know the Document is
the Model of a JTextComponent (or JTextField). I also get the idea
that a DocumentAdapter converts ValueModel to Document interface to
make it usefull for JTextComponent.
It's seems logic for me, but i'm already stuck when implementing the
constructor PropertyAdapter. You specified:
ValueModel pa = new PropertyAdapter(person, "Name");
I know that person is my Person Object and Name is my attribute which
i make public with the ValueModel. Could you give me a short version
of this constructor, maybe some pseudo code?
BTW, you're doing a great job with the new form layoutmanager.
Thanks for the help,
joop
Karsten Lentzsch - 02 Feb 2004 21:29 GMT
> I know that person is my Person Object and Name is my attribute which
> i make public with the ValueModel. Could you give me a short version
> of this constructor, maybe some pseudo code?
Hi, Thanks for your compliments.
I hesitate to post even pseudo code for the PropertyAdapter
because it is a quite complex class, one of the largest
in my binding framework. I plan to open source the binding,
currently it ships only with my Swing Suite product.
Anyway, in the constructor, 'Name' is the name of a bound
Bean property as described by the Java Bean specification.
Often these properties have a getter and setter, follow
a naming convention and are implemented using a field.
However, the Bean specification describes a much more
flexible approach and how to determine the read-write state
and how to get and set a property value.
Therefore I use the standard Bean Introspector to look up
a PropertyDescriptor for a given property name. This way
I honor BeanInfo classes, the class hierarchy, non-standard
getter and setter names, etc.
AFAIK the Apache Commons library provides a class that does
something similar: convert bean properties to something else.
See the Bean utils or so.
I'm in the process of building a demo for my Binding
framework and will publish it in the JGoodies freeware
section. Hopefully the demo will clarify the things mentioned
here and in my previous mail.
Swing Suite customers listening to this thread my browse
the JavaDoc class comment of the PropertyAdapter class
for example adapter chains from a Bean property to a
then bound JTextComponent.
Best regards,
Karsten
Java Bean Specification:
http://java.sun.com/products/javabeans/docs/spec.html