Number of links at a port

I started a topic concerning ValidLink but this actually came out of a different issue I was seeing so I thought I would ask about that and see if anyone had some insight into what was going on.

I am capturing the item inserted event of the view document and then checking business rules to see if the “link” is allowed. (This is from the document changed event and then checking for the inserted flag in the event) These business rules are dependent on what “type” of link they are trying to create hence the reason I would like to have that information in the ValidLink event of the view. Anyways what I noticed is if I decide the link is not allowed during the inserted event, I call removeObject of the document passing in the newly created link. This works OK view wise but if I go to the port of one of the nodes then the count is incorrect and the port still thinks the link is still present. As a matter of fact running through the collection of links attached to the port still shows the removed link.

My questions are, Is RemoveObject the right method to call and is there a better event to capture? Thanks.

If you are handling the JGoDocumentEvent.INSERTED event, you can call getJGoObject on the JGoDocumentEvent argument that is passed to your handler and determine what kind of link was just inserted.

Calling removeObject should result in JGoLink.ownerChange being called, which is responsible for removing the link from the ports it was connected to. You might want to set a breakpoint in JGoLink.ownerChange and see what's happening.
FYI, an alternative would be to create your own subclass of JGoView and override validLink to only return true if the connection is valid. This would avoid creating the link, detectecting the creation of that link, and then removing the link. The JGoView.validLink method is called prior to creating the link in the first place.

I actually do have my own view class and am checking valid link but I need to know what “kind” of link is being connected. It would be really nice if there was an event such as link draw starting that happened before valid link so I could set the link type on my view.

JGoView.startNewLink is called at the beginning of the process of drawing a new link. The argument port is the port from which the user is “drawing” the new link.

That port is passed to the JGoView.validLink predicate to determine whether a link is permitted to be drawn between two ports. Whether that start port is the first or the second argument to the JGoView.validLink predicate depends on whether the user is drawing “backwards”.

So you could override the startNewLink method to decide what “kind” of link to draw.

JGoView.newLink is the method that is called to actually construct a JGoLink, connect it up to two ports, and add it to the JGoDocument.

So you could override the newLink method to customize the kind of link that is created, such as by constructing different link classes.