Restricting certain Port connections

Hi,

I would like to restrict certain combinations of connections and have
looked into validLink() to achieve that but I’m getting myself into all
sorts of problems so was wondering if overriding the validLink() within
the JGoView would be a better place to achieve what I am wanting to do…

Ok so heres what I would like to happen (Its based loosly on the
processor application to some degree…). I have a couple of different
node types lets call them A,B and C for now.

  1. When a node A is placed and an attempt to connect a special output
    port (Lets call this link) to another nodes input. It will normally
    allow this. (So A can connect to B’s input)

  2. If a subsequent link from that node A, attempts to connect to
    node B then it is ignored (can use !isAlreadyLinked() for that as per
    the example)

  3. If node A then attempts to connect to another instance of B on the diagram then this is allowed

  4. if node A attempts to connect to node C then this is disallowed, as
    only nodes of the same type as to that of the original connection can
    be connected. So if I wanted to connect A to C I would have to first
    remove all of the other conenctions and then connect A to C.

Phew… so thats basically what I want to do. At the moment what I have
tried has leaned around that of the validLink override from JGoPort.
What I had was something like this…

     if(getNumFromLinks() == 0 )

     &nbs p;        {

     &nbs p;           

RemoteNode node = (RemoteNode)to.getParent();

     &nbs p;           

this.AllowNodesofType = node.getQueueType();

     &nbs p;           

return true;

     &nbs p;        }

So on the first connection I set my allowable node types and on
subsequent calls, if getNumFromLinks() > 0 then I would check to see
what the remote node type is verses that of my AllowNodesofType value
and return TRUE for them being the same or FALSE if they are not.

What would appear to happen is that the validLink appears to get called
before an attempt to connect it to the second node is undertaken, so
many of these can occur by the looks of it which I think leaves my
value of AllowNodesofType suspect

So is it better to do this checking at the view level when I have the
to and from node within validLink() or should what I am trying to do
work at the port level ?

Any ideas on this am I on the right track for doing this

Thanks

Wayne

I don’t think you want to be changing the state of your nodes/ports during the computation of validLink. It’s true that validLink may be called for lots of pairs of ports.
It would be better to only record that information after the link has actually been completed. That could be either in overrides of JGoView.newLink and JGoView.reLink, or equivalently, in JGoViewListeners looking for JGoViewEvent.LINK_CREATED and LINK_RELINKED.

Walter,

Thanks for that. Yeah doing the checking in the view worked far better.
I did however find out what was tripping me up in the validLink in the
JGoPorts way. So for anyone else wanting to do similar things the
ValidLink would appear to get called N times, once for each node within
the view. I originally thourght it would only get called once for the
inteneded connection. So after working that out (As always learning on
the way) it was an easy choice to go with the view method of doing.

Thanks