// $Id$ ; $Name$
// MyPort.java
//
import rsdesigner.design.*;
import rsdesigner.component.*;
import java.util.*;
/*
    Methods that can be called on a port to provide useful information
*/

public class MyPort
{
    static String separator = ",";
    /**
        this method is for putting on a cross-sheet port shape
        so that it can show the name of the object(s)
        it's connected to on the other sheet
    */
    public static String connected(Shape shape) throws RSDException
    {
        String result = null;

        if (!(shape instanceof PortShape))
        {
            return "No valid port shape supplied";
        }

        PortShape portshape = (PortShape) shape;
        //we need to be sure it's a fiber shape's port shape
        Shape parent = portshape.getContainer();
        if (parent == null)
        {
            return "No Parent";
        }
        if (!(parent instanceof FibreShape))
        {
            return "Parent must be a fiber shape";
        }

        //ok, we've done the checks, now the fun begins

        //let's assume the portshape that was passed in has other shapes
        //elsewhere, because it's a cross-sheet portshape. So what we need
        //to do is get those other portshapes and report what they're connected
        //to via a fibershape.

        //to get the other portshapes we first need to get our portshape's artifact,
        //then we get it shapes and filter out our portshape.
        Port port = (Port) portshape.getArtifact();
        //get our portshape's sys_id so we can use it to filter the portshape out
        //from the list of portshapes we'll get from the artifact
        SysID mySysID = portshape.getSysID();
        Iterator shapes = port.getShapes();
        while(shapes.hasNext())
        {
            //get the next portshape that comes out of the iterator
            PortShape otherportshape = (PortShape) shapes.next();
            //just make sure it's not our portshape
            if (mySysID.equals(otherportshape.getSysID()))
            {
                //System.out.println("Found our portshape");
                continue;
            }

            //now we have another portshape for the port we can get its parent
            //(It has to be a fibershape). From there we can get the other portshapes
            //on the fibershape and report what they're connected to
            String thisResult = findShapesAtOtherEnd(otherportshape);
            if (thisResult != null)
            {
                if (result != null)
                {
                    result = result + separator + thisResult;
                }
                else
                {
                    result = thisResult;
                }
            }

        }
        //allow for the fact that we might not have found any connected objects
        if (result != null)
        {
            return result;
        }
        else
        {
            return "*";
        }
    }

    /**
        This method gets the parent fibreshape of the supplied portshape
        and then returns the portshapes
    */
    static String findShapesAtOtherEnd(PortShape myportshape) throws RSDException
    {
        String result = null;
        //get the parent of our portshape and then obtain its portshapes. Filter out our
        //portshape so we just get the other portshapes for this fibershape. From there
        //we can report what they are connected to.

        //get this portshape's sys_id so we can filter it from the list of portshapes that the
        //fibershape has
        SysID mySysID = myportshape.getSysID();

        //now get the set of portshapes and work out what they're connected to
        ComponentShape parent = (ComponentShape) myportshape.getContainer();
        Iterator portshapes = parent.getPortShapes();
        while(portshapes.hasNext())
        {
            PortShape otherportshape = (PortShape) portshapes.next();
            if (mySysID.equals(otherportshape.getSysID()))
            {
                continue;
            }

            //ok we've got a portshape at the other end of the fibreshape, so
            //now report what it's connected to
            String connectedobjects = findConnectedObjects(otherportshape);
            if (result != null)
            {
                result = result + separator + connectedobjects;
            }
            else
            {
                result = connectedobjects;
            }
        }
        return result;
    }

    /**
        This method returns the full name of objects that are connected to this portshape
    */
    static String findConnectedObjects(PortShape myportshape) throws RSDException
    {
        String result = null;
        //look at what the portshape is connected to and report the fullname of
        //the connected port's parent
        Iterator portshapes = myportshape.getConnectedPortShapes();
        while (portshapes.hasNext())
        {
            PortShape portshape = (PortShape) portshapes.next();
            //get the connected portshape's parent and then its artifact so we can get its full name
            ComponentShape parentshape = (ComponentShape) portshape.getContainer();
            Artifact parent = parentshape.getArtifact();
            String fullname = parent.getStringProperty("full_name", null);

            //if we got a valid full name then return it
            if (fullname != null)
            {
                if (result != null)
                {
                    result = result + separator + fullname;
                }
                else
                {
                    result = fullname;
                }
            }
        }
        return result;
    }
}

