Class UndoManager
- Namespace
- StockSharp.Xaml.Diagram.GXDiagram.Model
- Assembly
- StockSharp.Xaml.Diagram.dll
The UndoManager class observes and records model changes and supports undo and redo operations.
public class UndoManager : IUndoManager
- Inheritance
-
UndoManager
- Implements
- Inherited Members
- Extension Methods
Constructors
UndoManager()
Create a UndoManager that is ready to record model modifications, but that does not know about any models yet.
public UndoManager()
Properties
ChecksTransactionLevel
Gets or sets whether this undo manager will output warnings to Trace listeners when model changes occur outside of a transaction.
public bool ChecksTransactionLevel { get; set; }
Property Value
- bool
This defaults to false.
CompoundEdits
Gets a list of all of the compound edits.
public IList<IUndoableEdit> CompoundEdits { get; }
Property Value
- IList<IUndoableEdit>
This will be a
List
of mostly UndoManager.CompoundEdits
CurrentEdit
Gets the current compound edit for recording additional model change events.
public UndoManager.CompoundEdit CurrentEdit { get; set; }
Property Value
Remarks
This is initialized and augmented by HandleModelChanged(object, ModelChangedEventArgs) before it is added to CompoundEdits by a top-level call to CommitTransaction(string).
CurrentTransactionName
Gets the current transaction name given by StartTransaction(string).
public string CurrentTransactionName { get; }
Property Value
- string
If no transaction is ongoing, this will be an empty string.
EditToRedo
Gets the current StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit to be redone, or null if there is none.
public IUndoableEdit EditToRedo { get; }
Property Value
- See Also
EditToUndo
Gets the current StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit to be undone, or null if there is none.
public IUndoableEdit EditToUndo { get; }
Property Value
- See Also
IsUndoingRedoing
public bool IsUndoingRedoing { get; protected set; }
Property Value
Remarks
When this property is true, CanUndo() and CanRedo() will be false. To avoid confusion, HandleModelChanged(object, ModelChangedEventArgs) ignores all model change events when this property is true.
MaximumEditCount
Gets or sets the maximum number of compound edits that this undo manager will remember.
public int MaximumEditCount { get; set; }
Property Value
- int
If the value is negative, no limit is assumed. A new value of zero is treated as if the new value were one. The initial value is 999.
Remarks
This property is useful in helping limit the memory consumption of typical application usage. But note that this only limits the number of compound edits, not the size of any individual UndoManager.CompoundEdit, which may still have an unlimited number of ModelChangedEventArgss.
Decreasing this value will not necessarily remove any existing edits if there currently exist more edits in CompoundEdits than the new value would allow.
Models
Gets a list of models for which this UndoManager is recording undo/redo information.
public IEnumerable<IDiagramModel> Models { get; }
Property Value
Remarks
You can manipulate this list explicitly by calling AddModel(IDiagramModel) and RemoveModel(IDiagramModel). Setting UndoManager automatically calls these methods. Undo() and Redo() use this list to call RaiseChanged(ModelChangedEventArgs) with notices about starting and ending undo and redo actions, and about starting/finishing/rollingback edits.
NestedTransactionNames
Gets a stack of ongoing transaction names.
public IList<string> NestedTransactionNames { get; }
Property Value
Remarks
The outermost transaction name will be the first item in the list. The last one will be the name of the most recent (nested) call to StartTransaction(string).
TransactionLevel
Gets the current transaction level.
public int TransactionLevel { get; protected set; }
Property Value
- int
This value is zero when no transaction is in progress. The initial value is zero. StartTransaction(string) will increment this value; EndTransaction(bool, string) will decrement it. When this value is greater than zero, CanUndo() and CanRedo() will be false, because additional logically related model change events may occur.
UndoEditIndex
Gets the index into CompoundEdits for the current undoable edit.
public int UndoEditIndex { get; protected set; }
Property Value
- int
-1 if there's no undoable edit to be undone.
Methods
AddModel(IDiagramModel)
Make sure this undo manager knows about a IDiagramModel for which it is receiving model Changed event notifications.
public virtual void AddModel(IDiagramModel model)
Parameters
model
IDiagramModel
Remarks
This just adds model
to the list of Models.
- See Also
CanRedo()
This predicate is true when one can call Redo().
public virtual bool CanRedo()
Returns
Remarks
In order to be able to perform a redo, a transaction must not be in progress, nor an undo or a redo. Furthermore there must be an EditToRedo that itself is ready to be redone, because its StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.CanRedo predicate is true.
- See Also
CanUndo()
This predicate is true when one can call Undo().
public virtual bool CanUndo()
Returns
Remarks
In order to be able to perform an undo, a transaction must not be in progress, nor an undo or a redo. Furthermore there must be an EditToUndo that itself is ready to be undone, because its StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.CanUndo predicate is true.
- See Also
Clear()
Clear all of the UndoManager.CompoundEdits and reset all other state.
public virtual void Clear()
Remarks
However, this does not forget the models that this undo manager is managing.
CommitCompoundEdit(CompoundEdit)
This method is called by EndTransaction(bool, string) when committing a compound edit.
protected virtual UndoManager.CompoundEdit CommitCompoundEdit(UndoManager.CompoundEdit cedit)
Parameters
cedit
UndoManager.CompoundEditthe CurrentEdit; this may be null if there had been no changes at commit time
Returns
- UndoManager.CompoundEdit
By default, the unmodified
cedit
.
Remarks
You may wish to override this method in order to perform optimizations, such as removing duplicate or unnecessary ModelChangedEventArgss.
CommitTransaction(string)
Just call EndTransaction(bool, string), committing the current transaction, with the presentation name for the transaction.
public bool CommitTransaction(string tname)
Parameters
tname
stringthe transaction name
Returns
- bool
the value of the call to EndTransaction(bool, string)
EndTransaction(bool, string)
Stop the current transaction, either rolling it back or committing it.
protected virtual bool EndTransaction(bool commit, string tname)
Parameters
commit
booltrue to terminate the transaction normally; false to abort it and rollback the existing edits
tname
stringthe internal locale-neutral name for the transaction
Returns
- bool
true for a committed top-level transaction
Remarks
If this call stops a top-level transaction, a value of false for
commit
just undoes and discards the information in the
CurrentEdit.
If commit
is true for a top-level transaction,
we mark the CurrentEdit complete,
call CommitCompoundEdit(CompoundEdit),
and add the resulting UndoManager.CompoundEdit
to the list of compound edits that this undo manager is recording.
Committing a transaction when there have been some undos without corresponding redos will throw away the compound edits holding changes that happened after the current state, before adding this new compound edit to the undo manager's list of edits.
This method raises a Changed event
for each of this undo manager's Models,
with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.CommittedTransaction,
and with a OldValue
that is the UndoManager.CompoundEdit
that has been added to the list of CompoundEdits.
Similarly, if the transaction is aborted, either because commit
is false or because there is no CurrentEdit to commit,
all of the Models get a StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.RolledBackTransaction
Changed event. The values passed in the ModelChangedEventArgs
may all be null, however.
FindPrimaryObject(IUndoableEdit)
Given an StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit return an edited object that represents what was modified.
public virtual object FindPrimaryObject(IUndoableEdit edit)
Parameters
edit
IUndoableEditan StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit, usually either a ModelChangedEventArgs or a UndoManager.CompoundEdit
Returns
- object
typically a Node or a Link, but this may be null if there is no such object, perhaps because a model property was modified, or because there were no real edits in the argument
edit
.
HandleModelChanged(object, ModelChangedEventArgs)
Create an StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit for a Changed event.
public virtual void HandleModelChanged(object sender, ModelChangedEventArgs e)
Parameters
sender
objecte
ModelChangedEventArgs
Remarks
This calls SkipEvent(ModelChangedEventArgs) if for some reason we should ignore
the e
.
This then creates a ModelChangedEventArgs and adds it to the
CurrentEdit, a UndoManager.CompoundEdit which it allocates
if needed.
This method always ignores all Changed events while we are performing an
Undo() or Redo().
Redo()
Restore the state of some models to after the current StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.
public virtual void Redo()
Remarks
This calls StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.Redo on the current EditToRedo. This will raise a Changed event with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.StartingRedo before actually performing the redo, and will raise a Changed event with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.FinishedRedo afterwards. The Data is the UndoManager.CompoundEdit that was the value of EditToRedo before calling Redo.
- See Also
RemoveModel(IDiagramModel)
Call this method to inform this undo manager that it no longer will be notified of model Changed events.
public virtual void RemoveModel(IDiagramModel model)
Parameters
model
IDiagramModel
Remarks
This just removes model
from the list of Models.
- See Also
RollbackTransaction()
Just call EndTransaction(bool, string), rolling back the current transaction.
public bool RollbackTransaction()
Returns
- bool
the value of the call to EndTransaction(bool, string)
SkipEvent(ModelChangedEventArgs)
This predicate is responsible for deciding if a ModelChangedEventArgs is not interesting enough to be recorded.
protected virtual bool SkipEvent(ModelChangedEventArgs evt)
Parameters
Returns
- bool
normally false, which causes the given event to be remembered; but true for negative valued enumerations of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.
StartTransaction(string)
Begin a transaction, where the changes are held by a UndoManager.CompoundEdit.
public virtual bool StartTransaction(string tname)
Parameters
tname
string
Returns
- bool
true if starting a top-level transaction
Remarks
Transactions can be nested:
Nested transactions will share the same compound edit as the top-level one. This will raise a Changed event for each of the Models, with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.StartedTransaction.
- See Also
ToString()
For debugging.
public override string ToString()
Returns
Undo()
Restore the state of some models to before the current StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.
public virtual void Undo()
Remarks
This calls StockSharp.Xaml.Diagram.GXDiagram.Model.IUndoableEdit.Undo on the current EditToUndo. This will raise a Changed event with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.StartingUndo before actually performing the undo, and will raise a Changed event with a hint of StockSharp.Xaml.Diagram.GXDiagram.Model.ModelChange.FinishedUndo afterwards. The Data is the UndoManager.CompoundEdit that was the value of EditToUndo before calling Undo.
- See Also