Dashboard > UDIG Developer Guide > Home > 6 Working with GIS Application > 04 Commands > 2 NavCommand Example
2 NavCommand Example Log In | Sign Up   View a printable version of the current page.

Added by Jesse Eichar , last edited by Jody Garnett on Jul 01, 2008  (view change)
Labels: 
(None)

NavCommand is used to go places, specifically these are used to modify the Viewport.

Changes to the viewport are kept in their own undo/redo stack accessible with the Back/Forward navigation buttons (just like a web browser).

Examples

Use from an ActionTool

Action Tools are a good place to issue commands from; this example places a "Go Home" option in the Navigate menu. This example is included in net.refractions.udig.tutorials.examples.

<extension
         point="net.refractions.udig.project.ui.tool">
      <actionTool
            class="net.refractions.udig.tutorials.examples.GoHomeActionTool"
            id="net.refractions.udig.tutorials.examples.actionTool1"
            menuPath="navigate/gohome"
            name="Home"
            onToolbar="false"
            tooltip="Go Home">
         <enablement></enablement>
      </actionTool>
   </extension>

And here is the implementation:

public class GoHomeActionTool extends AbstractActionTool {
    public void run() {
        NavigationCommandFactory factory = getContext().getNavigationFactory();
        
        CoordinateReferenceSystem worldCRS = getContext().getCRS();
        double y = 48.428611;
        double x = -123.365556;
        NavCommand goHome = new SetViewportCenterCommand(new Coordinate(x,y), DefaultGeographicCRS.WGS84 );
            
        getContext().sendASyncCommand( goHome );
    }
    public void dispose() {
    }
}

Custom Navigation Command

A navigation command (INavCommand) is a special type of command that changes the current area being viewed, the viewport. Navigation commands are placed in a separate stack from normal commands and are undone and redone using the navigation arrows on the user interface as opposed to the undo/redo buttons int the edit menu.

Navigation commands should be limited to commands that only change the view area, not changing the current CRS.

The class AbstractNavCommand reduces the burden on the developer by implementing undo functionality.

INavCommand Code Example

PanCommand.java
/**
 * A command that pans the viewport. The command can be defined in terms of pixels on the screen or
 * in terms of world units.
 * 
 * @author jeichar
 * @since 0.3
 */
public class PanCommand extends AbstractNavCommand implements NavCommand {

	double worldx, worldy;

	int pixelx, pixely;

	boolean inPixel;

	/**
	 * Creates a new instance of PanCommand
	 * 
	 * @param pixelx The amount to pan in the x direction
	 * @param pixely The amount to pan in the y direction
	 */
	public PanCommand(int pixelx, int pixely) {
		this.pixelx = pixelx;
		this.pixely = pixely;
		inPixel = true;
	}

	/**
	 * Creates a new instance of PanCommand
	 * 
	 * @param worldx The amount to pan in the x direction
	 * @param worldy The amount to pan in the y direction
	 */
	public PanCommand(double worldx, double worldy) {
		this.worldx = worldx;
		this.worldy = worldy;
		inPixel = false;
	}

	/**
	 * @see net.refractions.udig.project.internal.command.navigation.AbstractNavCommand#runImpl()
	 */
	protected void runImpl() throws Exception {
		if (inPixel)
			model.panUsingScreenCoords(pixelx, pixely);
		else
			model.panUsingWorldCoords(worldx, worldy);
	}

	/**
	 * @see net.refractions.udig.project.internal.command.Command#copy()
	 */
	public Command copy() {
		if (inPixel)
			return new PanCommand(pixelx, pixely);

		return new PanCommand(worldx, worldy);
	}

	/**
	 * @see net.refractions.udig.project.command.Command#getName()
	 */
	public String getName() {
		return Policy.bind("PanCommand.pan"); //$NON-NLS-1$
	}

}
Powered by a free Atlassian Confluence Open Source Project License granted to uDig. Evaluate Confluence today.
Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators
User-friendly Desktop Internet GIS