Commands

Within the Tensegrity Graph Framework, a command class implements an action which is either application-specific or document-specific. 'New Document', 'Save Document' and 'Close Document' are examples of application-specific commands while 'Cut', 'Copy', 'Paste' and 'Delete' are document-specific commands.

By separating Command objects from the UI elements, it is possible for a Skeleton-based application to use the same command objects from within a menubar and a toolbar. Furthermore, Command objects can be used in application code that you write elsewhere.

The Tensegrity Skeleton Framework provides a lengthy set of standard Command classes for a graph document. These classes may be used right away. It is also possible, however, to write your own Command classes by implementing the Command interface.

When implementing the Command interface, the code which does the action has to be placed inside the method perform(). The following example illustrates this.

Example 9.1. The ToggleAssignEdgeModeCommand

/**
 * Command to toggle the edge types used when in 'edge-creation-mode'. 
 * It toggles between 'contain' and 'assigned to' edges.
 */
public class ToggleAssignEdgeModeCommand extends GraphDocumentCommand
{
  /** unique constant to look up the local string for this command */
  public final static String MENU_EDIT_TOGGLE_EDGE_MODE =
        "menu.edit.toggle_assign_edge";
    
  // constants specifying the geometries and styles 
  // for the different edge types
  private final static String ASS_EDGE_GEO_DES =
        "AssignEdgeGeometryDescriptor";
  private final static String ASS_EDGE_STY_DES =
        "AssignEdgeStyleDescriptor";
  private final static String CON_EDGE_GEO_DES =
        "ContainEdgeGeometryDescriptor";
  private final static String CON_EDGE_STY_DES =
        "ContainEdgeStyleDescriptor";
    
  private boolean assignMode=false;
    
  /**
   * Constructor specifying the <code>ApplicationFrame</code> and a 
   * unique id for this command.
   * @param applicationFrame the <code>ApplicationFrame</code> this 
   * command is used within.
   * @param command an unique id for this command.
   */
  public ToggleAssignEdgeModeCommand(
      ApplicationFrame applicationFrame, String command)
  {
      super(applicationFrame, command);
  }
      
  public void perform(Object args)
  {
    super.perform(args);
    // get a reference to the active graph document
    GraphDocument graphDocument =
      (GraphDocument) 
          getApplicationFrame().getMDIComponent().getActiveDocument();
    
    VisualGraphView vgv =graphDocument.getGraphPanel().getVisualGraph();
    
    // set corresponding geometries and styles
    vgv.setDefaultEdgeGeometry(
        assignMode ? CON_EDGE_GEO_DES : ASS_EDGE_GEO_DES);
    vgv.setDefaultEdgeStyle(
        assignMode ? CON_EDGE_STY_DES : ASS_EDGE_STY_DES);
    
    // toggle mode field
    assignMode =!assignMode;
  }
}

Whenever a Command object is created, it becomes available to other parts of your application code. Every Command object is registered with an application wide registry called the CommandRegistry. The following code shows how a Command object may be accessed:

Example 9.2. Accessing a created Command

// get the reference to the command registry
final CommandRegistry commandRegistry =
    getApplicationFrame().getCommandRegistry();
// get the reference to a certain command
Command command = commandRegistry.get(
    ToggleAssignEdgeModeCommand.MENU_EDIT_TOGGLE_EDGE_MODE);