JMap Tools
A JMap tool provides interaction between the user and the map using the mouse. For example, when the user activates the selection tool and clicks on the map to select an item, it is the selection tool class that performs the work. This tool is programmed to make a selection at the location that is clicked on the map. Similarly, when the distance measurement tool is used, the tool class calculates and displays the distance between the 2 points clicked by the user. Thus, developing a JMap tool allows you to implement custom actions.
In general, only one tool at a time can be enabled. In order to be enabled, a tool must be the active tool in a JMap view. To do so, you must use the method setCureentTool(Tool) of the View or ViewManager class. Note that the method used to activate the tool, such as a button or a menu item, has no connection with the operation of the tool itself.
When the user changes the active the tool (e.g. by pressing a button), the code will essentially be as follows:
JMapApplicationContext.getInstance().getViewManager().setCurrentTool(new MyTool());
|
When a tool is active, it receives all the mouse events that are generated by the active view. The Tool class is responsible for processing these events and taking the appropriate actions.
To develop a new tool, you must program a class derived from the abstract class Tool and implement only the methods needed to perform the tool's function. For example, if the tool must perform an action when the user clicks the mouse, you must implement the onToolClicked() method.
Tool class methods |
|
This method is called when the tool becomes the active tool of a view. The code for this method should be used as necessary to prepare the work of the tool. The view is passed as a parameter to this method. |
|
This method is called by the view when the tool becomes active to allow your tool to provide its own mouse cursor. The cursor will be visible on the view as long as your tool remains the active tool. |
|
This method is called when the user presses on one of the mouse buttons within the view. |
|
This method is called when the user releases a mouse button within the view. |
|
This method is called after the user has completed a mouse click within the view. |
|
This method is called repeatedly when the user moves the mouse inside the view. |
|
This method is called repeatedly when the user moves the mouse inside the view while keeping a button pressed. |
|
This method is called when the tool becomes inactive, i.e. when another tool is activated on the screen. The code for this method could be used, as needed, to perform a termination action or to free up resources. |
|
The following code example shows a simple tool that displays the properties of the first element found at the coordinates of the mouse cursor when the user presses and releases the left mouse button on the map. Only vector layers are considered and the search is performed from the highest layer to the lowest layer, in the layer display order. Other methods of the Tool class are not implemented because they are not required for the operation of this tool.
|
Drawing tools
You can implement tools to draw on the map simply by deriving them from existing JMap classes for drawing tools.
By deriving from these classes, all the basic operations are inherited. The following options are available:
| • | Persistent and volatile modes (determines whether items are created); |
| • | Select the layer that will receive the items drawn; |
| • | Style parameters of drawn elements (colors, line types, etc.); |
| • | Display dimensions on the map while drawing. |
The following table lists the classes for drawing tools available in JMap. All these classes are derived from the ToolDraw class.
Drawing classes derived from ToolDraw |
|
Draws a box (rectangle) on the map. |
|
Draws a circle on the map. |
|
Draws a simple line on the map. |
|
Draws a multiline on the map. |
|
Draws a polygon on the map. |
|
The following table lists the most commonly used methods of the ToolDraw class.
ToolDraw class methods |
|
Returns the StyleContainer object that contains the styles of the various types of elements (polygons, lines, etc.) that can be drawn. This method is called to change the style of the elements that will be drawn by the tool. |
|
Specifies the layer that will receive the items drawn by the tool, if the elements are persistent (see setPersistant (boolean) method below). If no layer is specified, a system layer is used by default. |
|
Determines whether the items will be stored on a layer or not (see setDrawLayer (VectorLayer) method above). If they are not stored, the elements disappear immediately after the drawing operation is completed. |
|
The following code example shows a tool derived from ToolDrawPolygon that inherits all of its drawing capabilities.
|