Class MapControl

All Implemented Interfaces:
ComponentListener, ImageObserver, MenuContainer, Serializable, EventListener, org.gvsig.tools.dispose.Disposable, org.gvsig.tools.observer.Observer

public class MapControl extends JComponent implements ComponentListener, org.gvsig.tools.observer.Observer, org.gvsig.tools.dispose.Disposable

A component that includes a MapContext with support for use it as a particular Behavior.

A developer can register a set of Behavior, but only one (that can be a composition of several) of them can be active. The active one defines the way to work and access with its MapContext's layers. The active behavior, in combination with the appropriate ToolListener will allow user work with a particular tool.

All mouse events produced on this component will be delegated to the current active behavior, the currentMapTool.

Drawing process:

Uses a double buffer for the drawing process of MapContext's information.

If the double buffer wasn't created, creates a new one.

Paints the component according the following algorithm:
invalid input: '&nbsp' If status is UPDATED:
invalid input: '&nbsp' invalid input: '&nbsp' If there is a double buffer:
invalid input: '&nbsp' invalid input: '&nbsp' invalid input: '&nbsp' If there is a behavior for managing the MapControl instance, delegates the drawing process to that behavior, calling: behavior_instance.paintComponent(g).
invalid input: '&nbsp' invalid input: '&nbsp' invalid input: '&nbsp' Else, repaints the current graphical information quickly calling: g.drawImage(image,0,0,null).
invalid input: '&nbsp' Else, (status is OUTDATED, or ONLY_GRAPHICS): executes a quickly repaint of the previous information calling g.drawImage(image,0,0,null), and creates a painting request to delegate the heavy drawing process to the

invalid reference
Drawer2
's worker thread, according the SingleWorketThread pattern, starting a timer to update (invoking repaint()) the view every delay of 1000 / drawFrameRate ms. during that heavy drawing process, and if its enabled drawAnimationEnabled. The painting request once is being attended, invokes MapContext to draw the layers: mapContext.draw(image, g, cancel,mapContext.getScaleView());

Some notes:

  • The painting process can be cancelled calling #cancelDrawing().
  • At last resort, the particular implementation of each layer in a MapControl's MapContrext will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.
  • It's possible to force repaint all layers, calling #drawMap(boolean).
  • It's possible repaint only the dirty layers, calling #rePaintDirtyLayers().
  • It's possible repaint only the GraphicLayer, calling #drawGraphics().

Tools:

A developer can:

Exception listener:

Adding an ExceptionListener, can get notification about any exception produced:

  • Attending a painting request.
  • Working with the active tool.
  • Applying a zoom in or zoom out operation.

Other:

Other useful capabilities of MapControl:

  • Cancel the current drawing process (notifying it also to the inner MapContext instance and its layers): #cancelDrawing().
  • Applying a zoom in operation centered at mouse position (without a ToolListener): #zoomIn().
  • Applying a zoom out operation centered at mouse position (without a ToolListener): #zoomOut().

Author:
Fernando Gonz�lez Cort�s, Pablo Piqueras Bartolom� (pablo.piqueras@iver.es)
See Also:
  • Field Details

  • Constructor Details

    • MapControl

      public MapControl()

      Creates a new MapControl instance with the following characteristics:

      • Name: MapControl .
      • Disables the double buffer of JComponent .
      • Sets opaque (see JComponent.setOpaque(boolean) ).
      • Sets its status to OUTDATED .
      • Creates a new CancelDraw object to notify MapContext's layers if can continue processing the drawn or must cancel it.
      • Creates a new MapContext with a new ViewPort in the default projection.
      • Creates a new
        invalid reference
        CommandListener
        for edition operations.
      • Creates a new MapToolListener, and associates it as a listener of whatever kind of mouse events produced in this component.
      • Creates a new
        invalid reference
        Drawer2
        for managing the painting requests.
      • Creates a new timer that will invoke refresh this component drawFrameRate per second, when is running a drawing process, and its enabled drawAnimationEnabled.

    • MapControl

      public MapControl(org.gvsig.fmap.mapcontext.MapContext theMapContext)
  • Method Details

    • addLayer

      public void addLayer(org.gvsig.fmap.mapcontext.layers.FLayer layer)
    • addLayerListener

      public void addLayerListener(ActionListener al)
    • setPaintMode

      public void setPaintMode(int paintMode)
    • getPaintMode

      public int getPaintMode()
    • setMapContext

      public void setMapContext(org.gvsig.fmap.mapcontext.MapContext model)

      Sets a MapContext to this component.

      The MapContext has the model, and most of the view, and control logic of the layers of this component, including a ViewPort to adapt the information to the projection, and to display it in the available area.

      If model hadn't a ViewPort, assigns the current one to it, otherwise, use its ViewPort.

      After assigning the MapContext and ViewPort, sets the same MapContextListener that was using, and changes the status to OUTDATED.

      Parameters:
      model - this component's MapContext, that includes the ViewPort.
      See Also:
    • getMapControlDrawer

      public MapControlDrawer getMapControlDrawer()
      Returns:
      the mapControlDrawer
    • setMapControlDrawer

      public void setMapControlDrawer(MapControlDrawer mapControlDrawer)
      Parameters:
      mapControlDrawer - the mapControlDrawer to set
    • getProjection

      public org.gvsig.crs.projection.lib.Projection getProjection()

      Gets this component's MapContext projection.

      Returns:
      this component's MapContext projection
      See Also:
    • setProjection

      public void setProjection(org.gvsig.crs.projection.lib.Projection proj)

      Sets the projection to this component's MapContext.

      Parameters:
      proj - the kind of projection to this component's MapContext
      See Also:
    • getMapContext

      public org.gvsig.fmap.mapcontext.MapContext getMapContext()

      Gets this component's MapContext, with the model, and most of the view, and control logic of the layers of this component, including a ViewPort to adapt the information to the projection, and display it in the available area.

      Returns:
      this component's MapContext, that includes the ViewPort used to project the graphical information, and display it in the available area
      See Also:
    • addBehavior

      public void addBehavior(String name, Behavior tool)

      Registers a new behavior to this component.

      According the nature of the Behavior, different events will be generated. Those events can be caught by a particular ToolListener, allowing user to interact with this MapControl object as a tool.

      Parameters:
      name - name to identify the behavior to add
      tool - the behavior to add
      See Also:
    • addBehavior

      public void addBehavior(String name, Behavior[] tools)

      Registers a new behavior to this component as a CompoundBehavior made up of tools.

      According the nature of the behaviors registered, different events will be generated. Those events can be caught by a particular ToolListener, allowing user to interact with this MapControl object as a tool.

      Parameters:
      name - name to identify the compound behavior to add
      tools - the compound behavior to add
      See Also:
    • getMapTool

      public Behavior getMapTool(String name)

      Gets the Behavior registered in this component, identified by name.

      Parameters:
      name - name of a registered behavior
      Returns:
      tool the registered behavior in this component as name, or null if no one has that identifier
      See Also:
    • getMapToolsKeySet

      public Set getMapToolsKeySet()

      Returns a set view of the keys that identified the tools registered.

      Returns:
      a set view of the keys that identified the tools registered
      See Also:
    • hasTool

      public boolean hasTool(String toolName)

      Returns true if this component contains a tool identified by toolName.

      Parameters:
      toolName - identifier of the tool
      Returns:
      true if this component contains a tool identified by toolName; otherwise false
      See Also:
    • setTool

      public void setTool(String toolName)

      Sets as current active Behavior associated to this component, that one which is registered and identified by toolName.

      Changing the current active behavior for this MapControl, implies also updating the previous behavior tool, and the current cursor.

      Parameters:
      toolName - name of a registered behavior
      See Also:
    • getTool

      public Behavior getTool(String name)
    • getCurrentMapTool

      public Behavior getCurrentMapTool()

      Gets as current active Behavior associated to this component, that one which is registered and identified by toolName.

      Changing the current active behavior for this MapControl, implies also updating the previous behavior tool, and the current cursor.

      Parameters:
      toolName - name of a registered behavior
      See Also:
    • getCurrentTool

      public String getCurrentTool()

      Returns the name of the current selected tool on this MapControl

      Returns:
      the name of the current's behavior tool associated to this component
      See Also:
    • cancelDrawing

      public void cancelDrawing()

      Determines that current drawing process of MapControl's MapContext's data must be canceled.

      It has no effects if now isn't drawing that graphical information.

      At last resort, the particular implementation of each layer in this MapControl's MapContrext will be that one which will draw the graphical information, and, if supports, which could cancel its drawing subprocess.

    • paintComponent

      protected void paintComponent(Graphics g)

      Paints the graphical information of this component using a double buffer.

      If the double buffer wasn't created, creates a new one.

      Paints the component according the following algorithm:
      invalid input: '&nbsp' If status is UPDATED:
      invalid input: '&nbsp' invalid input: '&nbsp' If there is no double buffer:
      invalid input: '&nbsp' invalid input: '&nbsp' invalid input: '&nbsp' If there is a behavior for managing the MapControl instance, delegates the drawing process to that behavior, calling: behavior_instance.paintComponent(g) invalid input: '&nbsp' .
      invalid input: '&nbsp' invalid input: '&nbsp' invalid input: '&nbsp' Else, repaints the current graphical information quickly calling: g.drawImage(image,0,0,null) invalid input: '&nbsp' .
      invalid input: '&nbsp' Else, (status is OUTDATED, or ONLY_GRAPHICS): executes a quickly repaint of the previous information calling g.drawImage(image,0,0,null), and creates a painting request to delegate the heavy drawing process to the

      invalid reference
      Drawer2
      's worker thread, according the SingleWorketThread pattern, starting a timer to update (invoking repaint() that comprises invoke this method) the view every delay of 360 ms. during the the process drawing.

      Overrides:
      paintComponent in class JComponent
      See Also:
    • getImage

      public BufferedImage getImage()

      Gets the BufferedImage used to accelerate the draw of new ''frames'' with changes, or new graphical items in this component.

      Returns:
      double buffered image used by this component to accelerate the draw of its graphical information, or null if isn't already created
      See Also:
    • drawMap

      public void drawMap(boolean doClear)

      Forces repaint all visible graphical information in this component.

      If doClear == true, before repainting, clears the background color, with the inner viewport's background color.

      Parameters:
      doClear - true if needs clearing the background color before drawing the map
      See Also:
    • rePaintDirtyLayers

      public void rePaintDirtyLayers()

      Cancels any current drawing process, changing the status to OUTDATED, and forcing repaint only the layers dirty.

      See Also:
    • drawGraphics

      public void drawGraphics()
    • componentHidden

      public void componentHidden(ComponentEvent e)
      Specified by:
      componentHidden in interface ComponentListener
      See Also:
    • componentMoved

      public void componentMoved(ComponentEvent e)
      Specified by:
      componentMoved in interface ComponentListener
      See Also:
    • componentResized

      public void componentResized(ComponentEvent e)
      Specified by:
      componentResized in interface ComponentListener
      See Also:
    • componentShown

      public void componentShown(ComponentEvent e)
      Specified by:
      componentShown in interface ComponentListener
      See Also:
    • addExceptionListener

      public void addExceptionListener(org.gvsig.utils.exceptionHandling.ExceptionListener o)
      See Also:
      • ExceptionHandlingSupport.addExceptionListener(ExceptionListener)
    • removeExceptionListener

      public boolean removeExceptionListener(org.gvsig.utils.exceptionHandling.ExceptionListener o)
      See Also:
      • ExceptionHandlingSupport.removeExceptionListener(ExceptionListener)
    • throwException

      protected void throwException(Throwable t)
      See Also:
      • ExceptionHandlingSupport.throwException(Throwable)
    • getViewPort

      public org.gvsig.fmap.mapcontext.ViewPort getViewPort()

      Gets the ViewPort of this component's MapContext .

      See Also:
      • MapContext.getViewPort()
    • getNamesMapTools

      public HashMap getNamesMapTools()

      Returns all registered Behavior that can define a way to work with this MapControl.

      Returns:
      registered Behavior to this MapControl
      See Also:
    • commandRepaint

      public void commandRepaint()
    • commandRefresh

      public void commandRefresh()
    • setPrevTool

      public void setPrevTool()

      Equivalent operation to undo.

      Exchanges the previous tool with the current one.

      See Also:
    • zoomIn

      public void zoomIn()

      Executes a zoom in operation centered at the center of the extent.

      This implementation is designed for being invoked outside a Behavior, for example by an action of pressing a button; and simulates that the event has been produced by releasing the button 1 of the mouse using the registered Behavior in this MapControl object that's responsible for the zoom in operation.

      See Also:
    • zoomOut

      public void zoomOut()

      Executes a zoom out operation centered at the center of the extent.

      This implementation is thought for being invoked outside a Behavior, for example by an action of pressing a button, and simulates that the event has been produced by releasing the button 1 of the mouse using the registered Behavior in this MapControl object that's responsible for the zoom out operation.

      See Also:
    • getMapToolListener

      public MapControl.MapToolListener getMapToolListener()

      Returns the listener used to catch all mouse events produced in this MapControl instance and that redirects the calls to the current map tool.

      Returns:
      the map tool listener used
    • setCurrentMapTool

      public void setCurrentMapTool(Behavior mapTool)

      Sets mapTool as this MapControl's current map tool.

      Parameters:
      mapTool - a map tool, or null to disable the interaction with the user
      See Also:
    • applyFrameRate

      public void applyFrameRate()

      Sets the delay to the timer that refreshes this MapControl instance.

      Delay (in ms) = 1000 / getDrawFrameRate()

      See Also:
      • invalid reference
        #getDrawFrameRate()
      • invalid reference
        #setDrawFrameRate(int)
    • isDrawAnimationEnabled

      public static boolean isDrawAnimationEnabled()

      Determines if its enabled the repaint that invokes the timer according to

      invalid reference
      #getDrawFrameRate()
      .

      Returns:
      true if its enabled; otherwise false
    • setDrawAnimationEnabled

      public static void setDrawAnimationEnabled(boolean drawAnimationEnabled)

      Sets if its enabled the repaint that invokes the timer according to

      invalid reference
      #getDrawFrameRate()
      .

      Parameters:
      drawAnimationEnabled - true to enable the mode; otherwise false
    • getCanceldraw

      public MapControl.CancelDraw getCanceldraw()

      Gets the shared object that determines if a drawing process must be cancelled or can continue.

      Returns:
      the shared object that determines if a drawing process must be cancelled or can continue
    • getCombinedTool

      public Behavior getCombinedTool()

      Gets the tool used in combination with the current tool of this MapControl.

      Returns:
      the tool used in combination with the currentMapTool ; null if there is no combined tool
    • setCombinedTool

      public void setCombinedTool(Behavior combinedTool)

      Sets a tool to be used in combination with the current tool of this MapControl.

      Parameters:
      combinedTool - a tool to be used in combination with the current tool of MapControl
    • addCombinedBehavior

      public void addCombinedBehavior(Behavior tool)

      Adds a new tool as combined tool.

      The new tool will be stored with the previous combined tools, and will be combined with the current tool.

      If tool was already stored as a combined tool, doesn't adds it.

      Parameters:
      tool - a new tool to be used combined with the current tool
    • removeCombinedTool

      public void removeCombinedTool(Behavior tool)

      Removes the tool tool used in combination with the current tool of this MapControl.

    • initializeGrid

      public void initializeGrid()

      Updates the grid on the ViewPort of the associated MapControl object according the values in the

      invalid reference
      com.iver.cit.gvsig.gui.cad.CADToolAdapter.prefs.Preferences
      .

      The preferences are:

      • Show/hide the grid.
      • Adjust or not the grid.
      • Horizontal ( X ) line separation.
      • Vertical ( Y ) line separation.

    • setAdjustGrid

      public void setAdjustGrid(boolean adjustGrid)
    • setGridVisibility

      public void setGridVisibility(boolean showGrid)
    • isRefentEnabled

      public boolean isRefentEnabled()
      Determines if snap tools are enabled or disabled.
      Returns:
      true to enable the snap tools; false to disable them
      See Also:
    • calculateSnapPoint

      public void calculateSnapPoint(Point2D point)

      Tries to find the nearest geometry or grid control point by the position of the current snap tool.

      Prioritizes the grid control points than the geometries ones.

      If finds any near, stores the map and pixel coordinates for the snap, and enables the bForceCoord attribute for the next draw of the mouse's cursor.

      Parameters:
      point - current mouse 2D position
    • setRefentEnabled

      public void setRefentEnabled(boolean activated)
      Sets the snap tools enabled or disabled.
      Parameters:
      activated - true to enable the snap tools; false to disable them
      See Also:
    • getGrid

      public Grid getGrid()
    • update

      public void update(org.gvsig.tools.observer.Observable observable, Object notification)
      Specified by:
      update in interface org.gvsig.tools.observer.Observer
    • getAdjustedPoint

      public Point2D getAdjustedPoint()
      Returns:
      the adjustedPoint
    • getMapAdjustedPoint

      public Point2D getMapAdjustedPoint()
      Returns:
      the mapAdjustedPoint
    • convertToMapPoint

      public Point2D convertToMapPoint(Point2D point, boolean useSnapping)
    • getPoint

      public Point2D getPoint()
      Gets the selected point. If the snapping is enabled it returns the selected point.
      Returns:
      The selected point
    • dispose

      public void dispose()
      Specified by:
      dispose in interface org.gvsig.tools.dispose.Disposable
    • isDefaultMouseWheelEnabled

      public boolean isDefaultMouseWheelEnabled()
    • setDefaultMouseWheelEnabled

      public void setDefaultMouseWheelEnabled(boolean defaultMouseWheelEnabled)
    • addChangeToolListener

      public void addChangeToolListener(ChangeListener listener)
    • getChangeToolListeners

      public ChangeListener[] getChangeToolListeners()
    • removeChangeToolListener

      public void removeChangeToolListener(ChangeListener listener)
    • removeAllChangeToolListener

      public void removeAllChangeToolListener()
    • hasChangeToolListeners

      public boolean hasChangeToolListeners()