Select to view content in your preferred language

How to deactivate the mouse motion and key events on the JMap?

2952
2
10-31-2011 12:51 PM
Jan-Tschada
Esri Contributor
Is it possible to deactivate the mouse motion and key events on the JMap by simple setting a flag? For instance, if someone tries to develop a custom tool which creates graphics on the map by clicking on it, the map still listen and reacts to the mouse motion events. If the user click on the map and drags the mouse a little bit the map extent changes unintentually. This is not a nice user experience.

Right now, we are removing the mouse, mouse motion, mouse wheel listener and the action map from the JMap instance. Create for each instance a decorator (offering setActivated) and add these decorated listeners back to the JMap instance. If the custom tool is active the decorators are deactivated and the user can create graphics by clicking on the map.

E.g. MouseMotionListenerDecorator
public class MouseMotionListenerDecorator implements MouseMotionListener {

 private final MouseMotionListener mouseMotionListener;
 private boolean activated = true;

 public MouseMotionListenerDecorator(MouseMotionListener mouseMotionListener) {
  this.mouseMotionListener = mouseMotionListener;
 }

 public boolean isActivated() {
  return activated;
 }

 public void setActivated(boolean activated) {
  this.activated = activated;
 }

 @Override
 public void mouseDragged(MouseEvent e) {
  if (!isActivated())
   return;

  mouseMotionListener.mouseDragged(e);
 }

 @Override
 public void mouseMoved(MouseEvent e) {
  if (!isActivated())
   return;

  mouseMotionListener.mouseMoved(e);
 }

}


Is there any better way by using the API?

Thank you
Product Manager
Developers and Location Services
Germany and Switzerland
0 Kudos
2 Replies
Jan-Tschada
Esri Contributor
The easiest thing as far as I can judge it, is using a map overlay and only delegating the mouse events to the JComponent if no current tool is set.

  // Create the map and add a map overlay
  // which delegates to the current tool
  map = new JMap();
  map.addMapOverlay(new MapOverlay() {
   
   @Override
   public void onMouseClicked(MouseEvent event) {
    if (currentTool == null)
     super.onMouseClicked(event);
    else
     currentTool.onMouseClicked(event);
   }
   
   @Override
   public void onMouseDragged(MouseEvent event) {
    if (currentTool == null)
     super.onMouseDragged(event);
    else
     currentTool.onMouseDragged(event);
   }
   
   @Override
   public void onMouseEntered(MouseEvent event) {
    if (currentTool == null)
     super.onMouseEntered(event);
    else
     currentTool.onMouseEntered(event);
   }
   
   @Override
   public void onMouseExited(MouseEvent event) {
    if (currentTool == null)
     super.onMouseExited(event);
    else
     currentTool.onMouseExited(event);
   }
   
   @Override
   public void onMouseMoved(MouseEvent event) {
    if (currentTool == null)
     super.onMouseMoved(event);
    else
     currentTool.onMouseMoved(event);
   }
   
   @Override
   public void onMousePressed(MouseEvent event) {
    if (currentTool == null)
     super.onMousePressed(event);
    else
     currentTool.onMousePressed(event);
   }
   
   @Override
   public void onMouseReleased(MouseEvent event) {
    if (currentTool == null)
     super.onMouseReleased(event);
    else
     currentTool.onMouseReleased(event);
   }
   
   @Override
   public void onMouseWheelMoved(MouseWheelEvent event) {
    if (currentTool == null)
     super.onMouseWheelMoved(event);
    else
     currentTool.onMouseWheelMoved(event);
   }
  });
Product Manager
Developers and Location Services
Germany and Switzerland
0 Kudos
RalfGottschalk
Esri Contributor
Hi Jan,

The Map Overlay would be the way to achieve this functionality.  You would associate a Map Overlay with each, or groups, of your tools.  Check out the source code in the Toolkits for examples.
0 Kudos