This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC modify model from outside of the table. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC modify model from outside of the table." Watch "MVC modify model from outside of the table." New topic
Author

MVC modify model from outside of the table.

Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
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

Joined: Dec 02, 2003
Posts: 385
After some thinking "a" seems more convenient since I don't have to code "Something" that will have to do model updates...
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Yes, "a" sounds best.

You might also consider using an Adapter instead of copying the table values.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
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

Joined: Dec 02, 2003
Posts: 385
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

Joined: Dec 02, 2003
Posts: 385
sorry I meant "class extends"
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC modify model from outside of the table.