// $Id: CheckConnection.java,v 1.7 2003/02/19 14:39:52 tonya Exp $ ; $Name:  $
//
// Example code for CheckConnection

import rsdesigner.uiextension.*;
import rsdesigner.component.*;
import rsdesigner.design.*;
import rsdesigner.javagateway.*;
import java.util.*;
/**
 * Describe class <code>CheckConnection</code> here.
 *
 * @author Simon Geard
 * @version 1.0
 */
public class CheckConnection extends Object implements rsdesigner.uiextension.Connection {

    /**
     * Creates a new <code>CheckConnection</code> instance - this constructor must be public
     * and must not have any arguments
     *
     */
    public CheckConnection() {}

    /**
     * <code>canConnectBetween</code> checks to see if the connection is permitted
     *
     * If loaded this procedure will be called on mouse-motion events in the fibre-creation process
     * so you shouldn't do any unnecessary or time consuming stuff here.
     *
     * @param item1 the parent <code>Shape</code> of the <code>PortShape</code> from which the connection is to be made
     * @param port1 the <code>String</code> representation of the sysID of the <code>PortShape</code> from which the connection is to be made
     * @param fibre the <code>FibreShape</code> <i>prototype </i> which will be used in the connection
     * @param item2 the parent <code>Shape</code> of the <code>PortShape</code> to which the connection is to be made
     * @param port2 the <code>String</code> representation of the sysID of the <code>PortShape</code> to which the connection is to be made
     * @return <code>boolean</code> - false if the connection is prohibited, true otherwise
     * @exception RSDException if an error occurs
     */
    public boolean canConnectBetween(Shape item1, String port1, FibreShape fibre, Shape item2, String port2) throws RSDException
    {
        // Find out where we are in the lunar cycle
        Calendar now = Calendar.getInstance();

        // Disallow connections with Purple_Wire unless it's the first of the month
        String fname = fibre.getArtifact().getName();
        if (fname.equals("Purple_Wire") && (now.get(Calendar.DAY_OF_MONTH)) != 1)
        {
            System.out.println("***Error: Purple_Wire can only be used on the first of the month");
            return false;
        }

        // Get the sysids of the ports
        System.out.println("port 1 = "+port1+" ; port 2 = "+port2);
        SysID pid_1;
        if (port1.equals("UNSET"))
        {
            pid_1 = new SysID();
        }
        else
        {
            pid_1 = new SysID(port1);
        }
        SysID pid_2;
        if (port2.equals("UNSET"))
        {
            pid_2 = new SysID();
        }
        else
        {
            pid_2 = new SysID(port2);
        }

        // Get the names of the artifacts
        String name1 = item1.getArtifact().getName();
        String name2 = item2.getArtifact().getName();

        // Report a valid connection
        System.out.println("Connecting " + name1 + " (port " + pid_1.toString() + ") through " + fname + " to " + name2 + " (port " + pid_2.toString() + ")");

        return true;
    }

    /**
     * The postConnect method is used to modify the properties of objects that are
     * connected (either using a fiber or direct connections between components).
     * The method in this example will check set the max_current property of the
     * fiber that connects two components.
     *
     * @param mp a collection of Properties for each of the Artifacts that will be connected
     * @exception RSDException if an error occurs
     */
    public void postConnect(ManageProperties mp) throws RSDException
    {
        System.out.println("[postConnect]");
        Double max_c = new Double(0);
        rsdesigner.design.Properties fibre_p = null;
        for (Enumeration e = mp.elements() ; e.hasMoreElements() ;) {
            rsdesigner.design.Properties p = (rsdesigner.design.Properties) e.nextElement();
            DBItem item = p.getPropertySet();

            // Extract the fibre
            if (item instanceof Fibre) {
                fibre_p = p;

            } else {
                // Get the max_current
                if (p.exists("max_current")) {
                    Object or = p.getValue("max_current");
                    Double r = new Double(or.toString());
                    System.out.println("max_current = "+r+" c.f. "+max_c);
                    max_c = (r.compareTo(max_c)>0) ? r : max_c;
                    System.out.println("\t->"+max_c);
                }
            }

        }

        if (fibre_p != null) {
            if (max_c.doubleValue() > 0.0) {
                if (fibre_p.exists("max_current")) {
                    Object or = fibre_p.getValue("max_current");
                    Double r = new Double(or.toString());
                    System.out.println("fibre max_current = "+r+" c.f. "+max_c);
                    max_c = (r.compareTo(max_c)>0) ? r : max_c;
                    System.out.println("\t->"+max_c);
                }
                // Set the max_current on the fibre to be max_c
                fibre_p.add("max_current","real",max_c.toString());
                System.out.println("setting max_current to "+max_c);
            } else {
                System.out.println("max_current not set - property probably undefined");
            }

        } else {
            System.out.println("direct connection - no fibre");
        }
    }

}
