public interface IMementoSupport
By implementing this interface as well as ILookup
, clients can add undoability for changes to their model
classes. The yFiles undo mechanism uses the return value of getState(Object)
to retrieve a state of an item at
the beginning of the compound editing process. When the process ends, another state will be retrieved of the same item
and compared to the original state. If they differ, an IUndoUnit
is created that uses the applyState(Object, Object)
method to apply either state to the item in case of undo
or redo
. This
represents an abstraction to the undo mechanism where it is only needed to define "states" of items and hides the more
complicated mechanism of creating and inserting IUndoUnit
s.
The following is an example implementation of an item that is being managed using IMementoSupport
:
A collection of items from this type can then be watched using the following code snippet:
Implementing the IMementoSupport
interface is quite unrestrained, the type of the state returned by getState(Object)
method can by anything as long as the applyState(Object, Object)
and stateEquals(Object, Object)
methods can deal with it:
In summary, use this concept when you want to track the state of items during certain operations for undo/redo. This is
efficient if it's easier to handle an item's state than the changes to the item themselves. If you want to focus on the
changes or on certain events, you should use custom IUndoUnit
implementations instead.
ILookup
,
ICompoundEdit
,
UndoEngine
Modifier and Type | Method and Description |
---|---|
void |
applyState(Object subject,
Object state)
Reapplies a previously queried state object to a given subject.
|
Object |
getState(Object subject)
Retrieves an object representing the state at the moment this method is called.
|
boolean |
stateEquals(Object state1,
Object state2)
Determines whether two state objects are equal.
|
void applyState(Object subject, Object state)
The state object has been created using the getState(Object)
method.
subject
- The subject to modify the state.state
- The state object as returned by getState(Object)
Object getState(Object subject)
The returned state may be reapplied later to the subject in the applyState(Object, Object)
method.
subject
- The subject to read the state fromsubject
.boolean stateEquals(Object state1, Object state2)
The undo mechanism in yFiles calls this method to determine if the subject has changed and whether an IUndoUnit
should be created for these changes. If this method returns false
, an IUndoUnit
is created for the two states that can reapply either state (for either undo or redo). If this method returns true
the state is considered not to have changed and no undo unit will be created. Conservative implementations my simply
return false
.
state1
- The first state as obtained from getState(Object)
state2
- The second state as obtained from getState(Object)
true
if the states are equal; false
otherwise.