Is an event triggered when links are constructed

Hi,

I would like my code to respond to the creation of a new link between GoObjects. As the user clicks on an object’s port (BoxNode port), drags the mouse to another GoObject’s port (another BoxNode) and releases the mouse, I would like to be able to capture the information about the link created. Is there an event/method that I can subscribe to that will provide information about the newly created link? My link lines are custom instantiations of a GoLabeledLink that set the Orthogonal property to true
and are styled with GoStrokeStyle.RoundedLineWithJumpOvers.

Thank you.

R. Houston

Yes, you can subscribe to LinkCreated event. The GoObject property of the GoSelectionEvent Args passed to your event handler will be the GoLabeledLink that was just created.

OK, thats what I thought, but I’m still missing an understanding of how links get created. Shouldn’t the class specified in the GoView.NewLinkPrototype
property get instantiated each time a new link is created? My custom GoLabeledLink (GoConnection) only seems to be instantiated one time, when the line of code: “GoView.NewLinkPrototype = New GoConnection()” is executed. My custom link class implements the interface IGoLink.

Since link creation doesn’t seem to be happening the way I was thinking it would, what approach should I use to tie the newly-created link to an instance of my GoConnection class?

Thanks.

R. Houston

That’s how it’s supposed to work. You haven’t overriden GoView.CreateLink, have you?

I think the two of you are misunderstanding each other with respect to the term “instantiation”.

Calling a constructor is not the only way to create an instance of a class in .NET.
You can also make a copy of an object by using Object.MemberwiseClone().

That’s what GoObject.CopyObject does. Read its documentation for more information.

So if you’ve set a breakpoint in your constructor, it won’t get tripped when a GoObject is copied.

Ok, thank you both for your comments.

Regarding object cloning, it seems to me that this mechanism performs a shallow copy not a deep copy. This creates somewhat of a problem for me in that I persist in my custom GoConnection class, a member variable that maintains information about the link (i.e., it’s style, color, from and to endpoints, etc…). The information maintained by this member variable is what I write to an XML file. Unfortunately, with each new link created by the user, the created clone, or shallow copied object, continues to point to the one instance of my member variable created at the time the line of code:

GoView.CreateLinkPrototype = New GoConnection()

is executed. What I really need is for a separate instance of my custom GoConnection class to be created each time a link is created so that a new instance of the member variable will also be created and therefore can preserve the state for that link. Would either of you have any thoughts or suggestions for how to achieve this or should I consider a redesign in this case?

Thank you again.

The documentation for GoObject.CopyObject talks about this.

That’s why you need to override CopyObject to fix up references to other things. (Or override GoGroup.CopyChildren to fix up references to the group’s child objects, if you have added any such fields.)

I’ve checked the CopyObject example code you provided in the Demo project and I coded my case similarly to yours but CopyObject isn’t working for me. First, CopyObject is called twice per link line. Why is this? Is it called once for each end of the link? Second, regardless of how many times CopyObject is called, neither the From or To information about the link line is filled in. I guess I was expecting back from the Base.CopyObject(env) an instance of an object with the From and To information filled in. Am I doing something wrong or is this expected behavior? Here’s what I’ve done inside my override of CopyObject

  1. Obtain a copy of my custom GoConnection class:

thisGoConnection = CType(MyBase.CopyObject(env), GoConnection)

  1. Create new instances of member variables outside of the GoConnection class and then using public properties on my GoConnection class, assigning member variables with the new values:

member_var = new Member_Var()
GoConnection.MemberVar = member_var

  1. Return the (copied) instance of the GoConnection class from the CopyObject function:

return thisGoConnection

  1. While in CopyObjects I was hoping to persist information about the link (the from and to information for example) into my member variable but the needed data is not being provided. Do I only have visibility to From and To information as part of the LinkCreated event?

I’d like to understand this better and do it the right way.

Thanks a lot.

R. Houston

The GoView.NewLinkPrototype is copied twice for a user’s link-drawing gesture. The first time is to create a prototype link object that is shown and modified while the user is dragging the mouse. The second time is to create the actual link that is connected to the ports and added to the document.

Of course the second copy might not happen because the user cancels the link or because no link is valid at the mouse point.

The prototype link is not modified to be connected to the two ports before the link is copied. The GoToolLinkingNew tool needs to copy the link before connecting its ports.

The GoView.LinkCreated event happens after the link has been copied, connected to the ports, and added to the document.

I think the concept that you are looking for is that only after an object has been added to a document you might consider persisting information about it. If some code makes a bunch of copies of a node, or if some code creates a bunch of new nodes by calling the constructor, that doesn’t mean much, if anything. It’s only when there is a relationship between newly created objects and existing “known” objects, such as when an object is added to a document or is linked to it somehow, that the new object can really have any “meaning”.

In your case, you might also need to worry about what happens when a link is relinked, if that is permitted in your application.

I suspect you need to worry about the structural and referential integrity of your objects during CopyObject, and worry about persistence at a separate/later time. The latter usually happens just when a document is saved or when a GoDiagram transaction completes.

Thanks Walter. Your answer helped. I appreciate your time.

R. Houston