/// <inheritdoc/> protected override void Undo(object parameter, UndoToken token) { if (undo == null) throw new InvalidOperationException("This command cannot be cancelled."); undo(parameter, token); }
/// <inheritdoc/> public override object Invoke(object currentValue, object parameter, out UndoToken undoToken) { var collectionDescriptor = (CollectionDescriptor)TypeDescriptorFactory.Default.Find(currentValue.GetType()); // TODO: Find a better solution for ContentSerializerAttribute that doesn't require to reference Core.Serialization (and unreference this assembly) if (collectionDescriptor.ElementType.IsAbstract || collectionDescriptor.ElementType.IsNullable() || collectionDescriptor.ElementType.GetCustomAttributes(typeof(ContentSerializerAttribute), true).Any()) { // If the parameter is a type instead of an instance, try to construct an instance of this type var type = parameter as Type; if (type != null && type.GetConstructor(Type.EmptyTypes) != null) parameter = Activator.CreateInstance(type); undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue)); collectionDescriptor.Add(currentValue, parameter); } else if (collectionDescriptor.ElementType == typeof(string)) { undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue)); collectionDescriptor.Add(currentValue, parameter ?? ""); } else { var newItem = ObjectFactory.NewInstance(collectionDescriptor.ElementType); undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue)); collectionDescriptor.Add(currentValue, parameter ?? newItem); } return currentValue; }
/// <inheritdoc/> public override object Undo(object currentValue, UndoToken undoToken) { var dictionaryDescriptor = (DictionaryDescriptor)TypeDescriptorFactory.Default.Find(currentValue.GetType()); var key = undoToken.TokenValue; dictionaryDescriptor.Remove(currentValue, key); return currentValue; }
public CommandActionItem(CancellableCommand command, object parameter, UndoToken undoToken, IEnumerable<IDirtiableViewModel> dirtiables) : base("Executing " + command.Name, dirtiables) { this.command = command; this.parameter = parameter; this.undoToken = undoToken; }
/// <inheritdoc/> public override object Undo(object currentValue, UndoToken undoToken) { var index = (int)undoToken.TokenValue; var collectionDescriptor = (CollectionDescriptor)TypeDescriptorFactory.Default.Find(currentValue.GetType()); collectionDescriptor.RemoveAt(currentValue, index); return currentValue; }
protected override void Undo(object parameter, UndoToken token) { var undoTokens = (Dictionary<ModelNodeCommandWrapper, UndoToken>)token.TokenValue; foreach (var command in commands) { command.UndoCommand(parameter, undoTokens[command]); } Refresh(); }
/// <inheritdoc/> public override object Invoke(object currentValue, object parameter, out UndoToken undoToken) { var dictionaryDescriptor = (DictionaryDescriptor)TypeDescriptorFactory.Default.Find(currentValue.GetType()); var newKey = dictionaryDescriptor.KeyType != typeof(string) ? Activator.CreateInstance(dictionaryDescriptor.KeyType) : GenerateStringKey(currentValue, dictionaryDescriptor, parameter); object newItem = null; // TODO: Find a better solution that doesn't require to reference Core.Serialization (and unreference this assembly) if (!dictionaryDescriptor.ValueType.GetCustomAttributes(typeof(ContentSerializerAttribute), true).Any()) newItem = !dictionaryDescriptor.ValueType.IsAbstract ? Activator.CreateInstance(dictionaryDescriptor.ValueType) : null; dictionaryDescriptor.SetValue(currentValue, newKey, newItem); undoToken = new UndoToken(true, newKey); return currentValue; }
/// <inheritdoc/> public override object Invoke(object currentValue, object parameter, out UndoToken undoToken) { var dictionaryDescriptor = TypeDescriptorFactory.Default.Find(currentValue.GetType()) as DictionaryDescriptor; var tuple = parameter as Tuple<object, object>; if (dictionaryDescriptor == null || tuple == null) throw new InvalidOperationException("This command cannot be executed on the given object."); var removedObject = dictionaryDescriptor.GetValue(currentValue, tuple.Item1); undoToken = new UndoToken(true, new UndoTokenData(tuple.Item1, tuple.Item2)); dictionaryDescriptor.Remove(currentValue, tuple.Item1); dictionaryDescriptor.SetValue(currentValue, tuple.Item2, removedObject); return currentValue; }
protected override void Undo(object parameter, UndoToken token) { object index; var modelNode = NodePath.GetSourceNode(out index); if (modelNode == null) throw new InvalidOperationException("Unable to retrieve the node on which to apply the undo operation."); var modelNodeToken = (ModelNodeToken)token.TokenValue; var currentValue = modelNode.GetValue(index); var newValue = NodeCommand.Undo(currentValue, modelNodeToken.Token); modelNode.SetValue(newValue, index); Refresh(modelNode, index); AdditionalCommand?.UndoCommand(null, modelNodeToken.AdditionalToken); }
/// <summary> /// Undoes the execution of this command. /// </summary> /// <param name="parameter">The parameter used to invoke the command.</param> /// <param name="token">The <see cref="UndoToken"/> generated by the execution of this command.</param> public void UndoCommand(object parameter, UndoToken token) { if (!AllowReentrancy) { if (currentlyRunning) return; currentlyRunning = true; } Undo(parameter, token); if (!AllowReentrancy) { currentlyRunning = false; } }
/// <inheritdoc/> public override object Undo(object currentValue, UndoToken undoToken) { var dictionaryDescriptor = TypeDescriptorFactory.Default.Find(currentValue.GetType()) as DictionaryDescriptor; var undoData = (UndoTokenData)undoToken.TokenValue; if (dictionaryDescriptor == null) throw new InvalidOperationException("This command cannot be cancelled on the given object."); if (dictionaryDescriptor.ContainsKey(currentValue, undoData.PreviousIndex)) throw new InvalidOperationException("Unable to undo remove: the dictionary contains the key to re-add."); var removedObject = dictionaryDescriptor.GetValue(currentValue, undoData.NewIndex); dictionaryDescriptor.Remove(currentValue, undoData.NewIndex); dictionaryDescriptor.SetValue(currentValue, undoData.PreviousIndex, removedObject); return currentValue; }
protected override UndoToken Redo(object parameter, bool creatingActionItem) { UndoToken token; object index; var modelNode = NodePath.GetSourceNode(out index); if (modelNode == null) throw new InvalidOperationException("Unable to retrieve the node on which to apply the redo operation."); var currentValue = modelNode.GetValue(index); var newValue = NodeCommand.Invoke(currentValue, parameter, out token); modelNode.SetValue(newValue, index); Refresh(modelNode, index); var additionalToken = new UndoToken(); if (AdditionalCommand != null) { additionalToken = AdditionalCommand.ExecuteCommand(null, false); } return new UndoToken(token.CanUndo, new ModelNodeToken(token, additionalToken)); }
/// <inheritdoc/> public override object Undo(object currentValue, UndoToken undoToken) { var descriptor = TypeDescriptorFactory.Default.Find(currentValue.GetType()); var collectionDescriptor = descriptor as CollectionDescriptor; var dictionaryDescriptor = descriptor as DictionaryDescriptor; var undoData = (UndoTokenData)undoToken.TokenValue; if (collectionDescriptor != null) { var position = (int)undoData.Indexer; if (collectionDescriptor.HasInsert) collectionDescriptor.Insert(currentValue, position, undoData.Item); else collectionDescriptor.Add(currentValue, undoData.Item); } else if (dictionaryDescriptor != null) { if (dictionaryDescriptor.ContainsKey(currentValue, undoData.Indexer)) throw new InvalidOperationException("Unable to undo remove: the dictionary contains the key to re-add."); dictionaryDescriptor.SetValue(currentValue, undoData.Indexer, undoData.Item); } return currentValue; }
/// <inheritdoc/> public override object Invoke(object currentValue, object parameter, out UndoToken undoToken) { var descriptor = TypeDescriptorFactory.Default.Find(currentValue.GetType()); var collectionDescriptor = descriptor as CollectionDescriptor; var dictionaryDescriptor = descriptor as DictionaryDescriptor; if (collectionDescriptor != null) { var position = (int)parameter; var removedObject = collectionDescriptor.GetValue(currentValue, position); undoToken = new UndoToken(true, new UndoTokenData(parameter, removedObject)); collectionDescriptor.RemoveAt(currentValue, position); } else if (dictionaryDescriptor != null) { var removedObject = dictionaryDescriptor.GetValue(currentValue, parameter); undoToken = new UndoToken(true, new UndoTokenData(parameter, removedObject)); dictionaryDescriptor.Remove(currentValue, parameter); } else throw new InvalidOperationException("This command cannot be executed on the given object."); return currentValue; }
public ModelNodeToken(UndoToken token, UndoToken additionalToken) { Token = token; AdditionalToken = additionalToken; }
/// <summary> /// Undoes the execution of the command. /// </summary> /// <param name="parameter">The command parameter.</param> /// <param name="token">The <see cref="UndoToken"/> generated by the execution of this command.</param> protected abstract void Undo(object parameter, UndoToken token);
/// <inheritdoc/> public abstract object Undo(object currentValue, UndoToken undoToken);
/// <summary> /// Redoes the node command. The default implementation simply calls the <see cref="Invoke"/> method. /// </summary> /// <param name="currentValue">The current value of the associated object or member.</param> /// <param name="parameter">The parameter of the command.</param> /// <param name="undoToken">The <see cref="UndoToken"/> that will be passed to the <see cref="Undo"/> method when undoing the execution of this command.</param> /// <returns>The new value to assign to the associated object or member.</returns> public virtual object Redo(object currentValue, object parameter, out UndoToken undoToken) { return Invoke(currentValue, parameter, out undoToken); }
/// <summary> /// Undoes the execution of this command. /// </summary> /// <param name="parameter">The parameter used to invoke the command.</param> /// <param name="token">The <see cref="UndoToken"/> generated by the execution of this command.</param> public void UndoCommand(object parameter, UndoToken token) { Undo(parameter, token); }
protected override void FreezeMembers() { command = null; parameter = null; undoToken = default(UndoToken); }
public sealed override object Undo(object currentValue, UndoToken undoToken) { return undoToken.TokenValue; }
public sealed override object Undo(object currentValue, UndoToken undoToken) { throw new NotSupportedException("This command is not cancellable."); }
public sealed override object Redo(object currentValue, object parameter, out UndoToken undoToken) { var newValue = ChangeValue(currentValue, parameter, true); undoToken = !Equals(newValue, currentValue) ? new UndoToken(true, currentValue) : new UndoToken(false); return newValue; }
public sealed override object Invoke(object currentValue, object parameter, out UndoToken undoToken) { undoToken = new UndoToken(false); InvokeUncancellable(currentValue, parameter); return currentValue; }
/// <inheritdoc/> public abstract object Invoke(object currentValue, object parameter, out UndoToken undoToken);