Package icyllis.modernui.core
Class UndoManager
java.lang.Object
icyllis.modernui.core.UndoManager
-
Field Summary
Modifier and TypeFieldDescriptionstatic final int
Always allow merge with the last undo state, if possible.static final int
Never merge with the last undo state.static final int
Allow merge with the last undo state only if it contains operations with the caller's owner. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addOperation
(UndoOperation<?> op, int mergeMode) Add a new UndoOperation to the current update.void
beginUpdate
(CharSequence label) Start creating a new undo state.int
commitState
(UndoOwner owner) Commit the last finished undo state.int
countRedos
(UndoOwner[] owners) Return the number of redo states on the undo stack.int
countUndos
(UndoOwner[] owners) Return the number of undo states on the undo stack.void
Finish the creation of an undo state, matching a previous call tobeginUpdate(java.lang.CharSequence)
.int
forgetRedos
(UndoOwner[] owners, int count) int
forgetUndos
(UndoOwner[] owners, int count) int
Return the current maximum number of undo states.getLastOperation
(int mergeMode) Return the most recentUndoOperation
that was added to the update.getLastOperation
(UndoOwner owner, int mergeMode) Return the most recentUndoOperation
that was added to the update and has the given owner.<T extends UndoOperation<?>>
TgetLastOperation
(Class<T> clazz, UndoOwner owner, int mergeMode) Return the most recentUndoOperation
that was added to the update and has the given owner.getRedoLabel
(UndoOwner[] owners) Return the user-visible label for the top redo state on the stack.getUndoLabel
(UndoOwner[] owners) Return the user-visible label for the top undo state on the stack.int
Return the number of timesbeginUpdate(java.lang.CharSequence)
has been called without a matchingendUpdate()
call.boolean
hasOperation
(UndoOwner owner) Check whether there is anUndoOperation
in the currentbeginUpdate(java.lang.CharSequence)
undo state.boolean
isInUndo()
Returns true if we are currently inside of an undo/redo operation.boolean
Returns true if currently inside of abeginUpdate(java.lang.CharSequence)
.int
Perform redo of last/top count undo states in the transient redo stack.void
restoreInstanceState
(Parcel p, ClassLoader loader) Restore an undo state previously created withsaveInstanceState(Parcel)
.void
Flatten the current undo state into a Parcel object, which can later be restored withrestoreInstanceState(Parcel, java.lang.ClassLoader)
.void
setHistorySize
(int size) Set the maximum number of undo states that will be retained.void
setUndoLabel
(CharSequence label) Forcibly set a new for the new undo state being built within abeginUpdate(java.lang.CharSequence)
.void
suggestUndoLabel
(CharSequence label) Set a new for the new undo state being built within abeginUpdate(java.lang.CharSequence)
, but only if there is not a label currently set for it.boolean
uncommitState
(int commitId, UndoOwner owner) Attempt to undo a previous call tocommitState(icyllis.modernui.core.UndoOwner)
.int
Perform undo of last/top count undo states.
-
Field Details
-
MERGE_MODE_NONE
public static final int MERGE_MODE_NONENever merge with the last undo state.- See Also:
-
MERGE_MODE_UNIQUE
public static final int MERGE_MODE_UNIQUEAllow merge with the last undo state only if it contains operations with the caller's owner.- See Also:
-
MERGE_MODE_ANY
public static final int MERGE_MODE_ANYAlways allow merge with the last undo state, if possible.- See Also:
-
-
Constructor Details
-
UndoManager
public UndoManager()
-
-
Method Details
-
getOwner
-
saveInstanceState
Flatten the current undo state into a Parcel object, which can later be restored withrestoreInstanceState(Parcel, java.lang.ClassLoader)
. -
restoreInstanceState
Restore an undo state previously created withsaveInstanceState(Parcel)
. This will restore the UndoManager's state to almost exactly what it was at the point it had been previously saved; the only information not restored is the data object associated with eachUndoOwner
, which requires separate calls togetOwner(String, Object)
to re-associate the owner with its data. -
setHistorySize
public void setHistorySize(int size) Set the maximum number of undo states that will be retained. -
getHistorySize
public int getHistorySize()Return the current maximum number of undo states. -
undo
Perform undo of last/top count undo states. The states impacted by this can be limited through owners.- Parameters:
owners
- Optional set of owners that should be impacted. If null, all undo states will be visible and available for undo. If non-null, only those states that contain one of the owners specified here will be visible.count
- Number of undo states to pop.- Returns:
- Returns the number of undo states that were actually popped.
-
redo
Perform redo of last/top count undo states in the transient redo stack. The states impacted by this can be limited through owners.- Parameters:
owners
- Optional set of owners that should be impacted. If null, all undo states will be visible and available for undo. If non-null, only those states that contain one of the owners specified here will be visible.count
- Number of undo states to pop.- Returns:
- Returns the number of undo states that were actually redone.
-
isInUndo
public boolean isInUndo()Returns true if we are currently inside of an undo/redo operation. This is useful for editors to know whether they should be generating new undo state when they see edit operations happening. -
forgetUndos
-
forgetRedos
-
countUndos
Return the number of undo states on the undo stack.- Parameters:
owners
- If non-null, only those states containing an operation with one of the owners supplied here will be counted.
-
countRedos
Return the number of redo states on the undo stack.- Parameters:
owners
- If non-null, only those states containing an operation with one of the owners supplied here will be counted.
-
getUndoLabel
Return the user-visible label for the top undo state on the stack.- Parameters:
owners
- If non-null, will select the top-most undo state containing an operation with one of the owners supplied here.
-
getRedoLabel
Return the user-visible label for the top redo state on the stack.- Parameters:
owners
- If non-null, will select the top-most undo state containing an operation with one of the owners supplied here.
-
beginUpdate
Start creating a new undo state. Multiple calls to this function will nest until they are all matched by a later call toendUpdate()
.- Parameters:
label
- Optional user-visible label for this new undo state.
-
isInUpdate
public boolean isInUpdate()Returns true if currently inside of abeginUpdate(java.lang.CharSequence)
. -
setUndoLabel
Forcibly set a new for the new undo state being built within abeginUpdate(java.lang.CharSequence)
. Any existing label will be replaced with this one. -
suggestUndoLabel
Set a new for the new undo state being built within abeginUpdate(java.lang.CharSequence)
, but only if there is not a label currently set for it. -
getUpdateNestingLevel
public int getUpdateNestingLevel()Return the number of timesbeginUpdate(java.lang.CharSequence)
has been called without a matchingendUpdate()
call. -
hasOperation
Check whether there is anUndoOperation
in the currentbeginUpdate(java.lang.CharSequence)
undo state.- Parameters:
owner
- Optional owner of the operation to look for. If null, will succeed if there is any operation; if non-null, will only succeed if there is an operation with the given owner.- Returns:
- Returns true if there is a matching operation in the current undo state.
-
getLastOperation
Return the most recentUndoOperation
that was added to the update.- Parameters:
mergeMode
- May be eitherMERGE_MODE_NONE
orMERGE_MODE_ANY
.
-
getLastOperation
Return the most recentUndoOperation
that was added to the update and has the given owner.- Parameters:
owner
- Optional owner of last operation to retrieve. If null, the last operation regardless of owner will be retrieved; if non-null, the last operation matching the given owner will be retrieved.mergeMode
- May be eitherMERGE_MODE_NONE
,MERGE_MODE_UNIQUE
, orMERGE_MODE_ANY
.
-
getLastOperation
public <T extends UndoOperation<?>> T getLastOperation(Class<T> clazz, UndoOwner owner, int mergeMode) Return the most recentUndoOperation
that was added to the update and has the given owner.- Parameters:
clazz
- Optional class of the last operation to retrieve. If null, the last operation regardless of class will be retrieved; if non-null, the last operation whose class is the same as the given class will be retrieved.owner
- Optional owner of last operation to retrieve. If null, the last operation regardless of owner will be retrieved; if non-null, the last operation matching the given owner will be retrieved.mergeMode
- May be eitherMERGE_MODE_NONE
,MERGE_MODE_UNIQUE
, orMERGE_MODE_ANY
.
-
addOperation
Add a new UndoOperation to the current update.- Parameters:
op
- The new operation to add.mergeMode
- May be eitherMERGE_MODE_NONE
,MERGE_MODE_UNIQUE
, orMERGE_MODE_ANY
.
-
endUpdate
public void endUpdate()Finish the creation of an undo state, matching a previous call tobeginUpdate(java.lang.CharSequence)
. -
commitState
Commit the last finished undo state. This undo state can no longer be modified with furtherMERGE_MODE_UNIQUE
orMERGE_MODE_ANY
merge modes. If called while inside of an update, this will push any changes in the current update on to the undo stack and result with a fresh undo state, behaving as ifendUpdate()
had been called enough to unwind the current update, then the last state committed, andbeginUpdate(java.lang.CharSequence)
called to restore the update nesting.- Parameters:
owner
- The optional owner to determine whether to perform the commit. If this is non-null, the commit will only execute if the current top undo state contains an operation with the given owner.- Returns:
- Returns an integer identifier for the committed undo state, which can later be used to try to uncommit the state to perform further edits on it.
-
uncommitState
Attempt to undo a previous call tocommitState(icyllis.modernui.core.UndoOwner)
. This will work if the undo state at the top of the stack has the given id, and has not been involved in an undo operation. Otherwise false is returned.- Parameters:
commitId
- The identifier for the state to be uncommitted, as returned bycommitState(icyllis.modernui.core.UndoOwner)
.owner
- Optional owner that must appear in the committed state.- Returns:
- Returns true if the uncommit is successful, else false.
-