Mouse Panning

Hello,

I need to implement mouse based panning support to my JGoView (the ability to drag the canvas around). This seems like a fairly common application feature— does support for this exist in JGo, or in any example code?

-Justin

This requires defining an additional mouse state for the JGoView.
Basically, in your subclass of JGoView, define:
public static final int MouseStatePanning = MouseStateLast+1; private Point myLastViewPoint = null;
Override JGoView methods:
public boolean doMouseDown(int modifiers, Point dc, Point vc) {
if (getState() == MouseStatePanning) {
myLastViewPoint = new Point(vc.x, vc.y);
return true;
} else {
return super.doMouseDown(modifiers, dc, vc);
}
}
public boolean doMouseMove(int modifiers, Point dc, Point vc) {
if (getState() == MouseStatePanning) {
if (myLastViewPoint != null) {
Point pos = getViewPosition();
Dimension delta = new Dimension(myLastViewPoint.x-vc.x, myLastViewPoint.y-vc.y);
convertViewToDoc(delta);
myLastViewPoint.x = vc.x;
myLastViewPoint.y = vc.y;
setViewPosition(pos.x+delta.width, pos.y+delta.height);
}
return true;
} else {
return super.doMouseMove(modifiers, dc, vc);
}
}
public boolean doMouseUp(int modifiers, Point dc, Point vc) {
if (getState() == MouseStatePanning) {
myLastViewPoint = null;
return false;
} else {
return super.doMouseUp(modifiers, dc, vc);
}
}
public void doCancelMouse() {
if (getState() == MouseStatePanning) {
setState(MouseStateNone);
} else {
super.doCancelMouse();
}
}
Then your command to put your view into manual panning mode is just a call to
JGoView.setState(MouseStatePanning). The actual panning doesn't start until
a mouse down has happened, since doMouseMove is a no-op while myLastViewPoint is null.
You'll probably want to call setCursor in the appropriate places, and maybe
other fanciness.

Thanks-- this works great. I added some code to trigger the panning mode with the shift key and change the cursor:

    @Override
    public void keyPressed(KeyEvent evt) {
        if (evt.isShiftDown()) {
            if (getState() != MouseStatePanning) {
                setState(MouseStatePanning);
                setCursor(Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR));
            }
        }
        super.keyPressed(evt);
    }

    @Override
    public void keyReleased(KeyEvent evt) {
        if (!evt.isShiftDown()) {
            if (getState() == MouseStatePanning) {
                setState(MouseStateNone);
                setCursor(getDefaultCursor());
            }
        }
        super.keyReleased(evt);
    }

However, during the actual dragging the cursor changes temporarily to the selection cursor. I’m having trouble determining what code in JGoView.java is causing this cursor change, as setCursor / setCursorType is only called in a few places…

Perhaps you need to call JGoView.setDefaultCursor.

Did anyone ever get a fix for this?

I have the same problem right now with the cursor changing to the selection cursor when the mouse moves, regardless of what I set the cursor to.
I have tried view.setCursor(), view.getCanvas().setCursor, and view.setDefaultCursor()
Many thanks for any input!

The cursor is being changed by the drag/drop mechanism. You can avoid that by overriding JGoView.initializeDragDropHandling() as follows:

public void initializeDragDropHandling() {
super.initializeDragDropHandling();
setDragDropEnabled(false);
}
Of course this will disable dragging from one JGoView to another, but this may not be an issue for you.

Thanks, Scott.

This does work, but creates another problem for me, since I am trying to use right-click drag to pan with the mouse. Now, I get my cursor correct, but I always get my popup menu after dragging.
I'll pursue it more later to see about getting it all fixed up :).
Many thanks for the tip!
Cheers,
Brian