private Vertex findSingleVertex() {
    Vertex vertex = selection.getAnyVertex();

    // if there is no selection we select an existing vertex
    if (vertex == null) {
      vertex = (Vertex) graph.getVertices().next();
    }
    return vertex;
  }
  /** A menu popup trigger has been detected. Handle it. */
  public void handlePopupTrigger(MouseEvent evt) {
    int clickX = evt.getX();
    int clickY = evt.getY();

    SelectableGraphElement clickedElement = graph.findGraphElement(clickX, clickY);
    if (clickedElement != null) {
      selection.selectOnly(clickedElement);
      postMenu(clickedElement, clickX, clickY);
    } else {
      postMenu(clickX, clickY);
    }
  }
  /** A mouse-pressed event. Analyse what we should do with it. */
  public void mousePressed(MouseEvent evt) {
    graphEditor.requestFocus();
    int clickX = evt.getX();
    int clickY = evt.getY();

    SelectableGraphElement clickedElement = graph.findGraphElement(clickX, clickY);
    notifyPackage(clickedElement);

    if (clickedElement == null) { // nothing hit
      if (!isMultiselectionKeyDown(evt)) {
        selection.clear();
      }
      if (isButtonOne(evt)) marquee.start(clickX, clickY);
    } else if (isButtonOne(evt)) { // clicked on something
      if (isMultiselectionKeyDown(evt)) {
        // a class was clicked, while multiselectionKey was down.
        if (clickedElement.isSelected()) {
          selection.remove(clickedElement);
        } else {
          selection.add(clickedElement);
        }
      } else {
        // a class was clicked without multiselection
        if (!clickedElement.isSelected()) {
          selection.selectOnly(clickedElement);
        }
      }

      if (isDrawingDependency()) {
        if (clickedElement instanceof Target)
          rubberBand = new RubberBand(clickX, clickY, clickX, clickY);
      } else {
        dragStartX = clickX;
        dragStartY = clickY;

        if (clickedElement.isHandle(clickX, clickY)) {
          resizing = true;
        } else {
          moving = true;
        }
      }
    }
  }
  /** The mouse was released. */
  public void mouseReleased(MouseEvent evt) {
    if (isDrawingDependency()) {
      SelectableGraphElement selectedElement = graph.findGraphElement(evt.getX(), evt.getY());
      notifyPackage(selectedElement);
      graphEditor.repaint();
    }
    rubberBand = null;

    SelectionSet newSelection = marquee.stop(); // may or may not have had a marquee...
    if (newSelection != null) {
      selection.addAll(newSelection);
      graphEditor.repaint();
    }

    if (moving || resizing) {
      endMove();
      graphEditor.revalidate();
      graphEditor.repaint();
    }
  }
 /** Select all graph vertices. */
 private void selectAll() {
   for (Iterator<? extends Vertex> i = graph.getVertices(); i.hasNext(); ) {
     selection.add(i.next());
   }
 }