/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 */


package org.biojava.bio;

import org.biojava.utils.*;

/**
 * Inidcates that an object has an associated annotation.
 * <P>
 * Many BioJava objects will have associated unstructured data. This should be
 * stored in an Annotation instance. However, the BioJava object itself will
 * probably not want to extend the Annotation interface directly, but rather
 * delegate off that functionality to an Annotation property. The Annotatable
 * interface indicates that there is an Annoation porperty. It also provides
 * an inner class called AnnotationForwarder. When implementing Annotatable, you
 * should always create a protected or private field containing an instance of
 * AnnotationForwarder, and register it as a ChangeListener with the associated
 * Annotation delegate instance.
 *
 * <pre>
 * public class Foo extends AbstractChangeable implements Annotatable {
 *   private Annotation ann; // the associated annotation delegate
 *   protected ChangeListener annFor; // the event forwarder
 *
 *   public Foo() {
 *     // make the ann delegate
 *     ann = new SimpleAnnotation();
 *
 *     annFor = new Annotatable.AnnotationForwarder(
 *       this, // this is the source of the new events
 *       getChangeSupport(Annotatable.ANNOTATION) // the type of the events
 *     );
 *
 *     // add the forwarder to our ann delegate
 *     ann.addChangeListener(annFor, Annotatable.ANNOTATION);
 *   }
 *
 *   public Annotation getAnnotation() {
 *     return ann;
 *   }
 * }
 * </pre>
 *
 * @author  Matthew Pocock
 * @author  Keith James (docs)
 */
public interface Annotatable extends Changeable {
  /**
   * Signals that the associated Annotation has altered in some way. The
   * chainedEvent property should refer back to the event fired by the
   * Annotation object.
   */
  public static final ChangeType ANNOTATION = new ChangeType(
    "the associated annotation has changed",
    "org.biojava.bio.Annotatable",
    "ANNOTATION"
  );

  /**
   * Should return the associated annotation object.
   *
   * @return an Annotation object, never null
   */
  Annotation getAnnotation();

  /**
   * A helper class so that you don't have to worry about forwarding events from
   * the Annotation object to the Annotatable one.
   * <P>
   * Once a listener is added to your Annotatable that is interested in
   * ANNOTATION events, then instantiate one of these and add it as a listener
   * to the annotation object. It will forward the events to your listeners and
   * translate them accordingly.
   *
   * @author Matthew Pocock
   */
  static class AnnotationForwarder extends ChangeForwarder {
    public AnnotationForwarder(Object source, ChangeSupport cs) {
      super(source, cs);
    }

    protected ChangeEvent generateEvent(ChangeEvent ce) {
      ChangeType ct = ce.getType();
      if(ct == Annotation.PROPERTY) {
        return new ChangeEvent(
          getSource(),
          ANNOTATION,
          ct
        );
      }
      return null;
    }
  }
}
