Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

2.5 Draw2D Events

We would like the user to be able to drag the figures around the diagram. To accomplish this, we create a new Draw2D event listener to process mouse events, move figures, and update the diagram. Start by creating a new FigureMover class that implements the Draw2D MouseListener and MouseMotionListener interfaces. Add a constructor that hooks the listener to the specified figure and a concrete method that does nothing for each method specified in the interfaces.

package com.qualityeclipse.genealogy.listener;

import org.eclipse.draw2d.*;
import org.eclipse.draw2d.geometry.*;

public class FigureMover
   implements MouseListener, MouseMotionListener
{
   private final IFigure figure;

   public FigureMover(IFigure figure) {
      this.figure = figure;
      figure.addMouseListener(this);
      figure.addMouseMotionListener(this);
   }

   ... stub methods here ...
}

When the user presses the mouse button, we need to record the location where the mouse down occurred by adding a field and implementing the mousePressed(...) method. In addition, this method must mark the event as "consumed" so that the Draw2D event dispatcher will send all mouse events to this listener's figure until the mouse button is released.

private Point location;
public void mousePressed(MouseEvent event) {
   location = event.getLocation();
   event.consume();
}

As the user moves the mouse around with the mouse button held down, we need to move the figure in the same direction and distance. The mouseDragged(...) method calculates the distance moved, moves the figure, and marks the event as consumed. To move the figure, we must update both the figure's bounding box and the layout information. Both the figure's original location and new location must be marked as "dirty" so that the update manager will redraw the diagram appropriately. The getBounds() method returns the actual rectangle object used by the figure to remember its bounds, so we cannot modify that object. Instead, we call getCopy() before calling translate(...) to prevent any undesired side effects.

public void mouseDragged(MouseEvent event) {
   if (location == null)
      return;
   Point newLocation = event.getLocation();
   if (newLocation == null)
      return;
   Dimension offset = newLocation.getDifference(location);
   if (offset.width == 0 && offset.height == 0)
      return;
   location = newLocation;

   UpdateManager updateMgr = figure.getUpdateManager();
   LayoutManager layoutMgr = figure.getParent().getLayoutManager();
   Rectangle bounds = figure.getBounds();
   updateMgr.addDirtyRegion(figure.getParent(), bounds);
   bounds = bounds.getCopy().translate(offset.width, offset.height);
   layoutMgr.setConstraint(figure, bounds);
   figure.translate(offset.width, offset.height);
   updateMgr.addDirtyRegion(figure.getParent(), bounds);
   event.consume();
}

When the mouse button is released, we clear the cached location and mark the event as consumed.

public void mouseReleased(MouseEvent event) {
   if (location == null)
      return;
   location = null;
   event.consume();
}

Finally, modify the GenealogyView class to import the FigureMover class and hook the listeners to each person figure and the marriage figure by modifying the createPerson(...) and createMarriage() methods. Once these steps are complete, the figures can be dragged around the window (see Figure 2–7). For more information on how the framework determines where figures are for the mouse events, see Chapter 7 on page 91.

private RectangleFigure createPersonFigure(String name) {

   ... existing code here ...

   new FigureMover(rectangleFigure);
   return rectangleFigure;
}

private PolygonShape createMarriageFigure() {

   ... existing code here ...

   new FigureMover(polygonShape);
   return polygonShape;
}
Figure 2–7

Figure 2–7 Genealogy view showing dragging of figures.

Implementing listeners such as these is useful when providing user interaction with pure Draw2D diagrams, but much of this functionality, such as dragging figures around a diagram, is already provided by the higher-level GEF framework.

  • + Share This
  • 🔖 Save To Your Account