package jdslx.core.ref;

import jdsl.core.api.Decorable;
import jdslx.core.api.*;

/**
 * Implements read and write access to attributes that
 * are stored as decorations.
 *
 * @see jdsl.core.api.Decorable
 *
 * @author Ulrik Brandes 
 * @version 1.0, 07/31/99
**/

public class DecorationAttribute implements Attribute {

  /**
   * Key of decoration.
  **/

  protected Object key_;

  /**
   * Let the attribute be the decoration with key <code>decorationKey</code>.
  **/

  public DecorationAttribute(Object decorationKey) 
  { key_ = decorationKey; }


  /**
   * Read access to the attribute.
   *
   * @return attribute of <code>elem</code>
   * @exception InvalidAttributeException 
   *        if <code>elem</code> is not decorated 
   *        with the key of this attribute, or <code>elem</code> is not
   *	  <code>Decorable</code> at all
  **/

  public Object get(Object elem) { 
    try {
      return ((Decorable) elem).get(key_);
    } catch(ClassCastException e) {
      throw new InvalidAttributeException(elem + " is not decorable.");
    }
  }


  /**
   * Write access to the attribute.
   *
   * @exception InvalidAttributeException 
   *         if <code>elem</code> is not <code>decorable</code>
  **/

  public void set(Object elem, Object value) { 
    try {
      ((Decorable) elem).set(key_, value);
    } catch(ClassCastException e) {
      throw new InvalidAttributeException(elem + " is not decorable.");
    }
  }

}
