L
lancout
So I have code that does dynamic layout of components, the hard work
courtesty of JGoodies. Now I want to bind the components to my
business object - domain model - and I feel the stickiness of quick
sand as I step once again into this territory...
My general requirement is simple. I want a control to reflect my
object such that changing either affects the other. Simple to say and
relatively simple to do in trivial code but let me explain where I am
at.
a) My domain objects are a hierarchy of POJO.
e.g.
public class Person {
private NameModel personName = new NameModel();
private AddressModel address = new AddressModel();
...
}
public class NameModel {
private String lastName;
...
}
public class AddressModel {
private String street;
...
}
b) My controls are dynamically instantiated by class name
private Component createControl(IControl control, Object model) {
Class controlClass = Class.forName(control.getClassName());
Component component = (Component) controlClass.newInstance();
...
return component;
}
c) Ideally the model would be bound in some fashion. JGoodies would do
something like
BeanAdapter adapter = new BeanAdapter(model, true);
ValueModel valueModel =
adapter.getValueModel("patientName","getPersonName",
"setPersonName");
Bindings.bind((javax.swing.JTextField) component, valueModel);
Unfortunately Bindings.bind expects a particular control - JTextField,
JTextArea so I have to cast my component and requires that the field
patientName is not a complex Object - so the code only works if my POJO
has primitive fields OR that the setter takes or primitive.
-------------------------
So regardless of all that I believe there is a fundamental design
decision needed here and the very best I can boil it down is using
either
a) Reflection
b) Scripting
Reflection
--------------
So it looks like one option is to use reflection on the getter/setter
to set the values of the model. This does not seem to work well with a
hierarchy of objects - without getting into some very ugly scenarios of
trying to blindly navigate the object tree.
Scripting
------------
I particularly like Rhino to provide excellent scripting capability.
The main drawback is that changing the domain model invalidates the
scripts and while sure you can catch it with unit test scripts - it is
a poor replacement of compile time testing.
What is everyone elses experience with this?
thanks
courtesty of JGoodies. Now I want to bind the components to my
business object - domain model - and I feel the stickiness of quick
sand as I step once again into this territory...
My general requirement is simple. I want a control to reflect my
object such that changing either affects the other. Simple to say and
relatively simple to do in trivial code but let me explain where I am
at.
a) My domain objects are a hierarchy of POJO.
e.g.
public class Person {
private NameModel personName = new NameModel();
private AddressModel address = new AddressModel();
...
}
public class NameModel {
private String lastName;
...
}
public class AddressModel {
private String street;
...
}
b) My controls are dynamically instantiated by class name
private Component createControl(IControl control, Object model) {
Class controlClass = Class.forName(control.getClassName());
Component component = (Component) controlClass.newInstance();
...
return component;
}
c) Ideally the model would be bound in some fashion. JGoodies would do
something like
BeanAdapter adapter = new BeanAdapter(model, true);
ValueModel valueModel =
adapter.getValueModel("patientName","getPersonName",
"setPersonName");
Bindings.bind((javax.swing.JTextField) component, valueModel);
Unfortunately Bindings.bind expects a particular control - JTextField,
JTextArea so I have to cast my component and requires that the field
patientName is not a complex Object - so the code only works if my POJO
has primitive fields OR that the setter takes or primitive.
-------------------------
So regardless of all that I believe there is a fundamental design
decision needed here and the very best I can boil it down is using
either
a) Reflection
b) Scripting
Reflection
--------------
So it looks like one option is to use reflection on the getter/setter
to set the values of the model. This does not seem to work well with a
hierarchy of objects - without getting into some very ugly scenarios of
trying to blindly navigate the object tree.
Scripting
------------
I particularly like Rhino to provide excellent scripting capability.
The main drawback is that changing the domain model invalidates the
scripts and while sure you can catch it with unit test scripts - it is
a poor replacement of compile time testing.
What is everyone elses experience with this?
thanks