Created
October 6, 2011 10:40
-
-
Save hamstar/1267103 to your computer and use it in GitHub Desktop.
Observable extension example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Helpers; | |
| import java.util.Observable; | |
| import java.util.Observer; | |
| import java.util.Vector; | |
| /** | |
| * @author Shane van Wyk [GFN5594] | |
| * @author Robert McLeod | |
| * @since 02/10/2011 | |
| */ | |
| public class PanelObservable extends Observable { | |
| protected boolean changed; | |
| protected Vector obs; | |
| private Observable observable; | |
| public void setObservable( Observable observable ) { | |
| this.observable = observable; | |
| } | |
| public void setChanged() { | |
| super.setChanged(); | |
| notifyObservers(); | |
| } | |
| public void notifyObservers(Object arg) { | |
| /* | |
| * a temporary array buffer, used as a snapshot of the state of | |
| * current Observers. | |
| */ | |
| Object[] arrLocal; | |
| synchronized (this) { | |
| /* We don't want the Observer doing callbacks into | |
| * arbitrary code while holding its own Monitor. | |
| * The code where we extract each Observable from | |
| * the Vector and store the state of the Observer | |
| * needs synchronization, but notifying observers | |
| * does not (should not). The worst result of any | |
| * potential race-condition here is that: | |
| * 1) a newly-added Observer will miss a | |
| * notification in progress | |
| * 2) a recently unregistered Observer will be | |
| * wrongly notified when it doesn't care | |
| */ | |
| if (!changed) | |
| return; | |
| arrLocal = obs.toArray(); | |
| clearChanged(); | |
| } | |
| if ( observable == null ) | |
| observable = this; | |
| for (int i = arrLocal.length-1; i>=0; i--) | |
| ((Observer)arrLocal[i]).update(observable, arg); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment