GEF v2.0

org.eclipse.gef.commands
Interface Command

All Known Implementing Classes:
AbstractCommand

public interface Command

This interface represents basic behaviour that every command is expected to support. A command can be tested for executability, it can be executed, it can be tested for undoability, it can be undone, and can then be redone. A comand also provides access to a result collection, an affected-objects collection, a label, and a description.

There are important constraints on the valid order in which the various methods may be invoked, e.g., you cannot ask for the result before you've executed the command. These constraints are documented with the various methods.


Field Summary
static String copyright
           
 
Method Summary
 boolean canExecute()
          This indicates whether the comamad is valid to execute.
 boolean canUndo()
          This returns whether the command can be undone.
 Command chain(Command command)
          This logically chains the given command to this command, by returning a command that represents the composition.
 void dispose()
          This is called to indicate that the command will never be used again.
 void execute()
          This will perform the command activity required for the effect.
 Collection getAffectedObjects()
          This returns the collection of things which this command wishes to present as the objects affected by the command.
 String getDescription()
          This returns a string suitable to help describe the effect of this command.
 String getLabel()
          This returns a string suitable to represent the label that identifies this command.
 Collection getResult()
          This returns collection of things which this command wishes to present as it's result.
 void redo()
          This will again perform the command activity required to redo the effect after undoing the effect.
 void undo()
          This will perform the command activity required to undo the effects of a preceding execute (or redo).
 

Field Detail

copyright

public static final String copyright
Method Detail

canExecute

public boolean canExecute()
This indicates whether the comamad is valid to execute. The UnexecutableCommand.INSTANCE.canExecute() always returns false. This must be called before calling execute.

execute

public void execute()
This will perform the command activity required for the effect. The effect of calling execute when canExecute returns false, or when canExecute hasn't been called, is undefined.

canUndo

public boolean canUndo()
This returns whether the command can be undone. The result of calling this before execute is well defined, but the result of calling this before calling canExecute is undefined, i.e., a command that retuns false for canExecute may return true for canUndo, even though that is a contradiction.

undo

public void undo()
This will perform the command activity required to undo the effects of a preceding execute (or redo). The effect, if any, of calling undo before execute or redo have been called, or when canUndo returns false, is undefined.

redo

public void redo()
This will again perform the command activity required to redo the effect after undoing the effect. The effect, if any, of calling redo before undo is called is undefined. Note that if you implement redo to call execute then any derived class will be restricted to by that decision also.

getResult

public Collection getResult()
This returns collection of things which this command wishes to present as it's result. The result of calling this before an execute or redo, or after an undo, is undefined.

getAffectedObjects

public Collection getAffectedObjects()
This returns the collection of things which this command wishes to present as the objects affected by the command. Typically should could be used as the selection that should be highlighted to best illustrate the effect of the command. The result of calling this before an execute, redo, or undo is undefined. The result may be different after an undo than it is after an execute or redo, but the result should be the same (equivalent) after either an execute or redo.

getLabel

public String getLabel()
This returns a string suitable to represent the label that identifies this command.

getDescription

public String getDescription()
This returns a string suitable to help describe the effect of this command.

dispose

public void dispose()
This is called to indicate that the command will never be used again. Calling any other method after this one has undefined results.

chain

public Command chain(Command command)
This logically chains the given command to this command, by returning a command that represents the composition. The resulting command may just be this, if this command is capabable of composition. Otherwise, it will be a new command created to compose the two.

Instead of the following pattern of usage

   Command result = x;
   if (condition) result = result.chain(y);
 
you should consider using a CompoundCommand and using CompoundCommand.unwrap() to optimize the result:
   CompoundCommand subcommands = new CompoundCommand();
   subcommands.append(x);
   if (condition) subcommands.append(y);
   Command result = subcommands.unwrap();
 
This gives you more control over how the compound command composes it's result and affected objects.

GEF v2.0