package org.aikodi.chameleon.core.element;


public class EventManager {
  private final Element _element;

  public EventManager(Element element) {
    if(element == null) {
      throw new IllegalArgumentException("The given element cannot be null.");
    }
    this._element = element;
  }

//  private Set<EventStream<Change,Element>> _streams;

//  void activate(EventStream<Change,Element> stream) {
//    if(_streams == null) {
//      _streams = new HashSet<>(5);
//      activate();
//    }
//    _streams.add(stream);
//  }

  protected void activate() {
  }

//  void deactivate(EventStream<Change,Element> stream) {
//    _streams.remove(stream);
//    if(_streams.isEmpty()) {
//      _streams = null;
//      deactivate();
//    }
//  }

  protected void deactivate() {
    _baseStream = null;
  }
  
  void notify(Event<Change,Element> event) {
    if(_baseStream != null) {
      _baseStream.send(event);
    }
  }

  private BaseStream _baseStream;

  public EventStream<Change,Element> self() {
    if(_baseStream == null) {
      _baseStream = new BaseStream(this);
    }
    return _baseStream;
  }

  public EventStream<Change,Element> descendant() {
    return new DescendantStream(_element);
  }
  
  public EventStream<Change,Element> any() {
    return self().union(descendant());
  }
}
@@ -558,16 +558,57 @@
 ate() %7B%0A
+    _element.enableChangeNotification();%0A
   %7D%0A%0A// 
@@ -848,40 +848,155 @@
 ;%0A  
-%7D%0A  %0A  void notify(Event%3CChange,
+  _descendantStream = null;%0A    _anyStream = null;%0A    _element.disableChangeNotification();%0A  %7D%0A  %0A  void notify(Event%3C? extends Change,? extends 
 Elem
@@ -1276,67 +1276,175 @@
 %0A  p
-ublic EventStream%3CChange,Element%3E descendant() %7B%0A    return
+rivate EventStream%3CChange,Element%3E _descendantStream;%0A  public EventStream%3CChange,Element%3E descendant() %7B%0A    if(_descendantStream == null) %7B%0A      _descendantStream =
  new
@@ -1476,97 +1476,250 @@
 t);%0A
+  
   %7D%0A  
-%0A  public EventStream%3CChange,Element%3E any() %7B%0A    return self().union(descendant())
+  return _descendantStream;%0A  %7D%0A  %0A  private EventStream%3CChange,Element%3E _anyStream;%0A%0A  public EventStream%3CChange,Element%3E any() %7B%0A    if(_anyStream == null) %7B%0A      _anyStream = self().union(descendant());%0A    %7D%0A    return _anyStream
 ;%0A  

finish the event manager