/// <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); }
/// <summary> /// Executes the command and return a token that can be used to undo it. /// </summary> /// <param name="parameter">The command parameter.</param> /// <param name="createActionItem">Indicates whether to create an action item in the action stack. This should be false in case of a Redo operation.</param> /// <returns>An <see cref="UndoToken"/> that can be used to undo the command.</returns> public UndoToken ExecuteCommand(object parameter, bool createActionItem) { // TODO: Improve this - we're discarding any change made directly by the command invoke and create a CommandActionItem after. // NOTE: PickupAssetCommand is currently assuming that there's such a transaction in progress, be sure to check it if changing this. var transactionalActionStack = ActionStack as ITransactionalActionStack; if (transactionalActionStack != null) { transactionalActionStack.BeginTransaction(); } UndoToken token = Redo(parameter, createActionItem); if (transactionalActionStack != null) { transactionalActionStack.DiscardTransaction(); } if (token.CanUndo && createActionItem) { var actionItem = new CommandActionItem(this, parameter, token, Dirtiables); ActionStack.Add(actionItem); } return(token); }
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); }
/// <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 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); }
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))); }
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 dictionaryDescriptor = (DictionaryDescriptor)TypeDescriptorFactory.Default.Find(currentValue.GetType()); var key = undoToken.TokenValue; dictionaryDescriptor.Remove(currentValue, key); return(currentValue); }
/// <inheritdoc/> public object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken) { var dictionaryDescriptor = (DictionaryDescriptor)descriptor; var key = undoToken.TokenValue; dictionaryDescriptor.Remove(currentValue, key); return(currentValue); }
/// <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); }
/// <inheritdoc/> public object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken) { var index = (int)undoToken.TokenValue; var collectionDescriptor = (CollectionDescriptor)descriptor; collectionDescriptor.RemoveAt(currentValue, index); return(currentValue); }
/// <inheritdoc/> protected override void Undo(object parameter, UndoToken token) { if (undo == null) { throw new InvalidOperationException("This command cannot be cancelled."); } undo(parameter, token); }
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 object Invoke(object currentValue, ITypeDescriptor descriptor, object parameter, out UndoToken undoToken) { var dictionaryDescriptor = (DictionaryDescriptor)descriptor; var newKey = dictionaryDescriptor.KeyType != typeof(string) ? Activator.CreateInstance(dictionaryDescriptor.KeyType) : GenerateStringKey(currentValue, descriptor, parameter); var newItem = !dictionaryDescriptor.ValueType.IsAbstract ? Activator.CreateInstance(dictionaryDescriptor.ValueType) : null; dictionaryDescriptor.SetValue(currentValue, newKey, newItem); undoToken = new UndoToken(true, newKey); return(currentValue); }
protected override void Undo(object parameter, UndoToken token) { var viewModelNode = nodePath.GetNode(); if (viewModelNode == null) { throw new InvalidOperationException("Unable to retrieve the node on which to apply the redo operation."); } var newValue = nodeCommand.Undo(viewModelNode.Content.Value, viewModelNode.Content.Descriptor, token); Refresh(viewModelNode, newValue); }
/// <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); }
public override Edge Apply(ref IState state, Operation op) { Edge e = new Edge(Global.STEPCOST, op, op); UndoToken u = new UndoToken(); u.H = state.H; u.Op = op; e.UndoToken = u; Array.Reverse(state.Arr, 0, op.Vals[0] + 1); if (heuristic != null) { state.H = heuristic.H(state); } return(e); }
/// <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); }
/// <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; } }
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); }
/// <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); }
/// <inheritdoc/> public object Invoke(object currentValue, ITypeDescriptor descriptor, object parameter, out UndoToken undoToken) { var collectionDescriptor = (CollectionDescriptor)descriptor; if (collectionDescriptor.ElementType.IsAbstract || collectionDescriptor.ElementType.IsNullable()) { 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 = Activator.CreateInstance(collectionDescriptor.ElementType); undoToken = new UndoToken(true, collectionDescriptor.GetCollectionCount(currentValue)); collectionDescriptor.Add(currentValue, parameter ?? newItem); } return(currentValue); }
public override Edge Apply(ref IState state, Operation op) { Edge e = new Edge(Global.STEPCOST, op, state.Op); UndoToken u = new UndoToken(); u.H = state.H; u.Op = state.Op; e.UndoToken = u; byte tile = state.Arr[op.Vals[0]]; state.Arr[state.Op.Vals[0]] = tile; state.Op = op; state.Arr[op.Vals[0]] = 0; if (heuristic != null) { state.H = heuristic.H(state); } return(e); }
/// <summary> /// Executes the command and return a token that can be used to undo it. /// </summary> /// <param name="parameter">The command parameter.</param> /// <param name="createActionItem">Indicates whether to create an action item in the action stack. This should be false in case of a Redo operation.</param> /// <returns>An <see cref="UndoToken"/> that can be used to undo the command.</returns> public UndoToken ExecuteCommand(object parameter, bool createActionItem) { // TODO: Improve this - we're discarding any change made directly by the command invoke and create a CommandActionItem after. var transactionalActionStack = actionStack as ITransactionalActionStack; if (transactionalActionStack != null) { transactionalActionStack.BeginTransaction(); } UndoToken token = Redo(parameter, createActionItem); if (transactionalActionStack != null) { transactionalActionStack.DiscardTransaction(); } if (token.CanUndo && createActionItem) { var actionItem = new CommandActionItem(this, parameter, token, dirtiables); actionStack.Add(actionItem); } return(token); }
/// <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);
/// <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); }
public ModelNodeToken(UndoToken token, UndoToken additionalToken) { Token = token; AdditionalToken = additionalToken; }
/// <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)); }
/// <inheritdoc/> public abstract object Undo(object currentValue, UndoToken undoToken);
/// <inheritdoc/> public abstract object Invoke(object currentValue, object parameter, out UndoToken undoToken);