org.eclipse.emf.common.command
Class AbstractCommand

java.lang.Object
  extended by org.eclipse.emf.common.command.AbstractCommand
All Implemented Interfaces:
Command
Direct Known Subclasses:
AbstractOverrideableCommand, AddMappingCommand, AddOverrideCommand, ChangeCommand, CommandWrapper, CompoundCommand, CreateCopyOverrideCommand, CreateMappingCommand, DragAndDropCommand, IdentityCommand, InitializeCopyOverrideCommand, RemoveMappingCommand, RemoveOverrideCommand, RestoreInitialStateCommand, SetOverrideCommand, UnexecutableCommand

public abstract class AbstractCommand
extends java.lang.Object
implements Command

An abstract implementation of a basic command. Each derived class must implement Command.execute() and Command.redo(), must either implement undo() or implement canUndo() to return false, and must either override prepare() (this is the preferred approach) or can override canExecute() directly.

It is very convenient to use prepare, as it is guaranteed to be called only once just before canExecute is to be tested. It can be implemented to create any additional commands that need to be executed, and the result it yields becomes the permanent cached return value for canExecute.


Nested Class Summary
static interface AbstractCommand.NonDirtying
          A marker interface implemented by commands that don't dirty the model.
 
Field Summary
protected  java.lang.String description
          Holds a short textual description of the command as returned by getDescription() and set by setDescription(java.lang.String).
protected  boolean isExecutable
          Keeps track of whether the command is executable.
protected  boolean isPrepared
          Keeps track of whether prepare needs to be called.
protected  java.lang.String label
          Holds the label of the command as returned by getLabel() and set by setLabel(java.lang.String).
 
Constructor Summary
protected AbstractCommand()
          Creates an empty instance.
protected AbstractCommand(java.lang.String label)
          Creates an instance with the given label.
protected AbstractCommand(java.lang.String label, java.lang.String description)
          Creates and instance with the given label and description.
 
Method Summary
 boolean canExecute()
          Calls prepare(), caches the result in isExecutable, and sets isPrepared to true; from then on, it will yield the value of isExecutable.
 boolean canUndo()
          Returns true because most command should be undoable.
 Command chain(Command command)
          Creates a new compound command, containing this command and the given command, that delegates chain to CompoundCommand.append(org.eclipse.emf.common.command.Command).
 void dispose()
          Called to indicate that the command will never be used again.
 java.util.Collection<?> getAffectedObjects()
          Returns an empty list.
 java.lang.String getDescription()
          Returns a string suitable to help describe the effect of this command.
 java.lang.String getLabel()
          Returns a string suitable to represent the label that identifies this command.
 java.util.Collection<?> getResult()
          Returns an empty list.
protected  boolean prepare()
          Called at most once in canExecute() to give the command an opportunity to ready itself for execution.
 void setDescription(java.lang.String description)
          Sets the description after construction.
 void setLabel(java.lang.String label)
          Sets the label after construction.
 java.lang.String toString()
          Returns an abbreviated name using this object's own class' name, without package qualification, followed by a space separated list of field:value pairs.
 void undo()
          Throws a runtime exception.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.eclipse.emf.common.command.Command
execute, redo
 

Field Detail

isPrepared

protected boolean isPrepared
Keeps track of whether prepare needs to be called. It is tested in canExecute() so that prepare() is called exactly once to ready the command for execution.


isExecutable

protected boolean isExecutable
Keeps track of whether the command is executable. It is set in canExecute() to the result of calling prepare().


description

protected java.lang.String description
Holds a short textual description of the command as returned by getDescription() and set by setDescription(java.lang.String).


label

protected java.lang.String label
Holds the label of the command as returned by getLabel() and set by setLabel(java.lang.String).

Constructor Detail

AbstractCommand

protected AbstractCommand()
Creates an empty instance.


AbstractCommand

protected AbstractCommand(java.lang.String label)
Creates an instance with the given label.

Parameters:
label - the label.

AbstractCommand

protected AbstractCommand(java.lang.String label,
                          java.lang.String description)
Creates and instance with the given label and description.

Parameters:
label - the label.
description - the description.
Method Detail

prepare

protected boolean prepare()
Called at most once in canExecute() to give the command an opportunity to ready itself for execution. The returned value is stored in canExecute(). In other words, you can override this method to initialize and to yield a cached value for the all subsequent calls to canExecute.

Returns:
whether the command is executable.

canExecute

public boolean canExecute()
Calls prepare(), caches the result in isExecutable, and sets isPrepared to true; from then on, it will yield the value of isExecutable.

Specified by:
canExecute in interface Command
Returns:
whether the command can execute.

canUndo

public boolean canUndo()
Returns true because most command should be undoable.

Specified by:
canUndo in interface Command
Returns:
true.

undo

public void undo()
Throws a runtime exception.

Specified by:
undo in interface Command
Throws:
java.lang.UnsupportedOperationException - always.

getResult

public java.util.Collection<?> getResult()
Returns an empty list.

Specified by:
getResult in interface Command
Returns:
an empty list.

getAffectedObjects

public java.util.Collection<?> getAffectedObjects()
Returns an empty list.

Specified by:
getAffectedObjects in interface Command
Returns:
an empty list.

getLabel

public java.lang.String getLabel()
Description copied from interface: Command
Returns a string suitable to represent the label that identifies this command.

Specified by:
getLabel in interface Command
Returns:
a string suitable to represent the label that identifies this command.

setLabel

public void setLabel(java.lang.String label)
Sets the label after construction.

Parameters:
label - the new label.

getDescription

public java.lang.String getDescription()
Description copied from interface: Command
Returns a string suitable to help describe the effect of this command.

Specified by:
getDescription in interface Command
Returns:
a string suitable to help describe the effect of this command.

setDescription

public void setDescription(java.lang.String description)
Sets the description after construction.

Parameters:
description - the new description.

chain

public Command chain(Command command)
Creates a new compound command, containing this command and the given command, that delegates chain to CompoundCommand.append(org.eclipse.emf.common.command.Command).

Specified by:
chain in interface Command
Parameters:
command - the command to chain with this one.
Returns:
a new chained compound command.

dispose

public void dispose()
Description copied from interface: Command
Called to indicate that the command will never be used again. Calling any other method after this one has undefined results.

Specified by:
dispose in interface Command

toString

public java.lang.String toString()
Returns an abbreviated name using this object's own class' name, without package qualification, followed by a space separated list of field:value pairs.

Overrides:
toString in class java.lang.Object
Returns:
string representation.

Copyright 2001-2006 IBM Corporation and others.
All Rights Reserved.