• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

MVC modify model from outside of the table.

 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have UI table. The data in table model may change depending on user actions outside the scope of the table (data displayed depends on what is selected in other tables). How to properly notify table of this data change and update the model (I am working with JTable):

a) Table model observes data object, updates itself and fires table change event.
b) Somebody from outside changes model and calls fireTableChanged on model.
c) Somebody from outside calls controller method, which call method on model to update itself. Model updates itself and fires change event.

or any other combination...

Any suggestions? I think all these methods would work, but which one is "the best" ? This problem is WAY to long in my mind.

Regards!
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After some thinking "a" seems more convenient since I don't have to code "Something" that will have to do model updates...
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, "a" sounds best.

You might also consider using an Adapter instead of copying the table values.
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought alot about using adapter, but the TableModel must provide the same data I would like to get from adapter. So my particular TableModel implementation is adapter itself since it takes data object and provides TableModel interface for that object.

Still I had to make small outside class that implements Observer. It watches for data object and calls fireTableDataChanged on model. I did this because my data object is not Observable rather it contains Observable object When I did it I though "extends would be evil" so I put Observable inside the data object. That made a problem where model class would observe one object but take data from another object... And it couldn't know which of several objects changed because objects were not observables but only contained observables... I would have to create model passing it data object AND observables which is ugly at least. Or I would have to pass original object as and argument to notifyObservers, but it doesn't seem right for Observable to tailor it's notifications to particular observers when it does not even know what those observers do.

What lesson did I learn? Data objects better extend Observable directly, not have reference to one (not be composed of Observables). This is because there is no interface for Observable in JDK.

Best regards,
Vladas
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If data object already "extends" something then it may be useful to make object that extends Observable and contains original data object. But I did it upside down Which is bad..
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry I meant "class extends"
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, Observable is an abstract class, not an interface. I guess because it has a fair bit of functionality and helper methods built in.

When I started Java it didn't occur to me to look into Observable because I that it was just for Swing, so I made my own little pub-sub widget. The GoF on Observer mentions a "change manager" and I guess that's what I made. I saw it more as a subscription manager, tho, so the subject object would not have to know maintain a list of its observers. I also used the "push" variation, where the change event notification can include the changed data as arguments.

This Messaging page has a few diagrams for observer & pub-sub framework alternatives.

So, your data object could be a Publisher even without being Observable. I'd be happy to share my little Publisher - 123 lines - if you like.

If you don't go that way, could your embedded Observable objects have a reference to their parent to make it easier for observers to figure out what changed?
 
Vladas Razas
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thinking about Change Manager. I programmed data view that receives data piped from multiple layers. In my case it's table view.

Table View <- Data Transformer <- Data counter <- Data Filter <- Real data.

If I don't make every object in this chain observable, table view doesn't know what changed, because change starts in Data Filter
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic