Class MapTextItem

All Implemented Interfaces:
IFeedbackProvider, IDrawable, ITextEditable, EventListener

public class MapTextItem extends AMapItem implements ITextEditable
A map-native text label whose anchor is stored as a geographic coordinate (longitude/latitude in radians).

Distinction from TextItem

TextItem anchors its bounding box to a world-space (projection-space) point. When the map projection changes or the view recenters, the text box drifts because the anchor is in a coordinate system that moves relative to the Earth's surface. MapTextItem stores the anchor as a geographic point; the anchor is re-projected on every draw so the label stays above the same geographic feature regardless of the active projection.

Rendering model

The text box is always axis-aligned in screen space. Only the anchor moves with the projection; the box itself is a screen-space rectangle computed from font metrics on each draw, exactly as in TextItem. This means rotation of the text box is not supported (and is meaningless for a geographic label).

Drag

Drag converts both the start and current mouse positions independently to geographic coordinates and moves the anchor by the difference. This is exact at all latitudes (no raw lon/lat approximation).

Resize

Not applicable — the box size is determined by the font and text content. ResizePolicy.SCALEONLY is set so that shift+drag scales rather than attempting a shape-preserving reshape.

Rotation

Not supported. Geographic labels are conventionally upright; rotating a text label relative to the map surface has no standard geographic meaning.

Focus

AItem._focus holds the geographic anchor (x = longitude, y = latitude in radians). AMapItem.getFocusPoint(edu.cnu.mdi.container.IContainer) projects it to device pixels for the selection indicator. The bounding box is then built screen-space around that pixel anchor, matching TextItem's screen-space round-trip.

  • Constructor Details

    • MapTextItem

      public MapTextItem(Layer layer, Point2D.Double location, Font font, String text, Color lineColor, Color fillColor, Color textColor)
      Creates a map-native text label.
      Parameters:
      layer - the layer; must not be null
      location - geographic anchor in radians (x = longitude, y = latitude)
      font - font to use; if null the default font is used
      text - text to display; must not be null or empty
      lineColor - border color; null for no border
      fillColor - background color; null for transparent background
      textColor - text foreground color; null defaults to black
  • Method Details

    • drawItem

      public void drawItem(Graphics2D g2, IContainer container)
      Projects the geographic anchor to device pixels and draws the text box (background fill, border, and text) in screen space around that pixel.

      Returns immediately if the anchor is off-map (non-finite projection). No path-based geometry is maintained; the bounding box is rebuilt from font metrics on every draw.

      Specified by:
      drawItem in class AItem
      Parameters:
      g2 - the graphics context
      container - the rendering container; must be a MapContainer
    • shouldDraw

      public boolean shouldDraw(Graphics2D g2, IContainer container)
      Return true if this item should be drawn on the current frame.

      This is a secondary visibility check beyond the simple AItem._visible flag. A typical implementation checks whether the item's screen bounds intersect the current clip rectangle.

      Returns false if the anchor is unplaced or the bounding rectangle does not intersect the visible component area.

      Specified by:
      shouldDraw in class AItem
      Parameters:
      g2 - the graphics context (may be used to read clip bounds)
      container - the container being rendered
      Returns:
      true if AItem.drawItem(java.awt.Graphics2D, edu.cnu.mdi.container.IContainer) should be called
    • getBounds

      public Rectangle getBounds(IContainer container)
      Returns the screen-space bounding rectangle of the text box.

      Computed from font metrics centred on the projected anchor. Returns null if the anchor is off-map.

      Overrides:
      getBounds in class AItem
      Parameters:
      container - the rendering container
      Returns:
      bounding rectangle in device coordinates, or null
    • getWorldBounds

      public Rectangle2D.Double getWorldBounds()
      Return the world-coordinate bounding rectangle of this item.

      Returns null — a geographic label has no world-space extent.

      Specified by:
      getWorldBounds in class AItem
      Returns:
      the world-coordinate bounding rectangle
    • startModification

      public void startModification()
      Classify and begin a drag, resize, or rotate modification.

      Priority: rotate (if start point is in rotation handle) → resize (if start point is in a selection handle) → drag (otherwise).

      This method is a no-op if AItem._modification is null.

      Records the geographic start anchor and its screen projection for subsequent drag computation.

      Overrides:
      startModification in class AItem
    • modify

      public void modify()
      Update this item's geometry in response to a mouse movement during an active modification.

      Called repeatedly while the user is dragging, resizing, or rotating. The current mouse position is available through AItem._modification.

      DRAG: independently inverse-projects the start and current mouse positions to geographic coordinates and shifts the anchor by the difference. This is exact at all latitudes. RESIZE / ROTATE: silently ignored.

      Specified by:
      modify in class AItem
    • translateWorld

      public void translateWorld(double dx, double dy)
      Translate this item by (dx, dy) in world coordinates.

      Moves the geographic anchor by raw lon/lat offsets. See MapLineItem for the high-latitude caveat. A no-op if the anchor is unplaced.

      Specified by:
      translateWorld in class AItem
      Parameters:
      dx - the world-coordinate offset along the X axis
      dy - the world-coordinate offset along the Y axis
    • setFocus

      public void setFocus(Point2D.Double location)
      Set the world-coordinate focus of this item.

      Stores the geographic location directly. NaN coordinates represent an unplaced label.

      Overrides:
      setFocus in class AItem
      Parameters:
      location - the new focus; may be null to clear
    • edit

      public void edit()
      Opens the text-edit dialog and applies the result.

      Rebuilding the path (as TextItem does for rotation) is unnecessary here because MapTextItem has no stored path.

    • getFeedbackStrings

      public void getFeedbackStrings(IContainer container, Point pp, Point2D.Double wp, List<String> feedbackStrings)
      Populate the feedback string list with information about this item. The default implementation is a no-op.

      Reports the text content and geographic anchor when the cursor is over the label.

      Specified by:
      getFeedbackStrings in interface IFeedbackProvider
      Overrides:
      getFeedbackStrings in class AItem
      Parameters:
      container - the container rendering this item
      pp - the current mouse position in screen coordinates
      wp - the corresponding world-coordinate position
      feedbackStrings - the list to add strings to
    • getAlignment

      public int getAlignment()
      Returns:
      the current text alignment
    • setAlignment

      public void setAlignment(int align)
      Parameters:
      align - text alignment constant from SwingConstants
    • getFont

      public Font getFont()
      Description copied from interface: ITextEditable
      Returns the current display font.
      Specified by:
      getFont in interface ITextEditable
      Returns:
      the current font
    • setFont

      public void setFont(Font font)
      Sets the display font.
      Specified by:
      setFont in interface ITextEditable
      Parameters:
      font - the font to use; if null the default font is restored
    • getText

      public String getText()
      Returns the full text content as a single string with embedded newlines.
      Specified by:
      getText in interface ITextEditable
      Returns:
      the text; never null
    • setText

      public void setText(String text)
      Sets the text content, replacing special characters via UnicodeUtils.specialCharReplace(java.lang.String).
      Specified by:
      setText in interface ITextEditable
      Parameters:
      text - the new text; null is treated as an empty string