// 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: Perspective.java
// Classes: Perspective
// Original Author: ics125b spring 1996
// $Id: Perspective.java,v 1.2 2000/09/19 21:08:34 dougo Exp $

// Modified by : Kedar Patankar

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

import java.awt.Rectangle;
import java.awt.Point;
import java.awt.Graphics;

import java.util.Observable;
import java.util.Vector;
import java.util.Enumeration;


/** Class to display graphics for NetNode's */

public class Perspective extends DiagramElement 
{
	
	/** when the Perspective is selected, how close should the selection
	* box be to the Perspective's bbox. Needs-More-Work: this shuold be
	* in SelectionBox. */
	
	private static final int BORDER_WIDTH = 8;

	/** The NetNode that is being displayed */
	private UVertex _parentNode;

	/** The Fig's that make up the look of this Perspective */
	private Fig _fig;
	
	/** True when we want to draw the user's attention to this
	* Perspective */
	boolean _highlight = false;

	public Perspective(){}

  /**  Constructs a new Perspective on the given NetNode with the
   *  given Fig's. */
	public Perspective(UVertex parent_node, Fig fig) 
	{
		_fig=fig;
		_parentNode = parent_node;
	  //Look into this afterwards
		_parentNode.addObserver(this);
	}

	private Vector arcPerspectives(Document ed)
	{
		Vector arcPerspectives = new Vector();
		UGraph nl=ed.net();
		Enumeration arcs = _parentNode.get_inArcIdList().elements();
		while (arcs.hasMoreElements()) 
		{
			DiagramElement de = nl.getArc((UID) arcs.nextElement()).get_persp();
			arcPerspectives.addElement(de);
		}
		arcs = _parentNode.get_outArcIdList().elements();
		while (arcs.hasMoreElements()) 
		{
			DiagramElement de = nl.getArc((UID) arcs.nextElement()).get_persp();
			arcPerspectives.addElement(de);
		}
		return arcPerspectives;
	}

  /** Replys a rectangle that bounds all the Fig's in this perspective*/
	
	public Rectangle getBoundingBox() { return _fig.getBoundingBox(); }

  /** Move the Perspective by the given amount */

	void translate(int dx,int dy)
	{
		position().x += dx;
		position().y += dy;
		_fig.translate(dx,dy);
	}


  /** When a Perspective is damaged, all of its arcs may need
   * redrawing. */
  public void damagedIn(Document ed) 
  {
    Enumeration arcPers = arcPerspectives(ed).elements();
    while (arcPers.hasMoreElements()) {
      DiagramElement de = (DiagramElement) arcPers.nextElement();
      de.damagedIn(ed);
    }
    super.damagedIn(ed);
  }

  /** When a Perspective is removed, dispose all of the NetArcs and
   * the underlying NetNode. Needs-More-Work: is this right? Doesn't
   * ActionDelete call this method also? How is deletion of a
   * Perspective handled without disposing the underlying model? */
  public void removeFrom(Document ed) {
    Enumeration arcPers = arcPerspectives(ed).elements();
    while (arcPers.hasMoreElements()) {
      DiagramElement de = (DiagramElement) arcPers.nextElement();
//	  System.out.println("calling arc disposal from vertex");
      de.dispose(ed);
    }
    if (_parentNode != null) _parentNode.dispose(ed);
    super.removeFrom(ed);
    _parentNode = null;
  }

  /** The owner of a Perspective is an underlying NetNode */
  public Object owner() { return _parentNode; }

  /** Reply the dimensions of the FigRect which is the first figure added to FigList*/

  public Fig get_figure(){ return _fig;}
  public Point get_dimensions(){ return _fig.get_dimensions();}


  /** Reply true iff the point lies within any of the Fig's in the
   * perspective. */
  public boolean inside(int x, int y) {
	  return (_fig.inside(x, y));
  }

  public boolean hasId(UID id)
  {
	  if(((UVertex)owner()).get_vid().equals(id))
		  return true;
	  return false;
  }
  /** Reply true iff this Perspective intersects the given Rectangle */
  public boolean intersects(Rectangle rct) {
    return getBoundingBox().intersects(rct);
  }

  /** Reply the NetNode associated with the Fig under the mouse, or
   * null if there is none. */
  public final UVertex pickNode(Point p) { return pickNode(p.x, p.y); }

  public UVertex pickNode(int x, int y) {
	  boolean ret=_fig.inside(x,y);
	  if(ret) return (UVertex)owner();
    else return null;
  }

  /** Draws the perspective to the given Graphics */
  public void draw(Graphics g) 
  {
    Rectangle clip = g.getClipBounds();
    Rectangle bbox = getBoundingBox();
    if (clip == null || bbox.intersects(clip)) 
	{
		if(_parentNode.get_vdeco()!=null)
		{
			Integer i = _parentNode.get_vdeco().get_travtag();
			if(i.intValue()==3)
				_fig.drawColored(g);
			else
				_fig.draw(g);
		}
		else
			_fig.draw(g);
    }
    if (_highlight) {
      g.setColor(Globals.prefs().highlightColor()); /* needs-more-work */
      g.drawRect(bbox.x-3, bbox.y-3, bbox.width+6, bbox.height+6);
      g.drawRect(bbox.x-4, bbox.y-4, bbox.width+8, bbox.height+8);
    }
  }

  /** Needs-More-Work: remove this, its logic has been moved to
   * SelectionBox. */
  public void drawSelected(Graphics g) { }


  /** When a Perspective is selected the Editor should record that
   * fact in an instance of SelectionBox. */
  public Selection selectionObject() { return new SelectionBox(this); }

  /** If I get a notification that I shuold be Highlighted or
   * Unhighlight, set a flag and mark my area as damaged. */
  public void update(Observable o, Object arg) {
    if (arg.equals("Highlight")) {
      startTrans();
      _highlight = true;
      endTrans();
    }
    else if (arg.equals("Unhighlight")) {
      startTrans();
      _highlight = false;
      endTrans();
    }
  }

    // Pratima R Kayiti**** PRK ****
  public void setGraphicAttribute(String k, Object v){
    }
 
} /* end class Perspective */


