Class AbstractMapPane

All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, EventListener, Accessible, MapBoundsListener, MapLayerListListener, MapPane, RenderingExecutorListener
Direct Known Subclasses:
JLayeredMapPane, JMapPane

public abstract class AbstractMapPane extends JPanel implements MapPane, RenderingExecutorListener, MapLayerListListener, MapBoundsListener
Base class for Swing map panes. It extends Swing's JPanel class and handles window sizing and repainting as well as redirecting mouse events. It also provides basic implementations of all interface methods. Sub-classes must implement drawLayers(boolean) and override JComponent.paintComponent(java.awt.Graphics).
Since:
8.0
Author:
Michael Bedward
See Also:
  • Field Details

    • DEFAULT_PAINT_DELAY

      public static final int DEFAULT_PAINT_DELAY
      Default delay (500 milliseconds) before the map will be redrawn when resizing the pane or moving the displayed image. This avoids flickering and redundant rendering.
      See Also:
    • DEFAULT_BACKGROUND_COLOR

      public static final Color DEFAULT_BACKGROUND_COLOR
      Default background color (white).
    • paneTaskExecutor

      protected final ScheduledExecutorService paneTaskExecutor
    • resizedFuture

      protected Future<?> resizedFuture
    • paintDelay

      protected int paintDelay
    • acceptRepaintRequests

      protected final AtomicBoolean acceptRepaintRequests
    • baseImageMoved

      protected final AtomicBoolean baseImageMoved
    • imageMovedFuture

      protected Future<?> imageMovedFuture
    • imageOrigin

      protected final Point imageOrigin
    • drawingLock

      protected final Lock drawingLock
    • paramsLock

      protected final ReadWriteLock paramsLock
    • listeners

      protected final Set<MapPaneListener> listeners
    • dragBox

      protected final MouseDragBox dragBox
    • pendingDisplayArea

      protected ReferencedEnvelope pendingDisplayArea
    • fullExtent

      protected ReferencedEnvelope fullExtent
    • mapContent

      protected MapContent mapContent
    • renderingExecutor

      protected RenderingExecutor renderingExecutor
    • keyHandler

      protected KeyListener keyHandler
    • mouseEventDispatcher

      protected MapMouseEventDispatcher mouseEventDispatcher
    • labelCache

      protected LabelCache labelCache
    • clearLabelCache

      protected AtomicBoolean clearLabelCache
    • currentCursorTool

      protected CursorTool currentCursorTool
  • Constructor Details

  • Method Details

    • drawLayers

      protected abstract void drawLayers(boolean recreate)
      Draws layers into one or more images which will then be displayed by the map pane.
    • getRenderingExecutor

      public RenderingExecutor getRenderingExecutor()
      Gets the rendering executor, creating a default one if necessary.
      Returns:
      the rendering executor
    • getMouseEventDispatcher

      public MapMouseEventDispatcher getMouseEventDispatcher()
      Gets the current mouse event dispatcher which is responsible for converting each input Java AWT mouse event into a MapMouseEvent and forwarding it to each MapMouseListener.
      Specified by:
      getMouseEventDispatcher in interface MapPane
      Returns:
      the current mouse event dispatcher (may be null)
    • setMouseEventDispatcher

      public void setMouseEventDispatcher(MapMouseEventDispatcher dispatcher)
      Replaces the current mouse event dispatcher. All current listeners will be removed. It is the responsibility of the client to add them to the new dispatcher if this is desired.
      Specified by:
      setMouseEventDispatcher in interface MapPane
      Parameters:
      dispatcher - the new dispatcher (may be null)
    • setRenderingExecutor

      public void setRenderingExecutor(RenderingExecutor executor)
      Sets the rendering executor. If executor is null, the default DefaultRenderingExecutor will be set on the next call to getRenderingExecutor().
      Parameters:
      executor - the rendering executor
    • getKeyHandler

      public KeyListener getKeyHandler()
      Gets the current handler for keyboard actions.
      Returns:
      current handler (may be null)
    • setKeyHandler

      public void setKeyHandler(KeyListener controller)
      Sets a handler for keyboard actions which control the map pane's display. The default handler is MapPaneKeyHandler which provides for scrolling and zooming.
      Parameters:
      controller - the new handler or null to disable key handling
    • getPaintDelay

      public long getPaintDelay()
      Gets the current paint delay interval in milliseconds. The map pane uses this delay period to avoid flickering and redundant rendering when drag-resizing the pane or panning the map image.
      Returns:
      delay in milliseconds
    • setPaintDelay

      public void setPaintDelay(int delay)
      Sets the current paint delay interval in milliseconds. The map pane uses this delay period to avoid flickering and redundant rendering when drag-resizing the pane or panning the map image.
      Parameters:
      delay - the delay in milliseconds; if <= 0 the default delay period will be set
    • setIgnoreRepaint

      public void setIgnoreRepaint(boolean ignoreRepaint)
      Specify whether the map pane should defer its normal repainting behaviour.

      Typical use:

      
       myMapPane.setRepaint(false);
      
       // do various things that would cause time-consuming
       // re-paints normally
      
       myMapPane.setRepaint(true);
       myMapPane.repaint();
       
      Overrides:
      setIgnoreRepaint in class Component
      Parameters:
      ignoreRepaint - if false, paint requests will be handled normally; if true, paint requests will be deferred.
      See Also:
    • isAcceptingRepaints

      public boolean isAcceptingRepaints()
      Query whether the map pane is currently accepting or ignoring repaint requests from other GUI components and the system.
      Returns:
      true if the pane is currently accepting repaint requests; false if it is ignoring them
      See Also:
    • onShownOrResized

      protected void onShownOrResized()
    • setForNewSize

      protected void setForNewSize()
    • moveImage

      public void moveImage(int dx, int dy)
      Moves the image(s) displayed by the map pane from the current origin (x,y) (device pixels) to (x+dx, y+dy). If this method is called when the map pane is not visible, or when the pane's visible rectangle is empty, it is ignored.
      Specified by:
      moveImage in interface MapPane
      Parameters:
      dx - the x offset in pixels
      dy - the y offset in pixels.
    • onImageMoved

      protected void onImageMoved()
    • afterImageMoved

      protected void afterImageMoved()
      Called after the base image has been dragged. Sets the new map area and transforms
    • getMapContent

      public MapContent getMapContent()
      Gets the MapConent instance containing the layers being displayed by this map pane.
      Specified by:
      getMapContent in interface MapPane
      Returns:
      the map content
    • setMapContent

      public void setMapContent(MapContent content)
      Sets the MapContent instance containing the layers to display.
      Specified by:
      setMapContent in interface MapPane
      Parameters:
      content - the map content
    • getDisplayArea

      public ReferencedEnvelope getDisplayArea()
      Gets the current display area in world coordinates. This is a short-cut for mapPane.getMapContent().getViewport().getBounds(). If a MapContent object has not yet been associated with the map pane, an empty ReferencedEnvelope is returned.
      Specified by:
      getDisplayArea in interface MapPane
      Returns:
      the display area in world coordinates
    • setDisplayArea

      public void setDisplayArea(Bounds envelope)
      Sets the area to display in world units.
      Specified by:
      setDisplayArea in interface MapPane
      Parameters:
      envelope - the new display area
    • doSetDisplayArea

      protected void doSetDisplayArea(Bounds envelope)
      Helper method for setDisplayArea(org.geotools.api.geometry.Bounds) which is also called by other methods that want to set the display area without provoking repainting of the display
      Parameters:
      envelope - requested display area
    • reset

      public void reset()
      Reset the map area to include the full extent of all layers and redraw the display
      Specified by:
      reset in interface MapPane
    • getScreenToWorldTransform

      public AffineTransform getScreenToWorldTransform()
      Gets the screen to world coordinate transform. This is a short-cut for mapPane.getMapContent().getViewport().getScreenToWorld().
      Specified by:
      getScreenToWorldTransform in interface MapPane
      Returns:
      the screen to world coordinate transform
    • getWorldToScreenTransform

      public AffineTransform getWorldToScreenTransform()
      Gets the world to screen coordinate transform. This is a short-cut for mapPane.getMapContent().getViewport().getWorldToScreen().

      The returned AffineTransform can be used to determine the current drawing scale...

      
       double scale = mapPane.getWorldToScreenTransform().getScaleX();
       
      Specified by:
      getWorldToScreenTransform in interface MapPane
      Returns:
      the world to screen coordinate transform
    • addMapPaneListener

      public void addMapPaneListener(MapPaneListener listener)
      Adds a listener to receive MapPaneEvents.
      Specified by:
      addMapPaneListener in interface MapPane
      Parameters:
      listener - the listener to add
    • removeMapPaneListener

      public void removeMapPaneListener(MapPaneListener listener)
      Removes the specified listener.
      Specified by:
      removeMapPaneListener in interface MapPane
      Parameters:
      listener - the listener to remove
    • addMouseListener

      public void addMouseListener(MapMouseListener listener)
      Registers an object that wishes to receive MapMouseEvents such as a JMapStatusBar.
      Specified by:
      addMouseListener in interface MapPane
      Parameters:
      listener - the listener to add
      See Also:
    • removeMouseListener

      public void removeMouseListener(MapMouseListener listener)
      Removes the specified listener.
      Specified by:
      removeMouseListener in interface MapPane
      Parameters:
      listener - the listener to remove
    • getCursorTool

      public CursorTool getCursorTool()
      Gets the current cursor tool.
      Specified by:
      getCursorTool in interface MapPane
      Returns:
      the current cursor tool (may be null)
    • setCursorTool

      public void setCursorTool(CursorTool tool)
      Sets the current cursor tool.
      Specified by:
      setCursorTool in interface MapPane
      Parameters:
      tool - the tool; or null for no cursor tool
    • layerAdded

      public void layerAdded(MapLayerListEvent event)
      Called when a new map layer has been added. Sets the layer as selected (for queries) and, if the layer table is being used, adds the new layer to the table.
      Specified by:
      layerAdded in interface MapLayerListListener
      Parameters:
      event - encapsulating the event information
    • layerRemoved

      public void layerRemoved(MapLayerListEvent event)
      Called when a map layer has been removed
      Specified by:
      layerRemoved in interface MapLayerListListener
      Parameters:
      event - encapsulating the event information
    • layerChanged

      public void layerChanged(MapLayerListEvent event)
      Called when a map layer has changed, e.g. features added to a displayed feature collection
      Specified by:
      layerChanged in interface MapLayerListListener
      Parameters:
      event - encapsulating the event information
    • layerMoved

      public void layerMoved(MapLayerListEvent event)
      Triggered when a group of layers chenges position in the layer list
      Specified by:
      layerMoved in interface MapLayerListListener
      Parameters:
      event - encapsulating the event information
    • layerPreDispose

      public void layerPreDispose(MapLayerListEvent event)
      Triggered when a layer is about to be disposed.
      Specified by:
      layerPreDispose in interface MapLayerListListener
      Parameters:
      event - encapsulating the event information
    • mapBoundsChanged

      public void mapBoundsChanged(MapBoundsEvent event)
      Called by the map content's viewport when its bounds have changed. Used here to watch for a changed CRS, in which case the map is re-displayed at full extent.
      Specified by:
      mapBoundsChanged in interface MapBoundsListener
      Parameters:
      event - The change event.
    • publishEvent

      protected void publishEvent(MapPaneEvent ev)
      Publish a MapPaneEvent to registered listeners
      Parameters:
      ev - the event to publish
      See Also:
    • setFullExtent

      protected boolean setFullExtent()
      Determines the full extent of of
      Returns:
      true if full extent was set successfully
    • onRenderingStarted

      public void onRenderingStarted(RenderingExecutorEvent ev)
      Called by the executor when rendering has started. Publishes a MapPaneEvent of type MapPaneEvent.Type.RENDERING_STARTED to listeners.
      Specified by:
      onRenderingStarted in interface RenderingExecutorListener
    • onRenderingCompleted

      public void onRenderingCompleted(RenderingExecutorEvent event)
      Called by the executor when rendering has been completed. Publishes a MapPaneEvent of type MapPaneEvent.Type.RENDERING_STOPPED to listeners.
      Specified by:
      onRenderingCompleted in interface RenderingExecutorListener
    • onRenderingFailed

      public void onRenderingFailed(RenderingExecutorEvent ev)
      Called by the executor when rendering failed for some reason. Publishes a MapPaneEvent of type MapPaneEvent.Type.RENDERING_STOPPED to listeners.
      Specified by:
      onRenderingFailed in interface RenderingExecutorListener