Class MapPanel

All Implemented Interfaces:
LayerListListener, ActionListener, MouseListener, MouseMotionListener, WindowListener, ImageObserver, MenuContainer, Serializable, EventListener, Accessible

This class implements a custom mapping component. The mapping component works closely with a LayerList object that contains information about the layers that should be drawn in the component.

This component maintains the extent and scale of the data to be drawn, and handles mouse motion for zooming and panning. The MapPanel is a 'viewer' in the MVC model, and an associated LayerList is the Model. Some other object (such as a MapViewer) would be the controller.

The LayerList does most of the heavy work involved in rendering by maintaining a backing store, coordinating state, etc.

When drawing is performed is is done in the LayerList by a background thread (this allows the GUI to remain responsive). The MapPanel will refresh itself on 1000 msec intervals based on a Timer. The Timer invokes a method to determine if a refresh is appropriate, and conserves resources if there is nothing to redraw.

When screen damage must be repaired it is done so from a backing store.

This component is simple and unornamented. Most applications will embed the MapPanel within a MapViewer, which provides the ornamentation that you would expect from a map viewer (toolbar, scale box, eastings and northings, legend).

This Object accept drop actions, and will open files or run scripts accordingly.

This component is based on Swing and must follow the Swing threading rules.

See Also:
LayerList, MapViewer, Serialized Form
  • Field Details

  • Constructor Details

    • MapPanel

      public MapPanel(LayerList layers)
      MapPanel constructor.
      Parameters:
      layers - A LayerList object that contains the layer model.
    • MapPanel

      public MapPanel()
      No-args constructor only for use by the Serialization routines.
  • Method Details

    • getLayerList

      public LayerList getLayerList()
      Return the reference to the LayerList used in this panel to interested parties.
    • layersChanged

      public void layersChanged(int index)
      Receive notification that some layers have changed. The mapextents and limits will have to be recalculated.
      Specified by:
      layersChanged in interface LayerListListener
    • layerAdded

      public void layerAdded(int index)
      Receive notification that a layer was added. If this is the initial layer then change the extent to match this new layer.
      Specified by:
      layerAdded in interface LayerListListener
    • setEastingLabel

      public void setEastingLabel(JLabel label)
      Define a status label for the map eastings under the cursor
    • setNorthingLabel

      public void setNorthingLabel(JLabel label)
      Define a status label for the map northings under the cursor
    • setScaleField

      public void setScaleField(WholeNumberField field)
    • getTransform

      public AffineTransform getTransform()
      Get the AffineTransform that represents the mapping of geographic coordinates into page (screen) coordinates.
      Specified by:
      getTransform in interface LayerListListener
    • getExtent2D

      public Extent2D getExtent2D()
      Get an Extent2D object that represents the current extent of the view in geographic coordinates.
      Specified by:
      getExtent2D in interface LayerListListener
    • setBounds

      public void setBounds(int x, int y, int width, int height)
      Change the bounds of the component
      Overrides:
      setBounds in class Component
    • windowClosing

      public void windowClosing(WindowEvent e)
      Specified by:
      windowClosing in interface WindowListener
    • windowOpened

      public void windowOpened(WindowEvent e)
      Specified by:
      windowOpened in interface WindowListener
    • windowClosed

      public void windowClosed(WindowEvent e)
      Specified by:
      windowClosed in interface WindowListener
    • windowIconified

      public void windowIconified(WindowEvent e)
      Specified by:
      windowIconified in interface WindowListener
    • windowDeiconified

      public void windowDeiconified(WindowEvent e)
      Specified by:
      windowDeiconified in interface WindowListener
    • windowActivated

      public void windowActivated(WindowEvent e)
      Specified by:
      windowActivated in interface WindowListener
    • windowDeactivated

      public void windowDeactivated(WindowEvent e)
      Specified by:
      windowDeactivated in interface WindowListener
    • setMouseMode

      public void setMouseMode(int mode)
      Set the mouse motion mode to PAN, ZOOMIN, ZOOMOUT or SELECT
    • setMouseMode

      public void setMouseMode(ClickHandler clickHandler)
      Set the mouse motion mode to a ClickHandler
    • setMouseMode

      public void setMouseMode(RecordHandler recordHandler)
      Set the mouse motion mode to a ClickHandler
    • setMouseMode

      public void setMouseMode(LinetoHandler linetoHandler)
      Set the mouse motion mode to a LineHandler
    • getScale

      public double getScale()
      Compute the mapscale that is being used on the page (screen).
      Specified by:
      getScale in interface LayerListListener
    • setScale

      public void setScale(double value)
      Set the mapscale to be used on the page (screen).
    • setMaxExtent

      public void setMaxExtent(Extent2D extent)
      Set the default maximum extent of the image
      Specified by:
      setMaxExtent in interface LayerListListener
    • pushHistory

      public void pushHistory(Extent2D e)
      Push a map extent on to the history stack
    • popHistory

      public Extent2D popHistory()
      Pop a map extent off of the history stack
    • zoom

      public void zoom(double factor)
      Zoom by an arbitrary factor.
      Parameters:
      factor - Factors less than 1 zoom in, greater than one zoom out.
    • zoomIn

      public void zoomIn()
      Zoom in by a default 50%
    • zoomOut

      public void zoomOut()
      Zoom out by a default 100%
    • zoomTo

      public void zoomTo(double xmin, double ymin, double xmax, double ymax)
      Zoom to a new extent.
    • zoomAt

      public void zoomAt(double x, double y, double factor)
      Zoom to a new center location.
    • zoomOutRelative

      public void zoomOutRelative(double xmin, double ymin, double xmax, double ymax)
      Zoom out by a relative amount. The new extent will be centered on the midpoint of the box. The zoom out factor will be the ratio of the current extent to the box.
    • translate

      public void translate(double x, double y)
      Translate the extent by a specified x and y distance.
    • reset

      public void reset()
      Set the extent back to the maximum.
      Specified by:
      reset in interface LayerListListener
    • setAdjust

      public void setAdjust(boolean scaleChanged)
      Request that the drawing methods prepare to repaint the screen as soon as possible. Actual redrawing will not occur until the repaint timer fires the next time.
      Parameters:
      scaleChanged - Indicate if the scale has changed as a result of this request, in order to pass notification up to any other LayerList listeners (for example, the legend).
    • paintComponent

      public void paintComponent(Graphics gc)
      Custom component painting routine This still needs to be fixed to not draw on borders
    • print

      public void print(Graphics2D g, Dimension d, double printScale, int dpi, LayerPrintCallback callback)
      Print the map to a graphics context. This will require user code to set up the print job and get the map context. Probably not usable in user scripts.
    • actionPerformed

      public void actionPerformed(ActionEvent e)
      Handle events from the timer or scale changes. Repaint the map if an update has occured.
      Specified by:
      actionPerformed in interface ActionListener
    • maybeUpdate

      public void maybeUpdate()
      Update the map, so long as the mouse is not involved in a drag operation.
      Specified by:
      maybeUpdate in interface LayerListListener
    • mouseEntered

      public void mouseEntered(MouseEvent e)
      Specified by:
      mouseEntered in interface MouseListener
    • mouseExited

      public void mouseExited(MouseEvent e)
      Specified by:
      mouseExited in interface MouseListener
    • mouseClicked

      public void mouseClicked(MouseEvent e)
      Specified by:
      mouseClicked in interface MouseListener
    • mousePressed

      public void mousePressed(MouseEvent e)
      Arm the system for a mouse drag operation.
      Specified by:
      mousePressed in interface MouseListener
    • mouseReleased

      public void mouseReleased(MouseEvent e)
      Clean up after drag mode has ended.
      Specified by:
      mouseReleased in interface MouseListener
    • mouseMoved

      public void mouseMoved(MouseEvent e)
      Track the X,Y location of the mouse
      Specified by:
      mouseMoved in interface MouseMotionListener
    • mouseDragged

      public void mouseDragged(MouseEvent e)
      Catch mouse dragged events. These occur when the mouse is moved while the button is pressed.
      Specified by:
      mouseDragged in interface MouseMotionListener
    • setActions

      public void setActions()
      This method initializes a set of Action objects that describe functions that can be performed by this MapPanel.
    • getAction

      public AbstractAction getAction(String command)
      Get a previously defined action. This useful for building user interfaces.
    • makeButton

      public static JButton makeButton(Action a)
      Make a JButton from an Action, setting the command and insets to conform to the Patchworks look and feel.
    • getButtonGroup

      public ButtonGroup getButtonGroup()
      Get the ButtonGroup that is used for pointer modes such as zoomInMode, zoomOutMode, panMode etc., in case you need to add other actions to this buttongroup.
    • loadToolBar

      public JToolBar loadToolBar(JToolBar toolbar)
      This routine will populate a toolbar with commonly used tools
    • addFiles

      public void addFiles(List files, String encoding)
      Add files to the MapPanel. This is primarily used by the drag-and-drop mechanism, but can also be called from user scripts.