I am trying to disable movement of the globe on mouse click in World Wind. I expected to be able to do:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
ww.addMouseMotionListener(new MyMouseMotionListener());
}
where MyMouseMotionListener
consumes all of the mouse events. This is not working so instead I have to do:
void disableGlobeDrag(WorldWindowGLCanvas ww) {
for(MouseMotionListener l : ww.getMouseMotionListeners()) {
if(l.getClass().toString().equals("class gov.nasa.worldwind.awt.AWTInputHandler")) {
ww.removeMouseMotionListener(l);
}
}
}
Is it expected that consumed mouse events should still reach the gov.nasa.worldwind.awt.AWTInputHandler
listener?
Update: WorldWindowGLCanvas
is just calling addMouseMotionListener()
on java.awt.Component
so apparently I don't understand how consuming events works.
Update 2: despite sharing interfaces with Swing, calling WorldWindowGLCanvas.removeMouseMotionListener()
with AWTInputHandler
as the argument will prevent all other MouseMotionListener
s from receiving events. The add and remove methods on AWTInputHandler
should be used instead.
Why it's not working:
As discussed here, many event sources maintain an
EventListenerList
. The prescribed scheme allows an arbitrary number of listeners to be added or removed. This related example lists all instances ofDocumentListener
registered to text component'sDocument
. No one listener preempts another.What you might do:
Given an instance of a
WorldWindowGLCanvas
, you might look at the array returned bygetMouseMotionListeners()
and invokeremoveMouseMotionListener()
as warranted.After struggling with a way to do this and stumbling across this solution, I believe I've come up with the "correct" solution. Removing the mouse motion listener completely does technically work, but it breaks other functionality that might be useful (KML tree node selecton, on screen view controls).
Create a subclass an input handler to remove the "move to" functionality
Specify your new input handler in your worldwind.xml configuration file
Using this method, all other mouse interactions that take place on the canvas will still work correctly, but the "single click to pan" functionality is removed.
If you're curious about what other behaviors are specified that you could potentially override, you can look at gov.nasa.worldwind.awt.BasicViewInputHandler
Unfortunately removing the
MouseMotionListener
as @trashgod suggested does not work since there is some World Wind specific behavior happening: removinggov.nasa.worldwind.awt.AWTInputHandler
causes otherMouseMotionListener
s to stop receiving event notifications.To disable globe dragging and still receive events in another
MouseMotionListener
the following steps were necessary:Get a reference to World Wind's
AWTInputHandler
:Create a
MouseMotionListener
which consumes events:Add the mouse motion listener to the
AWTInputHandler
:That said, I have no idea why
WorldWindowGLCanvas
is usingComponent.addMouseMotionListener()
rather thanAWTInputHandler.addMouseMotionListener()
.