// 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: ArcPerspective.java
// Classes: ArcPerspective
// Original Author: Jason Robbins
// $Id: ArcPerspective.java,v 1.2 2000/09/19 21:08:30 dougo Exp $

// Modified by : Kedar Patankar

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

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

/** A DiagramElement that draws arcs between two vertices.*/

public class ArcPerspective extends DiagramElement 
{

	  private Perspective _sourcePerspective;
	  private Perspective _destPerspective;
	  private Fig _edge;
	  private UEdge parentArc;
	  public ArcPerspective(){}
	  public ArcPerspective(UEdge a,Document d)
	  {
		  _destPerspective = a.get_sourcePerspective(d);
		  _sourcePerspective = a.get_destPerspective(d);
		  _edge=new FigAltEdge(_sourcePerspective.center(),
			  _destPerspective.center(),Color.black);
		  owner(a);
		  a.set_persp(this);
	  }

	  public ArcPerspective(UEdge a,Document d,Point p)
	  {
		  _destPerspective = a.get_sourcePerspective(d);
		  _sourcePerspective = a.get_destPerspective(d);
		  _edge=new FigAltEdge(_sourcePerspective.center(),
			  _destPerspective.center(),Color.black,p);
		  owner(a);
		  a.set_persp(this);
	  }
	  public ArcPerspective(UEdge a,String edgeLabel,String cardinality,Document d)
	  {
		  _sourcePerspective = a.get_sourcePerspective(d);
		  _destPerspective = a.get_destPerspective(d);
		  _edge = new FigConstEdge(_sourcePerspective.center(),
			  _destPerspective.center(),Color.black,edgeLabel,cardinality);
		  owner(a);
		  a.set_persp(this);
	  }
	  public ArcPerspective(UEdge a,String edgeLabel,String cardinality,Document d,Point p)
	  {
		  _sourcePerspective = a.get_sourcePerspective(d);
		  _destPerspective = a.get_destPerspective(d);
		  _edge = new FigConstEdge(_sourcePerspective.center(),
			  _destPerspective.center(),Color.black,edgeLabel,cardinality,p);
		  owner(a);
		  a.set_persp(this);
	  }
	  public void owner(UEdge n){parentArc=n;}
	  public Object owner(){return parentArc;}
	  public Perspective get_sourcePerspective(){return _sourcePerspective;}

	  public String get_SourceName()
	  {
		  return ((UVertex)_sourcePerspective.owner()).get_vertexname().get_name().toString();
	  }
	  public String get_DestinationName()
	  {
		  return ((UVertex)_destPerspective.owner()).get_vertexname().get_name().toString();
	  }


	  /** Return the figure held by this perspective */
	  public Fig get_figure(){ return _edge;}
	  /** Draw this object.
	   * @see DiagramElement#drawSelected */
	  public void draw(Graphics g) 
	  {
		if(parentArc.get_edeco()!=null)
		{
			Integer i = parentArc.get_edeco().get_travtag();
			if(i.intValue()==3)
				drawColoredArc(g);
			else
			{
				g.setColor(Color.black);
				drawArc(g);
			}
		}
		else
		{
			g.setColor(Color.black);
			drawArc(g);
		}
	  }

	  /** Draw this object when the user has selected it.
	   * @see DiagramElement#drawSelected */
	  public void drawSelected(Graphics g) {
		  drawSelectedArc(g,Globals.prefs().handleColor());
	  }

	  void translate(int mx, int my)
	  {
		  _edge.translate(mx,my);
	  }
	  public int pickHandle(int x, int y) {	return _edge.pickHandle(x,y);}

	  public void dragHandle(int mx, int my, int handle)
	  {
		  _edge.dragHandle(mx,my,handle);
	  }

	  /** reply the bounding box for this arc.
	   * @see DiagramElement#getBoundingBox */
	public Rectangle getBoundingBox() 
	{
		 Rectangle bbox = null;
		 bbox=_edge.getBoundingBox();
		 return bbox;
	}


   public boolean inside(int x, int y)
   {
	   if(_edge.inside(x,y))
		   return true;
	   return false;
   }

  /** reply true iff this arc intersects the given rectangle.
   * @see DiagramElement#intersects
   */
  public boolean intersects(Rectangle rect)
  {
	  if (_edge.intersects(rect)) return true;
	  return false;
  }


/*  public void dispose(Document ed) 
  {
    if (parentArc != null) parentArc.dispose(ed);
//    parentArc.dispose(ed);
    super.dispose(ed);
    parentArc = null;
  }*/
  public void removeFrom(Document ed) 
  {
    if (parentArc != null) 
		parentArc.dispose(ed);
    super.removeFrom(ed);
    parentArc = null;
  }


  /** find the route that the arc should follow. Basically case
   * analysis to route around source and destination nodes. <p>
   * needs-more-work: a better algorithm would really be useful. */

  protected void computeRoute()
  {
	Point srcPt = _sourcePerspective.center();
    Point dstPt = _destPerspective.center();
	_edge.set_position(srcPt,dstPt);
  } /* end computeRoute */

  void drawSelectedArc(Graphics g, Color l_color)
  {
	  computeRoute();
	  Point destDimension=_destPerspective.get_dimensions();
	  Point sourceDimension=_sourcePerspective.get_dimensions();
	  _edge.draw(g,l_color,sourceDimension,destDimension);
	  _edge.drawSelected(g);
  }

  /** Draw the arc */
  void drawArc(Graphics g)
  {
	  computeRoute();
	  Point destDimension=_destPerspective.get_dimensions();
	  Point sourceDimension=_sourcePerspective.get_dimensions();
	  _edge.draw(g,sourceDimension,destDimension);
  }
  void drawColoredArc(Graphics g)
  {
	  computeRoute();
	  Point destDimension=_destPerspective.get_dimensions();
	  Point sourceDimension=_sourcePerspective.get_dimensions();
	  _edge.drawColoredArc(g,sourceDimension,destDimension);
  }

  /** reply a new selection object that can be used to manipulate this
   * object. For now, use SelectionHandles. needs-more-work: users
   * should be able to adjust arcs by dragging on handles */
  public Selection selectionObject() {
    return new SelectionHandles(this);
  }

   public void setGraphicAttribute(String k, Object v)
   {
   }

} /* end class ArcPerspective */

