How to stop nodes sitting on each other?

Hi,

I’m starting to get to grip’s with JGo at the moment so still trying to
find my feet across the package. So if this is a dead simple question
please forgive my ignorance.

Is there away to stop a user from dragging a node ontop of another node

? In my diagram at the moment if they do that the layout lines end up
crossing the node that the dragged node has been placed on, so they
dont get routed arounf it, so not the effect I would like. idealy I
would love to reset the drag (I guess like an undo operation) or
provide some more intelegence to the dragged node. So that it moves to
an unoccupied space that is in the vicinity of the dragged position,
but not on top of it.

Any hints on this or if there is example code in the many examples that anyone could point me to that would be great

Thanks in advance

Wayne

Oh and if you think you have saw this post before today, yes like a
fool I posted it to GoDiagram ! Well thats what happens on a public
holiday when I have to work I guess :-)

Well, you can do it, but it isn’t dead simple.
The natural thing to do is to override JGoObject.handleMove. The difficulty is in figuring out exactly when and how you want to do something different from the standard behavior.
You may want to call JGoDocument.pickObjects to find all of the objects that are at a particular point. You might considering calling JGoDocument.isUnoccupied to help you to try to find an empty place in the document.

walter you mentioned the method JGoDocument.isUnoccupied what version of JGO are you referring to that I can find this method?

Walter,

Thanks for this, In the end I decided just to abort the move if the user places a node on top of another one. The actual code if anyone else is interested is below. One item I still can’t seem to stop is dropping a node ontop of a flow link. The code below does not appear to work for that. Would there be a better way to detect that type of situation ? as it would appear the isUnoccupied() method does not work as in the node situation, is there another method to handle that situation ?

Word of advice. I noticed that if I did not check explicitly for the MouseUP event then it was possible to get for the want of a better description ‘node droppings’ so some very strange behaviour if I did not check for this !

public void handleMove(JGoView view, int modifiers, int event, int spotNumber, int origX, int origY, int newX, int newY)
{
if(event == JGoView.EventMouseUp)
{
// Change 160, 60 to be your node size so that the rectangle
// area fits your actual node.
Rectangle newRect = new Rectangle(newX, newY, 160, 60);
if (getDoc().isUnoccupied(newRect, this)) {
super.handleMove(view, modifiers, event, spotNumber, origX, origY, newX, newY);
}
}
}


Yes, that’s right–JGoDocument.isUnoccupied just checks for regions where no document object returned true for the JGoDocument.isAvoidable predicate. By default isAvoidable is false for links. That’s what most people want to do.
Checking for links crossing a rectangular region is more difficult to detect, since there isn’t a general shape intersection predicate for JGoObjects. However it is easy to iterate over the document objects to see if there are any for which the getBoundingRect() intersects a rectangle.
Instead of using “160, 60” as hard coded sizes, you can use the getWidth() and getHeight() of the object.

Walter,

Thanks for that I’ll give that a try

Wayne