/** The mouse was dragged - either draw a marquee or move some classes. */
  public void mouseDragged(MouseEvent evt) {
    if (isButtonOne(evt)) {
      if (marquee.isActive()) {
        Rectangle oldRect = marquee.getRectangle();
        marquee.move(evt.getX(), evt.getY());
        Rectangle newRect = (Rectangle) marquee.getRectangle().clone();
        if (oldRect != null) {
          newRect.add(oldRect);
        }
        newRect.width++;
        newRect.height++;
        graphEditor.repaint(newRect);
      } else if (rubberBand != null) {
        rubberBand.setEnd(evt.getX(), evt.getY());
        graphEditor.repaint();
      } else {
        if (!selection.isEmpty()) {
          int deltaX = snapToGrid(evt.getX() - dragStartX);
          int deltaY = snapToGrid(evt.getY() - dragStartY);

          if (resizing) {
            selection.resize(deltaX, deltaY);
          } else if (moving) {
            selection.move(deltaX, deltaY);
          }
        }
        graphEditor.repaint();
      }
    }
  }
 /** A key was released. Check whether a key-based move or resize operation has ended. */
 public void keyReleased(KeyEvent evt) {
   if (moving && (!evt.isShiftDown())) { // key-based moving stopped
     selection.moveStopped();
     moving = false;
   } else if (resizing && (!evt.isControlDown())) { // key-based moving stopped
     selection.moveStopped();
     resizing = false;
   }
   graphEditor.repaint();
 }
  /** 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();
    }
  }
  /** A key was pressed in the graph editor. */
  public void keyPressed(KeyEvent evt) {
    boolean handled = true; // assume for a start that we are handling the
    // key here

    if (isArrowKey(evt)) {
      if (evt.isControlDown()) { // resizing
        if (!resizing) startKeyboardResize();
        setKeyDelta(evt);
        selection.resize(keyDeltaX, keyDeltaY);
      } else if (evt.isShiftDown()) { // moving targets
        if (!moving) startKeyboardMove();
        setKeyDelta(evt);
        selection.move(keyDeltaX, keyDeltaY);
      } else { // navigate the diagram
        navigate(evt);
      }
    } else if (isPlusOrMinusKey(evt)) {
      resizeWithFixedRatio(evt);
    }

    // dependency selection
    else if (evt.getKeyCode() == KeyEvent.VK_PAGE_UP || evt.getKeyCode() == KeyEvent.VK_PAGE_DOWN) {
      selectDependency(evt);
    }

    // post context menu
    else if (evt.getKeyCode() == KeyEvent.VK_SPACE || evt.getKeyCode() == KeyEvent.VK_ENTER) {
      postMenu();
    }

    // 'A' (with any or no modifiers) selects all
    else if (evt.getKeyCode() == KeyEvent.VK_A) {
      selectAll();
    }

    // Escape removes selections
    else if (evt.getKeyCode() == KeyEvent.VK_ESCAPE) {
      if (moving || resizing) {
        endMove();
      }
      clearSelection();
    } else {
      handled = false;
    }

    if (handled) evt.consume();

    graphEditor.repaint();
  }
  /** 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;
        }
      }
    }
  }
 /**
  * Create the controller for a given graph editor.
  *
  * @param graphEditor
  * @param graph
  */
 public SelectionController(GraphEditor graphEditor) {
   this.graphEditor = graphEditor;
   this.graph = graphEditor.getGraph();
   marquee = new Marquee(graph);
   selection = new SelectionSet();
 }
 /**
  * Post the context menu on the diagram at the specified location
  *
  * @param x
  * @param y
  */
 private void postMenu(int x, int y) {
   graphEditor.popupMenu(x, y);
 }