How to get the separation of the view from the model when using SwingWorker with a lengthy process that should send updates back to the controller?
I can use SwingWorkers doInBackground() to support the EDT response by calling, for example, model.doLongProcess() where it was great!
I have an attempt to return data before the process is completed in order to update the view with progress.
I know that I can get the data back using the SwingWorkers publish() method, but this, I think, forces me to write code for the doLongProcess() method in doInBackground() .
For reference: my MVC implementation looks something like this:
http://www.leepoint.net/notes-java/GUI/structure/40mvc.html
/ structure/calc-mvc/CalcMVC.java -- Calculator in MVC pattern.
I have one model class that combines a number of other classes into a simple interface for the controller.
I really do not want to move all / some / any code from these classes to the controller - it does not belong there.
Update:
This is the approach that I take - this is not the purest decision, and it can be taken as an abuse of PropertyChangeSupport .. at a semantic level.
Basically, all low-level classes that have long methods will have a PropertyChangeSupport field. Longer running methods periodically call firePropertyChange() to update the status of this method and not necessarily report a property change - this is what I mean by semantic abuse!
Then the Model class, which wraps the low-level classes, catches these events and throws its own high level firePropertyChange .., which the controller can listen to ...
Edit:
To clarify when I call firePropertyChange (propertyName, oldValue, newValue);
- propertyName ---> I am abusing the propertyName property to represent the topic name
- oldValue = null
- newValue = the message I want to convey
Then, the PropertyChangeListener property in the model or sometime can recognize the message based on the topic name.
So, Iv basically bent the system to use it as a publish-subscribe ....
I assume that instead of this method, I could add a progress field to the lowlevel classes that will be updated, and then firePropertyChange based on this .. this will correspond to how it is supposed to be used.