Prevent selecting links when nodes are selected


I have enabled the drag selection tool. When the user selects a number of nodes and links, I would like only the nodes to be selected. However, I would still like the user to be able to select one link at a time when necessary.

I tried calling isSelected on the relevant links in the selection when the ChangedSelection event is received, but that does not seem to work as I expect it --and the documentation clearly states that the diagram or the selection should be not changed on ChangedSelection.

Would there be a way to do this?


I suggest overriding DragSelectingTool.selectInRect. Call the base method first, and then deselect any Links. Remember that while iterating over the Diagram.selection collection you cannot modify the collection, so you will either need to iterate over a copy of the Diagram.selection or you will need to iterate over the Diagram.links collection or you will need to find another way to find the links that you want to deselect.

Thanks Walter. But this will not help when Ctrl-clicking nodes to update the selection I believe.

Would you have any other idea?


Clicking or control-clicking on nodes won’t affect the selection of links.

Right, sorry, I meant Ctrl-clicking links of course.

Overriding DragSelectingTool.selectInRect will only affect the drag selection process, not any clicking behavior, which is handled by the ClickSelectingTool.

If I try to override DragSelectingTool.selectInRect, this gets called after the ChangedSelection event is fired. But I do need to also handle ChangedSelection, as I also want to perform the same checks when programmatic selection is performed. So I need to handle this the same way on a drag selection, on a ctrl-click selection, and on a programmatic selection.

Am I really not allowed to call Node.isSelected and Link.isSelected when handling ChangedSelection? The way this is worded in the document, it sounds like I am not really allowed to make such changes, but this is not so clear.


Well, in your ChangedSelection listener you can skip any Links that you don’t care about.

That is right – you may not set Part.isSelected within the ChangedSelection DiagramEvent. That would modify the collection while listeners are handling the event.

I suppose you could implement DragSelectingTool.selectInRect to only select Nodes. Maybe if all of your Links are in their own Layer, you can set Layer.allowSelect = false, call the base method, and then set Layer.allowSelect back to true again.