X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scl.compiler%2Fsrc%2Forg%2Fsimantics%2Fscl%2Fcompiler%2Fmodule%2Frepository%2FUpdateListener.java;h=4e3ad119d77bc5a686577e00090bea4b177a6305;hb=8561e498009a25473db94b0e667866aa79de90b1;hp=7320758e48f6a2c1f6f1208db93b90c1b4fb4c96;hpb=657af94d0cf41efb8231e70c0f1167e75938fb16;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/UpdateListener.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/UpdateListener.java index 7320758e4..4e3ad119d 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/UpdateListener.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/module/repository/UpdateListener.java @@ -1,5 +1,41 @@ package org.simantics.scl.compiler.module.repository; -public interface UpdateListener { - void notifyAboutUpdate(); +import gnu.trove.set.hash.THashSet; + +/** + * Listener that is notified about changes in modules and their dependencies. It is possible + * to listen multiple different modules with one listener. When a change happens, the listener + * automatically stops listening any other changes. The idea is that the client then asks all modules + * again using the listener as a parameter. + */ +public abstract class UpdateListener { + private final THashSet observables = new THashSet(); + + public interface Observable { + void removeListener(UpdateListener listener); + } + + public abstract void notifyAboutUpdate(); + + /** + * Registers an observable to the listener. The client code should never + * call this method. It is needed so that it is possible to implement + * {@link #stopListening}. + */ + public void addObservable(Observable observable) { + synchronized(observables) { + observables.add(observable); + } + } + + /** + * Stops listening changes. + */ + public void stopListening() { + synchronized(observables) { + for(Observable observable : observables) + observable.removeListener(this); + observables.clear(); + } + } }