// Copyright (c) 1995, 1996 Regents of the University of California.
// All rights reserved.
//
// This software was developed by the Arcadia project
// at the University of California, Irvine.
//
// Redistribution and use in source and binary forms are permitted
// provided that the above copyright notice and this paragraph are
// duplicated in all such forms and that any documentation,
// advertising materials, and other materials related to such
// distribution and use acknowledge that the software was developed
// by the University of California, Irvine.  The name of the
// University may not be used to endorse or promote products derived
// from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
// WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

// File: LayerComposite.java
// Classes: LayerComposite
// Original Author: Jason Robbins
// $Id: LayerComposite.java,v 1.2 2000/09/19 21:08:32 dougo Exp $

// Modified by : Kedar Patankar

package edu.neu.ccs.demeter.tools.apstudio.graphedit;

import java.util.Vector;
import java.util.Enumeration;
import java.awt.Graphics;


/** This class implements a kind of Layer that contains other
 * Layers. Layer's can be nested in an is-part-of tree. That tree can
 * be walked to draw the contents of the view, find what the user
 * clicked on, find a layer by name, save the contents to a file,
 * etc. */

public class LayerComposite extends Layer {

  /** The Layer's contained within this LayerComposite */
  protected Vector _sublayers = new Vector();

  /** In most editors one Layer is the active layer and all mouse
   * clicks go to the contents of that layer. For now I assume this,
   * but I would like to avoid this assumption in the future */
  protected Layer _activeLayer;

  /** Construct a new LayerComposite with no sublayers */
  public LayerComposite() { }

  /** Add a sublayer to this layer */
  public void addSublayer(Layer lay) {
    _sublayers.addElement(lay);
    lay.addObserver(this);
    setActive(lay);
  }

  /** Find a layer with the given name somewhere in the layer tree */
  public Layer findLayerNamed(String aName) {
    Layer res = super.findLayerNamed(aName);
    if (res != null) return res;
    Enumeration layers = _sublayers.elements();
    while (layers.hasMoreElements()) {
      Layer curLayer = (Layer) layers.nextElement();
      res = curLayer.findLayerNamed(aName);
      if (res != null) return res;
    }
    return res;
  }

  /** Make one of my sublayers the active one. */
  public void setActive(Layer lay) {
    if (_sublayers.contains(lay)) _activeLayer = lay;
    else System.out.println("That layer is not one of my sublayers");
  }

  /** Draw the contents of this LayerComposite by drawing all sublayers*/
  public void drawContents(Graphics g) {
    Enumeration layers = _sublayers.elements();
    while (layers.hasMoreElements()) {
      Layer lay = (Layer) layers.nextElement();
      lay.draw(g);
    }
  }

  /** When an editor or some tool wants to look at all the
   * DiagramElement's that are contained in this layer, reply the
   * contents of my active layer. Maybe this should really reply _all_
   * the contents of all sublayers. */
  public Vector contents() {
    if (_activeLayer == null) return null;
    return _activeLayer.contents();
    /* needs-more-work: should accumulate???? */
    /* Vector v = new Vector();
    Enumerations layers = _sublayers.elements();
    while (layers.hasMoreElements()) {
      Layer lay = (Layer) layers.nextElement();
      Vector lay_contents = lay.contents();
      if (lay_contents != null) return lay_contents;
    }  */
  }

  /** When the user tries to add a new DiagramElement to a
   * LayerComposite, pass that addition along to my active layer */
  public void add(DiagramElement de) {
    if (_activeLayer == null) return;
    _activeLayer.add(de);
  }


  /** When the user tries to remove a new DiagramElement from a
   * LayerComposite, pass that removal along to my active layer */
  public void remove(DiagramElement de) {
    if (_activeLayer == null) return;
    _activeLayer.remove(de);
  }

  /** See comments above, this message is passed to my active layer */
  public void removeAll() {
    if (_activeLayer == null) return;
    _activeLayer.removeAll();
  }

  /** See comments above, this message is passed to my active layer */
  public Enumeration elements() {
    if (_activeLayer == null) return null;
    return _activeLayer.elements();
  }

  /** See comments above, this message is passed to my active layer */
  public DiagramElement pick(int x, int y) {
    if (_activeLayer == null) return null;
    return _activeLayer.pick(x, y);
  }

  public DiagramElement pick(UID id) {
    if (_activeLayer == null) return null;
    return _activeLayer.pick(id);
  }

  /** Try to find a Perspective instance that presents the given
   * Net-level object */
  public DiagramElement perspectiveFor(Object obj) {
    DiagramElement de = null;
    Enumeration lays = _sublayers.elements();
    while (lays.hasMoreElements()) {
        Layer sub = (Layer) lays.nextElement();
        de = sub.perspectiveFor(obj);
        if (de != null) return de;
    }
    return null;
  }

} /* end class LayerComposite */

