/*
 * @(#) EventHandler.java  1.10 10/09/02
 *
 * Copyright ***,  All Rights Reserved.
 * 
 * This software is the proprietary information of ********  
 * Use is subject to license terms.
 * 
 */

// import java packages
//
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import java.util.*;
import java.io.*;
import java.lang.*;

/**
 * This class contains all elements for a graph arc
 */
public class GraphArc {

    //-----------------------------------------------------------------
    //
    // static data members
    //
    //-----------------------------------------------------------------

    //-----------------------------------------------------------------
    //
    // instance data members
    //
    //-----------------------------------------------------------------

    // vectors of information parsed from the gui resources file
    //
    public Vertex from_d = null;
    public Vertex to_d = null;
    public String weights_d = "0";
    public boolean epsilon_d = false;
    public int xloc_d = 0;
    public int yloc_d = 0;
    public int width_d = 0;
    public int height_d = 0;

    //-----------------------------------------------------------------
    //
    // static function members
    //
    //-----------------------------------------------------------------

    //-----------------------------------------------------------------
    //
    // instance function members
    //
    //-----------------------------------------------------------------

    /**
     * is this arc is pointed 
     * 
     * @param point_a the point to be tested
     * @return true if point is selected
     */
    public boolean isSelected ( Point point_a) {
	
	// if this is a self-loop arc
	//
	if(isSelfArc()){

	    if(point_a.x >= xloc_d 
	       && point_a.x <= xloc_d + width_d 
	       && point_a.y >= yloc_d 
	       && point_a.y <= yloc_d + height_d) {
		// the arc is selected
		//
		return true;
	    }
	}
	else {
			
	    float angle = 
		(float)(height_d - yloc_d) / (float)(width_d - xloc_d);

	    int left_x = 0;
	    int right_x = 0;
	    int low_y = 0;
	    int high_y = 0;

	    if(xloc_d >= width_d) {
		right_x = xloc_d;
		left_x = width_d;
	    }
	    else {
		right_x = width_d;
		left_x = xloc_d;
	    }

	    if(yloc_d >= height_d) {
		high_y = yloc_d;
		low_y = height_d;
	    }
	    else {
		high_y = height_d;
		low_y = yloc_d;
	    }
			    
	    float rule1 = height_d - (float)point_a.y 
		- angle * ( width_d - (float)point_a.x); 
	    float rule2 = yloc_d -1 - (float)point_a.y 
		- angle * ( xloc_d -(float) point_a.x);
	    float rule3 = yloc_d - (float)point_a.y 
		- angle * ( xloc_d - 1 - (float)point_a.x);
	    float rule4 = yloc_d - 2 
		- (float)point_a.y - angle * ( xloc_d  - (float)point_a.x);
	    float rule5 = yloc_d + 1 
		- (float)point_a.y - angle * ( xloc_d  - (float)point_a.x);
	    float rule6 = yloc_d + 2 
		- (float)point_a.y - angle * ( xloc_d  - (float)point_a.x);
	
	    // the condition that the arc is selected
	    //
	    if(((rule1 >= -3 && rule1 <= 3) 
		|| (rule2 >= -3 && rule2 <=3) 
		|| (rule3 >= -3 && rule3 <= 3) 
		|| (rule4 >=-3 && rule4 <= 3) 
		|| (rule5 >= -3 && rule5 <= 3) 
		|| (rule6 >= -3 && rule6 <= 3)) 
	       && point_a.x >= left_x 
	       && point_a.x <= right_x 
	       && point_a.y >= low_y 
	       && point_a.y <= high_y) {

		// the arc is selected
		//
		return true;
	    }
	    
	}
	return false;
    }

    /**
     * checks to see if this arc a self arc 
     * 
     * @return true if it is a self arc
     */
    public boolean isSelfArc() {

	if ( from_d == to_d && from_d != null ){
	    return true;
	}
	return false;
	
    }

    /**
     * get the arc width
     * 
     * @return the arc width
     */
    public int getArcWidth() {

	// return the width
	//
	return width_d;
    }

    /**
     * set the arc width
     * 
     * @param width_a width to set
     */
    public void setArcWidth(int width_a) {

	// return the width
	//
	width_d = width_a;
    }

    /**
     * get the arc height
     * 
     * @return the arc height
     */
    public int getArcHeight() {

	// return the height
	//
	return height_d;
    }

    /**
     * set the arc height
     * 
     * @param height_a height to set
     */
    public void setArcHeight(int height_a) {

	// return the height
	//
	height_d = height_a;
    }

    /**
     * store the vertex, set as the form of properties
     * 
     * @param prefix_a  prefix to be used for properties
     * @param digraph_a current DiGraph object
     */
    public void store(String prefix_a, DiGraph digraph_a) {

	// get the io to set properties
	//
	Properties io = CData.io_d;
	
	// tags
	//
	String arc_from_tag = prefix_a + CData.IO_ARC_FROM_ID;
	String arc_to_tag = prefix_a + CData.IO_ARC_TO_ID;
	String arc_weights_tag = prefix_a + CData.IO_ARC_WEIGHTS;
	String arc_epsilon_tag = prefix_a + CData.IO_ARC_EPSILON;
	String arc_width_tag = prefix_a + CData.IO_ARC_WIDTH;
	String arc_height_tag = prefix_a + CData.IO_ARC_HEIGHT;
	String arc_loc_x_tag = prefix_a + CData.IO_ARC_LOC_X;
	String arc_loc_y_tag = prefix_a + CData.IO_ARC_LOC_Y;

	// values
	//
	String from_id = String.valueOf(digraph_a.vertices_d.indexOf(from_d));
	String to_id = String.valueOf(digraph_a.vertices_d.indexOf(to_d));

	String arc_width = String.valueOf(width_d);
	String arc_height = String.valueOf(height_d);
	String arc_loc_x = String.valueOf(xloc_d);
	String arc_loc_y = String.valueOf(yloc_d);
	String arc_epsilon = String.valueOf(epsilon_d);

	// store all data members
	//
	io.setProperty(arc_from_tag, from_id);
	io.setProperty(arc_to_tag, to_id);
	io.setProperty(arc_weights_tag, weights_d);
	io.setProperty(arc_epsilon_tag, arc_epsilon);
	io.setProperty(arc_width_tag, arc_width);
	io.setProperty(arc_height_tag, arc_height);
	io.setProperty(arc_loc_x_tag, arc_loc_x);
	io.setProperty(arc_loc_y_tag, arc_loc_y);
    }

    /**
     * load the vertex, gets properties
     * 
     * @param prefix_a  prefix to be used
     * @param digraph_a current DiGraph object
     */
    public void load(String prefix_a, DiGraph digraph_a) {

	// get the io to set properties
	//
	Properties io = CData.io_d;
	
	// tags
	//
	String arc_from_tag = prefix_a + CData.IO_ARC_FROM_ID;
	String arc_to_tag = prefix_a + CData.IO_ARC_TO_ID;
	String arc_weights_tag = prefix_a + CData.IO_ARC_WEIGHTS;
	String arc_epsilon_tag = prefix_a + CData.IO_ARC_EPSILON;
	String arc_width_tag = prefix_a + CData.IO_ARC_WIDTH;
	String arc_height_tag = prefix_a + CData.IO_ARC_HEIGHT;
	String arc_loc_x_tag = prefix_a + CData.IO_ARC_LOC_X;
	String arc_loc_y_tag = prefix_a + CData.IO_ARC_LOC_Y;

	// values
	//
	String from_id = io.getProperty(arc_from_tag);
	String to_id = io.getProperty(arc_to_tag);

	String arc_width = io.getProperty(arc_width_tag);
	String arc_height = io.getProperty(arc_height_tag);
	String arc_loc_x = io.getProperty(arc_loc_x_tag);
	String arc_loc_y = io.getProperty(arc_loc_y_tag);
	String arc_epsilon = io.getProperty(arc_epsilon_tag);

	int f_id = Integer.valueOf(from_id).intValue();
	int t_id = Integer.valueOf(to_id).intValue();

	// load all data members
	//
	from_d = (Vertex)digraph_a.vertices_d.get(f_id);
	to_d = (Vertex)digraph_a.vertices_d.get(t_id);
	weights_d = io.getProperty(arc_weights_tag);
	width_d = Integer.valueOf(arc_width).intValue();
	height_d = Integer.valueOf(arc_height).intValue();
	xloc_d = Integer.valueOf(arc_loc_x).intValue();
	yloc_d = Integer.valueOf(arc_loc_y).intValue();
	epsilon_d = Boolean.valueOf(arc_epsilon).booleanValue();
    }
}

// 
// end of file

