Announcement Announcement Module
Collapse
No announcement yet.
Question on data binding Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Question on data binding

    Hi,

    I have a question in regards to data binding. It seems that when there is a FormModel for the domain object, and the FormModel is used in some UI component (say a JTextField), any edits in the JTextField changes the domain object. However, if changes were made on the domain object (say, there was an update for the domain object from the server, and the domain's data was updated), the change does not propragate up to the UI Component. Am I missing a linkage between the domain object and the FormModel, or is this not supported?

    In my prototyping with data binding, I also noticed that for a given domain object, if I have 2 instances of a FormModel for it (say I have 2 views, with each creating a form model for the same domain object), when I change the value in one view (via a UI component), the other view does not pick up that change. If I use the same FormModel for both views, then it works fine. So, for a given domain model, it can only have one FormModel that all UI components need to use in order for the data to be updated?

  • #2
    You have to change domain object with ValueModel#setValue, not direct

    for instance,

    swingFormModel.getValueModel("property").setValue( newValue);

    regards

    Comment


    • #3
      Your domain object needs to fire property change events so that the form model can be notified of external changes, unfortunatly there's no magic way for the form model to just know that the domain object has been modified.

      Code:
      class DomainObject implements PropertyChangePublisher {
      ...
          private PropertyChangeSupport pcs = new PropertyChangeSupport (this);
      ...
      
          public void setSomeProperty(String someProperty ) {
               String oldValue = this.someProperty;
               this.someProperty = someProperty;
               pcs.firePropertyChange("someProperty", oldValue, someProperty);
          }
      
      ...
      
      }
      I'll leave the rest of the implementation to you If your know AOP resonable well or are feeling adventurous you could also implement a PropertyChangePublisher mixin which in the long run would save you lots of code and effort.

      Ollie

      Comment


      • #4
        Is there a danger of "forever loop" or at least redundant operations? The user enters value into control, it changes model, model changes domain object, change of domain object trigers change in model.

        Comment


        • #5
          Why don't you try it out?

          Comment


          • #6
            I got it to work. Thanks for your inputs snpe and Ollie!

            Comment


            • #7
              Just to clarify, if our domain objects fire property change events then Spring-rich's FormModel will automatically pick up on model property changes and handle them accordingly? I too have an architecture where the server can push an update (asynchronousl) to the client, at which point the client will "set" the appropriate properties on the domain objects - the client's UI has to update immediately with the change.

              Thanks,

              Andy

              Comment


              • #8
                How do you guys notify combobox which was created using "createBoundComboBox(ValueModel, ValueModel, String..." that it needes to be refreshed? Is there an easy way?

                Comment


                • #9
                  Jurijus,

                  if you need to update the selected item just put a new value into selectedItemHolder or if you need to update the list of selectable items update selectableItemsHolder with the new list.

                  Most of the time the selectedItemHolder value model will be bound to a bean property so if the bean published property change notifications all you need to do is update the property directly.

                  Ollie

                  Comment


                  • #10
                    Did anybody try to update selectableItemsHolder for combobox to which ComboBoxAutoCompleteion was applied? Looks like Map, which is created in fillItem2StringMap, needs to be updated.

                    Comment


                    • #11
                      Originally posted by oliverhutchison
                      Your domain object needs to fire property change events so that the form model can be notified of external changes, unfortunatly there's no magic way for the form model to just know that the domain object has been modified.

                      Code:
                      class DomainObject implements PropertyChangePublisher {
                      ...
                          private PropertyChangeSupport pcs = new PropertyChangeSupport (this);
                      ...
                      
                          public void setSomeProperty(String someProperty ) {
                               String oldValue = this.someProperty;
                               this.someProperty = someProperty;
                               pcs.firePropertyChange("someProperty", oldValue, someProperty);
                          }
                      
                      ...
                      
                      }
                      I'll leave the rest of the implementation to you If your know AOP resonable well or are feeling adventurous you could also implement a PropertyChangePublisher mixin which in the long run would save you lots of code and effort.

                      Ollie
                      I noticed a lot of the binding code in Spring merely looks for the prescence of the PropertyChangePublisher interface on the domain object. Perhaps instead of tying the domain objects to the Spring framework in such a way, a better approach could be to use reflection to check for the presence of methods with the signatures...

                      Code:
                      addPropertyChangeListener(PropertyChangeListener)
                      removePropertyChangeListener(PropertyChangeListener)
                      ... for global notification, and ...

                      Code:
                      addPropertyChangeListener(String, PropertyChangeListener)
                      removePropertyChangeListener(String, PropertyChangeListener)
                      ... for individual property notification.

                      I believe this is the approach the JGoodies binding framwork takes, and it looks quite simple to achieve. It would probably be a matter of externalizing all such code to a more convenient utilities class.

                      -Scott

                      Comment

                      Working...
                      X