We have been able to make the following plug-in tutorial available:
The tutorial is in the form of a Adobe PDF file and has been converted to confluence.
This tutorial was also coverted in the uDig presentation at OSG'05.
Create your first uDig plug-in! (Featured at OSG'05 conference)
After completing this tutorial, you will have gained the skills to:
- Create a new Plugin
- Define a new Extension
- Implement a new Tool Extension
- Update the user interface from within a tool
- Open the Plug-in Development perspective (click on
)
- From the File menu, select New > Project…. Select Plug-in Project from the dialog, and then click the Next button.
Figure 1 - New Plug-in Project
- Create a name for the plug-in by entering net.refractions.udig.tutorials.distancetool in the Project Name text field and click the Next button.
Figure 2 - Naming the New Plug-in
- Accept the default values used to generate the plug-in and click the Finish button.
Figure 3 - New Plug-in Details
At this point the feature editor plug-in is created. Configuring the plug-in is the focus of the next section.
In this section you will configure the feature editor plug-in. Specifically, you will specify dependencies on other plug-ins in order to create a new feature editor.
- Open the Plug-in Development perspective.
- In the Package Explorer navigate to the plug-in created in the previous section. Open the plug-in manifest by navigating to the META-INF/MANIFEST.MF file under the root of the feature editor plug-in. Double click on the file to open the plug-in manifest editor.
Figure 4 - Plug-in Manifest Editor
- Open the plug-in dependencies by clicking on the Dependencies tab located at the bottom of the editor area.
- Click the Add… button in the Required plugins column and add the following plugin:
net.refractions.udig.project.ui
Figure 5 - Add New Plugin Dependencies
- At this point it is critical that you
Save your work as the dependencies need to propagate into the project.
Figure 6 - Plug-in Dependencies
In this section we are going to set up an icon directory and import the pictures we will use for our measure tool.
- Download measure_source.gif and measure_mode.gif to your desktop (Right click and Save Link As...)
- Select your plug-in project, net.refractions.udig.tutorials.distancetool, in the Package Explorer.
- Select File → New → Folder from the Menubar.
- Enter icons/etool16 as the folder name.
- Click the Finish button.
- Select icons directory.
- Select File → New → Folder from the Menubar.
- Enter pointers as the folder name.
- Click the Finish button.

- Right click on etool16 and select Import.
- Select File System.

- Click on browse and choose Desktop from the list (this will populate the directory field).
- Select the measure_mode.gif file and press Finish.

- Import the measure_source.gif file into the pointers directory following the same steps.
- Open the extensions page by clicking on the Extensions tab

- Click the Add… button
- Select the net.refractions.udig.project.ui.tool extension point from the list.

- Click the Finish button.
- Enter the following Extension Details:
- ID: net.refractions.udig.tutorial.distancetool
- Name: Distance Tool Example

- Right click on newly added extension, net.refractions.udig.project.ui.tool, and select New > modalTool
- Replace the default data in the id field with net.refractions.udig.tutorial.distancetool.
- Enter a tool tip message into the tooltip field: Measure the surface distance between two points
- Enter net.refractions.udig.tutorial.distancetool.DistanceTool into the class field.
- Enter icons/etool16/measure_mode.gif into the icon field.
(Or press the Browse button and locate the icon)
- Enter Distance into the name field.
- Set onToolbar to true.
- Enter net.refractions.udig.tool.category.info into the categoryId field.

- Right click on net.refractions.udig.tutorial.distancetool (modalTool) and select New > cursor.
- Enter icons/pointers/measure_source.gif in the image field.
Or press the browse button and find the pointer icon.
- Enter 10 in the hotSpotX field.
- Enter 10 in the hotSpotY field.

- Save your work before proceeding to the next section.
- Select net.refractions.udig.tutorial.distancetool (modalTool) in the Extensions editor.
- It is a child of the net.refractions.udig.project.ui.tool.
- Click the class hotlink.

- A dialog is brought up describing the class to be created, Check Generate comments.

- Press Finish, if not available ensure that all the information is in agreement with the picture above.
- Right click on editor and select * Source > Override/Implement Methods*
- Expand SimpleTool node and check the following:
- Check onMousePressed(MapMouseEvent)
- Check onMouseReleased(MapMouseEvent)
- Click the OK button.
- Implement the onMousePressed(MapMouseEvent) method.
Coordinate start; public void onMousePressed(MapMouseEvent e) {
start=getContext().pixelToWorld(e.x, e.y);
}
- Implement the onMouseReleased(MapMouseEvent) method.
public void onMouseReleased(MapMouseEvent e) {
Coordinate end=getContext().pixelToWorld(e.x, e.y);
try {
double distance=JTS.orthodromicDistance(
start, end,
getContext().getCRS() );
displayOnStatusBar(distance);
} catch (Exception e1) {
}
}
- Implement the displayError () method.
private void displayError() {
final IStatusLineManager statusBar =
getContext().getActionBars().getStatusLineManager ();
if( statusBar==null )
return;
getContext().updateUI(new Runnable() {
public void run() {
statusBar.setErrorMessage("Unable to calculate the distance");
}
});
}
- Implement the displayOnStatusBar(double) method.
private void displayOnStatusBar(double distance) {
final IStatusLineManager statusBar =
getContext().getActionBars().getStatusLineManager ();
if( statusBar==null )
return; int totalmeters=(int)distance;
final int km=totalmeters/1000;
final int meters=totalmeters-(km*1000);
float cm = (float) (distance-totalmeters)*10000;
cm = Math.round(cm);
final float finalcm=cm/100;
getContext().updateUI(new Runnable(){
public void run() {
statusBar.setMessage("Distance = "+km+","+meters+"m "+finalcm+"cm");
}
});
}
- At this point you may have red x's on the project - Lets fix them!
- Press Ctrl+Shift+0 to import any needed classes
- GeoTools contains two classes called JTS (they are moving the class to a more appropriate home), please choose org.geotools.geometry.jts.JTS:

- And then save the file. This should refresh the project and clear up any error markers left behind.
- From the Project menu select Run and choose the configuration you set-up in the previous tutorial (see 0 SDK Quickstart).
- When uDig launches make a new map and your Distance tool should now be in the tool bar ready for use.
- File -> New Map
- Drag and Drop a Web Map Server url onto the screen:
- Select the Distance Tool and drag from one point to another in the Map View and the distance should display in the bottom left corner.
Q: Unable to calculate the distance!
A: The projection of your data must be defined, you may see this if you are working with a shapefile that does not have a ".prj" file defined.
Q: A connection error has occured
A: Sounds like the data you were looking for is unavailable, try a different WMS.
- Tools are organized into "Categories" each with their own keyboard short-cut, the Distance Tool is in the category "Information".
- You can select the "Information" category by pressing "i" while working with the Map, you can cycle between the different information tools by pressing "i" additional times.
|