Example #1
0
  /**
   * Provides default behavior for mouse clicks.
   *
   * <p>If graphics classes have been registered using {@linkplain #registerMouseClickClasses()} for
   * left-clicks and {@linkplain #registerMapMenuClasses()} for right-clicks, it is checked whether
   * one of these classes have been clicked.
   *
   * @param evt the mouse event
   */
  @Override
  public boolean mouseClicked(MouseEvent evt) {
    if (evt.getButton() == MouseEvent.BUTTON1 && !mouseClickClasses.isEmpty()) {

      OMGraphic clickedGraphics =
          getSelectedGraphic(
              mouseClickGraphics,
              evt,
              mouseClickClasses.toArray(new Class<?>[mouseClickClasses.size()]));
      if (clickedGraphics != null) {
        // Clean up any info panels
        hideInfoPanels();
        if (HIDE_GLASS_PANEL) {
          getGlassPanel().setVisible(false);
        }

        // Allow custom handling of right-clicks by sub-classes
        handleMouseClick(clickedGraphics, evt);
        return true;

      } else {
        handleMouseClick(null, evt);
      }

    } else if (evt.getButton() == MouseEvent.BUTTON3 && !mapMenuClasses.isEmpty()) {
      OMGraphic clickedGraphics =
          getSelectedGraphic(
              mapMenuGraphics, evt, mapMenuClasses.toArray(new Class<?>[mapMenuClasses.size()]));
      if (clickedGraphics != null) {
        // Clean up any info panels
        hideInfoPanels();
        if (HIDE_GLASS_PANEL) {
          getGlassPanel().setVisible(false);
        }

        // Allow custom map menu initialization by sub-classes
        initMapMenu(clickedGraphics, evt);

        // Display the menu
        int yOffset = 2;
        if (mainFrame.getHeight() < evt.getYOnScreen() + mapMenu.getHeight()) {
          yOffset = mapMenu.getHeight();
        }
        mapMenu.show(this, evt.getX() - 2, evt.getY() - yOffset);
        return true;
      }
    }

    return false;
  }
Example #2
0
  /**
   * Default implementation of the mouseMoved method that opens {@linkplain InfoPanel} based on the
   * graphics closest to the mouse location.
   *
   * <p>In order to use this mechanism, first register the info panels using the {@code
   * registerInfoPanel()} method.
   *
   * @param evt the mouse event
   * @return if the event was handled
   */
  @Override
  public boolean mouseMoved(MouseEvent evt) {

    if (!isVisible() || mapMenu == null || mapMenu.isVisible()) {
      return false;
    }

    if (!infoPanels.isEmpty()) {
      OMGraphic newClosest =
          getSelectedGraphic(infoPanelsGraphics, evt, infoPanels.getGraphicsList());

      if (newClosest != null && newClosest.isVisible()) {

        InfoPanel infoPanel = infoPanels.getInfoPanel(newClosest.getClass());

        // Register the distance between the mouse position and the closest graphics
        infoPanel.setMouseDist(newClosest.distance(evt.getPoint().getX(), evt.getPoint().getY()));

        if (newClosest != closest) {
          closest = newClosest;
          Point containerPoint = convertPoint(evt.getPoint());

          infoPanel.setPos((int) containerPoint.getX(), (int) containerPoint.getY() - 10);

          // Allow custom initialization by sub-classes
          if (initInfoPanel(infoPanel, newClosest, evt, containerPoint)) {
            infoPanel.setVisible(true);
            getGlassPanel().setVisible(true);
          }

          // Hides all but the info panel closest to the mouse point
          checkInfoPanelVisiblity();
        }

        return true;
      } else if (newClosest == null) {
        closest = null;
        hideInfoPanels();
      }
    }

    return false;
  }