Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        /// <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);
        }
Ejemplo n.º 5
0
        /// <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);
        }
Ejemplo n.º 6
0
        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)));
        }
Ejemplo n.º 7
0
 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;
 }
Ejemplo n.º 8
0
        /// <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);
        }
Ejemplo n.º 9
0
        /// <inheritdoc/>
        public object Undo(object currentValue, ITypeDescriptor descriptor, UndoToken undoToken)
        {
            var dictionaryDescriptor = (DictionaryDescriptor)descriptor;
            var key = undoToken.TokenValue;

            dictionaryDescriptor.Remove(currentValue, key);
            return(currentValue);
        }
Ejemplo n.º 10
0
        /// <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);
        }
Ejemplo n.º 11
0
        /// <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);
        }
Ejemplo n.º 12
0
        /// <inheritdoc/>
        protected override void Undo(object parameter, UndoToken token)
        {
            if (undo == null)
            {
                throw new InvalidOperationException("This command cannot be cancelled.");
            }

            undo(parameter, token);
        }
Ejemplo n.º 13
0
        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();
        }
Ejemplo n.º 14
0
        /// <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);
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        /// <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);
        }
Ejemplo n.º 18
0
        /// <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);
        }
Ejemplo n.º 19
0
        /// <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;
            }
        }
Ejemplo n.º 20
0
        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);
        }
Ejemplo n.º 21
0
        /// <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);
        }
Ejemplo n.º 22
0
        /// <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);
        }
Ejemplo n.º 24
0
        /// <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);
        }
Ejemplo n.º 25
0
 /// <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);
Ejemplo n.º 26
0
 /// <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);
 }
Ejemplo n.º 27
0
 public ModelNodeToken(UndoToken token, UndoToken additionalToken)
 {
     Token           = token;
     AdditionalToken = additionalToken;
 }
Ejemplo n.º 28
0
 /// <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));
 }
Ejemplo n.º 29
0
 /// <inheritdoc/>
 public abstract object Undo(object currentValue, UndoToken undoToken);
Ejemplo n.º 30
0
 /// <inheritdoc/>
 public abstract object Invoke(object currentValue, object parameter, out UndoToken undoToken);