How to Start Label Editing on Mouse Double Click

Questions & Answers

Summary

This article presents a custom ViewMode implementation to invoke the built-in label editor provided by class Graph2DView on mouse double clicks.
For a better user experience, please go to the integrated documentation viewer to read this article.

Description

The default yFiles view implementation, class Graph2DView provides a built-in label editor that can be invoked using the Graph2DView.openLabelEditor methods.

By default, there is no special user gesture to open the label editor, unless the Graph2DViewActions class is installed with a view. Then, the F2 function key is set up to serve this purpose.
In order to set up mouse double clicks, for example, as the user gesture to open the label editor you can use the following ViewMode implementation.

/**
 * A simple ViewMode implementation that enables mouse double clicks
 * as the trigger condition to open a label editor.
 */
public class DoubleClickLabelEditMode extends ViewMode {
  public void mouseClicked(MouseEvent me) {
    if (me.getClickCount() == 2) {
      HitInfo hitInfo = getHitInfo(me);
      YLabel label = null;
      // Node labels.
      if (hitInfo.hasHitNodeLabels()) {
        label = hitInfo.getHitNodeLabel();
      }
      // Edge labels.
      else if (hitInfo.hasHitEdgeLabels()) {
        label = hitInfo.getHitEdgeLabel();
      }
      // It is easier to hit a node than its node label (especially 
      // if the latter lies completely inside the node's area).
      else if (hitInfo.hasHitNodes()) {
        Node n = hitInfo.getHitNode();
        NodeRealizer nr = view.getGraph2D().getRealizer(n);
        if (nr.labelCount() > 0) {
          label = nr.getLabel();
        }
      }
      if (label != null) {
        view.openLabelEditor(label, label.getBox().getX(), label.getBox().getY(), null, true);
      }
    }
  }
}

This ViewMode can be registered with a view like so:

// 'view' is of type y.view.Graph2DView.

view.addViewMode(new DoubleClickLabelEditMode());

Other user gestures that you might want to use can be easily set up using the above ViewMode implementation as a starting point. For example, to use a single click in conjunction with the default modifier key (SHIFT):

// Using SHIFT + mouse single click as the "trigger" condition.
if (me.getClickCount() == 1 && isModifierPressed(me)) {
  // ...
}

Carefully observe, however, that a number of user gestures already have a meaning when you are editing a graph in the yFiles view. For example:

  • mouse single click is already used to select graph elements (nodes, edges, labels)
  • SHIFT + mouse single click is already used to add, respectively remove graph elements to/from an already existing selection of similar graph elements

Categories this article belongs to:
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs
yFiles for Java > yFiles Viewer > Displaying and Editing Graphs > User Interaction
Applies to:
yFiles for Java 2: 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:
HitInfo - double - click - node - edge - label - editing - editor - ViewMode - openLabelEditor - open - user - gesture