All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.cedarsoft.commons.struct.WeakStructureListener Maven / Gradle / Ivy

The newest version!
package eu.cedarsoft.commons.struct;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.ref.WeakReference;

/**
 *
 */
public class WeakStructureListener implements StructureListener {
  private final WeakReference listenerReference;

  public WeakStructureListener( @NotNull StructureListener wrappedListener ) {
    listenerReference = new WeakReference( wrappedListener );
  }

  @Nullable
  public StructureListener getWrappedListener() {
    return listenerReference.get();
  }

  private void removeListener( @NotNull StructPart source ) {
    source.removeStructureListener( this );
  }

  public void childAdded( @NotNull StructureChangedEvent event ) {
    StructureListener wrappedListener = getWrappedListener();
    if ( wrappedListener == null ) {
      removeListener( event.getParent() );
    } else {
      wrappedListener.childAdded( event );
    }
  }

  public void childDetached( @NotNull StructureChangedEvent event ) {
    StructureListener wrappedListener = getWrappedListener();
    if ( wrappedListener == null ) {
      removeListener( event.getParent() );
    } else {
      wrappedListener.childDetached( event );
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy