public abstract class AbstractUndoUnit extends Object implements IUndoUnit
IUndoUnit
.
This class defaults most methods required by the IUndoUnit
interface. Subclasses only have to implement the
undo/redo logic. Most notably, IUndoUnit.tryMergeUnit(IUndoUnit)
and IUndoUnit.tryReplaceUnit(IUndoUnit)
are implemented to return
false
. Subclasses are of course allowed to override more methods if clients want to enable additional
functionality.
Use this class for simple custom implementations of IUndoUnit
.
IUndoUnit
,
UndoEngine
Modifier | Constructor and Description |
---|---|
protected |
AbstractUndoUnit(String undoName)
Creates a new instance using the given name as undo and redo name.
|
protected |
AbstractUndoUnit(String undoName,
String redoName)
Creates a new instance using the given name as undo and redo name.
|
Modifier and Type | Method and Description |
---|---|
void |
dispose()
Frees resources used by this Disposable on demand.
|
String |
getRedoName()
Returns the name of the redo unit.
|
String |
getUndoName()
Returns the name of the undo unit.
|
abstract void |
redo()
Redoes the work that is represented by this unit.
|
String |
toString() |
boolean |
tryMergeUnit(IUndoUnit unit)
Tries to merge the given unit with this one.
|
boolean |
tryReplaceUnit(IUndoUnit unit)
Tries to replace the given unit with this one.
|
abstract void |
undo()
Undoes the work that is represented by this unit.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
close
protected AbstractUndoUnit(String undoName)
undoName
- The undo name.public void dispose()
IDisposable
dispose
in interface IDisposable
public String getRedoName()
IUndoUnit
Depending on the implementation and context this might be a human readable representation of the redo action or a symbolic name that needs localization.
getRedoName
in interface IUndoUnit
public final String getUndoName()
IUndoUnit
Depending on the implementation and context this might be a human readable representation of the undo action or a symbolic name that needs localization.
getUndoName
in interface IUndoUnit
public abstract void redo()
IUndoUnit
Undo/redo are sensible operations and should maintain a consistent state before and after each operation since they may be executed potentially often one after another.
redo
in interface IUndoUnit
IUndoUnit.getRedoName()
public boolean tryMergeUnit(IUndoUnit unit)
IUndoUnit
This method is called when this
unit is the head of an UndoEngine
's or
CompositeUndoUnit
's queue and a new unit is added. It is meant to try to incorporate
the change of the given unit
into this
and if successful return true
. This should be the case
when the end state of this
unit is equal to the start state of the given unit
.
For example, if this
unit is the head of the queue and represents the work from state A
to state B
and the given unit the work from state B
to state C
, then this method should try to make this
unit shift from state A
to state C
. If this is successful, the method is expected to return
true
. The other unit is then disposed
by the UndoEngine
afterwards.
Clients don't necessarily have to implement this method if the unit doesn't happen very often. In fact, the default
implementation of tryMergeUnit(IUndoUnit)
simply returns false
.
Implementing this method faithfully will cause multiple units of work to inseparably appear as one and undoing/redoing
it will undo/redo the work of all collapsed units. Depending on the situation this may be reasonable, for example when
there are potentially a lot of changes where not every intermediate step is required to be recorded. In this case
implementing this method faithfully will greatly improve the performance and reduce the required amount of memory of the
undo process. If you want to group together multiple units as a single block but still want to be able to separate each
step, consider to use a CompositeUndoUnit
instead.
tryMergeUnit
in interface IUndoUnit
unit
- The unit to incorporate that happened after this unit.unit
has been incorporated into this unit and unit
can be disposed of.public boolean tryReplaceUnit(IUndoUnit unit)
IUndoUnit
This method is called if a newly added unit couldn't be merged
with the head unit of
the queue (i.e. returned false). Instead of trying to merge a newly added unit, this method tries to replace the given
unit (which is the head of the queue) with this
by incorporating the change of the given unit
into this
and if successful return true
. This should be the case when the start state of this
unit is equal to the
end state of the given unit
.
For example, if the given unit
represents the work from state A
to state B
and this
unit
the work from state B
to state C
, then this method should try to make this
unit shift from state
A
to state C
. If this is successful, the method is expected to return true
. The given unit
is then disposed
by the UndoEngine
afterwards and replaced
with this
unit in the queue.
Clients don't necessarily have to implement this method if the unit doesn't happen very often. In fact, the default
implementation of tryReplaceUnit(IUndoUnit)
simply returns
false
. Implementing this method faithfully will cause multiple units of work to inseparably appear as one
and undoing/redoing it will undo/redo the work of all collapsed units. Depending on the situation this may be
reasonable, for example when there are potentially a lot of changes where not every intermediate step is required to be
recorded. In this case implementing this method faithfully will greatly improve the performance and reduce the required
amount of memory of the undo process. If you want to group together multiple units as a single block but still want to
be able to separate each step, consider to use a CompositeUndoUnit
instead.
tryReplaceUnit
in interface IUndoUnit
unit
- The unit to incorporate that happened before this unit.this
unit has been incorporated into the given unit
and this
can be
disposed of.public abstract void undo()
IUndoUnit
Undo/redo are sensible operations and should maintain a consistent state before and after each operation since they may be executed potentially often one after another.
undo
in interface IUndoUnit
IUndoUnit.getUndoName()