// File: LayerComposite.java // Classes: LayerComposite // Original Author: Jason Robbins // $Id: LayerComposite.java,v 1.1.1.1 1997/02/27 20:52:41 chandra Exp $ package uci.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(int 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; } /** See comments above, this message is passed to my active layer */ public void sendToBack(DiagramElement prim) { if (_activeLayer == null) return; _activeLayer.sendToBack(prim); } /** See comments above, this message is passed to my active layer */ public void bringForward(DiagramElement prim) { if (_activeLayer == null) return; _activeLayer.bringForward(prim); } /** See comments above, this message is passed to my active layer */ public void sendBackward(DiagramElement prim) { if (_activeLayer == null) return; _activeLayer.sendBackward(prim); } /** See comments above, this message is passed to my active layer */ public void bringToFront(DiagramElement prim) { if (_activeLayer == null) return; _activeLayer.bringToFront(prim); } /** See comments above, this message is passed to my active layer */ public void reorder(DiagramElement prim, int function) { if (_activeLayer == null) return; _activeLayer.reorder(prim, function); } } /* end class LayerComposite */