Customizing Mouse Events in EditMode

Tips & Tricks

Summary

This article explains how to customize EditMode to give it focus support.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

EditMode is the default edit mode for Graph2DView. It provides several features like creating nodes, edges and bends and selecting graph elements. Therefore it makes use of other minor modes that have specialized responsibilities, for example CreateEdgeMode or handles the events by itself.
It also sets the default behavior for mouse events like mousePressedLeft(double, double) or mouseReleasedLeft(double, double). Sometimes you want to change these default settings and customize EditMode's behavior. This can easily be done by overwriting some of EditMode's methods.

In the following example we'll create our own custom EditMode that will nearly act similar to the default EditMode. The only difference is, it will only act on mouse events if the view or canvas, in which all graph components are represented, already has the focus. Otherwise this canvas will get the focus and nothing else will happen. This behaviour for example prevents nodes from being created when clicking into the canvas just for gaining the focus.

What the code does:

  • Overwrites demo.view.ViewActionDemo's method createEditMode() to create a custom EditMode.

  • The new EditMode only handles mousePressed and mouseDragged events if the view or the canvas has the focus.
package demo.view;

import java.awt.EventQueue;
import java.awt.event.MouseEvent;

import y.view.EditMode;

/**
 * Shows how to add focus support to EditMode. This demo uses a customized
 * EditMode that blocks the delegation of mouse events in case the graph view
 * has no focus. Additionally, it will grab the focus upon releasing the mouse on the view. 
 */
public class FocusDemo extends ViewActionDemo {
  public FocusDemo() {}

  protected EditMode createEditMode() {
    EditMode mode = new EditMode() {
      public void mousePressed(MouseEvent ev) {
        if (view.hasFocus() || view.getCanvasComponent().hasFocus()) {
          super.mousePressed(ev);
        }
      }

      public void mouseDragged(MouseEvent ev) {
        if (view.hasFocus() || view.getCanvasComponent().hasFocus()) {
          super.mouseDragged(ev);
        }
      }

      public void mouseReleased(MouseEvent ev) {
        if (view.getCanvasComponent().hasFocus()) {
          super.mouseReleased(ev);
        }
        else {
          view.getCanvasComponent().requestFocus();
        }
      }
    };
    return mode;
  }

  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      public void run() {
        initLnF();
        new FocusDemo().start("Focus Demo");
      }
    });
  }
}
To compile and run the demo, copy the attached file to the src/demo/view directory of your yFiles installation.

Resources

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > User Interaction
Applies to:
yFiles for Java 2: 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, 2.10, 2.11, 2.12, 2.13, 2.14, 2.15, 2.16, 2.17, 2.18
Keywords:
EditMode - focus - customize - mouse - view - mode - mousePressed - mouseReleased - click