This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I was hoping someone could suggest a few possible solutions to my problem.
I have an object of class A which contains a few properties and a list of objects of type B. The type B objects contain a few properties and a list of object of type C which in turn contain their own properties.
I'm writing a part of a GUI which has to allow users to change this A object (which will involve changing the B and C objects). I'm struggling to find an elegant way of coding this. The GUI is fairly complicated but has to be to allow the users enough flexibility in editting these objects. E.g. The main screen displays and allows the editting of some of the properties of A along with a table/list of the B objects. When the user selects one or more B objects in the table part of the GUI displays these objects so the user can make changes to the B and possible C parts. As these various properties are changed other parts of the GUI have to be updated to reflect the changes.
I started out making A extend Observable so if one part of the GUI makes changes to it the other parts know to redisplay the new data if appropriate. The problem I am facing is what to do with the B and C parts. E.g. if the user changes a property of C and this change has to be reflected elsewhere in the GUI how do I propagate this change to the various GUI parts.
One option is to make B and C also Observable but I see this becoming a bit of a nightmare to maintain as B's and C's are added a removed, selected and deselected, etc. having to add and remove Observers to listen for changes to these.
Another solution would be to have the A object listen to changes in the B object and the B objects listen to changes in the C objects then B nofity A of the change and A notify it observers of this change?
Or an alternative approach would be to hard code these 'events' in the GUI. E.g. when something happens in one part of the GUI have something like parent.getTablePanel().refreshCurrentRow() to have this change redrawn in another component, etc.
I don't like any of these solutions as they all seem messy. Is there any set way or best practice / design pattern someone could point me in the direction of to solve this kind of problem?
A binding framework like JGoodies Binding may be able to save you a lot of coding. It'll help you easily do things like bind a text editing component to the "text" property of the item selected in a table. Whether you change the selection or the text, it'll do the right thing. Behind the scenes PropertyListener's are being added and removed all the time, but the "nightmare" of keeping the right ones registered has already been implemented for you by the binding framework. I don't believe JGoodies will help you much with modifying the same property on several items selected in a list, but I suspect it wouldn't be hard to implement that. Even if you choose not to use a third-party binding framework, it might give you some ideas for how to make your own reusable binding code for this project.
Joined: Oct 25, 2005
Thanks for the reply. I'll takr a look at the JGoodies framework. I've always been impressed with various JGoodies APIs when I've used them before.
I've bodged a temporary solution for the moment by creating a seperate Object I've called EditSession and having my various GUI components observe this. Then whenever another component makes a change to the model if can post a 'message' to the EditSession and the other components can choose to react if they want to. Not the most elegant of solutions but it works for the moment.