예제 #1
0
        /// <summary>
        /// Cell property changed event handler. change of a value of a cell.
        /// </summary>
        /// <param name="sender">Sender of event.</param>
        /// <param name="e">What is changed.</param>
        public void UICellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1 || e.ColumnIndex == -1)
            {
                // if given cell is out of range
                return;
            }

            DataGridView     changedDataGridView = sender as DataGridView;
            DataGridViewCell newCell             = changedDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex];
            BaseCell         spreadSheetCell     = this.spreadSheet[e.RowIndex, e.ColumnIndex];

            ICommandInterface newCommand;

            // push new command to undo stack for latter call
            if (spreadSheetCell.Text == null)
            {
                // if the content of the cell is empty.
                newCommand = new UndoCommand(CommandType.Text, spreadSheetCell, string.Empty);
            }
            else
            {
                newCommand = new UndoCommand(CommandType.Text, spreadSheetCell, spreadSheetCell.Text);
            }

            this.undoStack.Push(newCommand);

            // change text to new cell value.
            spreadSheetCell.Text = newCell.Value.ToString();
        }
예제 #2
0
        private UndoCommand SpreadSheetUndo()
        {
            ICommandInterface command    = this.undoStack.Pop();
            UndoCommand       newCommand = command as UndoCommand;

            while (newCommand.Content == null)
            {
                command    = this.undoStack.Pop();
                newCommand = command as UndoCommand;
            }

            if (newCommand.CommandType == CommandType.Text)
            {
                ICommandInterface newRedoCommand = new RedoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Text);
                this.redoStack.Push(newRedoCommand);
            }
            else if (newCommand.CommandType == CommandType.Color)
            {
                ICommandInterface newRedoCommand = new RedoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Color.ToString());
                this.redoStack.Push(newRedoCommand);
            }

            command.Action();
            return(newCommand);
        }
예제 #3
0
        /// <summary>
        /// event handler for cell back color change.
        /// </summary>
        /// <param name="dataGridViewCell">datagridview cell.</param>
        /// <param name="newColorCode">new color code.</param>
        public void BackColorChangeHandler(DataGridViewCell dataGridViewCell, int newColorCode)
        {
            // create new command and push it
            ICommandInterface newCommand = new UndoCommand(CommandType.Color, this.spreadSheet[dataGridViewCell.RowIndex, dataGridViewCell.ColumnIndex], this.spreadSheet[dataGridViewCell.RowIndex, dataGridViewCell.ColumnIndex].Color.ToString());

            this.undoStack.Push(newCommand);

            // change cell's color to given color
            this.spreadSheet[dataGridViewCell.RowIndex, dataGridViewCell.ColumnIndex].Color = newColorCode;
            dataGridViewCell.Style.BackColor = System.Drawing.Color.FromArgb(Convert.ToInt32(this.spreadSheet[dataGridViewCell.RowIndex, dataGridViewCell.ColumnIndex].Color));
        }
예제 #4
0
        /// <summary>
        /// Undo execution event.
        /// </summary>
        public void UndoExecute(DataGridView dataGridView)
        {
            // execute command in the undo stack
            UndoCommand newCommand = this.SpreadSheetUndo();

            // update data grid view cell
            if (newCommand.CommandType == CommandType.Text)
            {
                dataGridView.Rows[newCommand.Cell.RowIndex].Cells[newCommand.Cell.ColumnIndex].Value = this.spreadSheet[newCommand.Cell.RowIndex, newCommand.Cell.ColumnIndex].Text;
                this.undoStack.Pop();
            }
            else if (newCommand.CommandType == CommandType.Color)
            {
                dataGridView.Rows[newCommand.Cell.RowIndex].Cells[newCommand.Cell.ColumnIndex].Style.BackColor = System.Drawing.Color.FromArgb(this.spreadSheet[newCommand.Cell.RowIndex, newCommand.Cell.ColumnIndex].Color);
            }
        }
예제 #5
0
        /// <summary>
        /// Spreadsheet undo execute.
        /// </summary>
        public void SpreadSheetUndo()
        {
            ICommandInterface command    = this.undoStack.Pop();
            UndoCommand       newCommand = command as UndoCommand;

            if (newCommand.CommandType == CommandType.Text)
            {
                ICommandInterface newRedoCommand = new RedoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Text);
                this.redoStack.Push(newRedoCommand);
            }
            else if (newCommand.CommandType == CommandType.Color)
            {
                ICommandInterface newRedoCommand = new RedoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Color.ToString());
                this.redoStack.Push(newRedoCommand);
            }

            command.Action();
        }
예제 #6
0
        private RedoCommand SpreadSheetRedo()
        {
            ICommandInterface command    = this.redoStack.Pop();
            RedoCommand       newCommand = command as RedoCommand;

            if (newCommand.CommandType == CommandType.Text)
            {
                ICommandInterface newUndoCommand = new UndoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Text);
                this.UndoStack.Push(newUndoCommand);
            }
            else if (newCommand.CommandType == CommandType.Color)
            {
                ICommandInterface newUndoCommand = new UndoCommand(newCommand.CommandType, newCommand.Cell, newCommand.Cell.Color.ToString());
                this.undoStack.Push(newUndoCommand);
            }

            command.Action();
            return(newCommand);
        }
예제 #7
0
        /// <summary>
        /// Add undo command to undo stack.
        /// </summary>
        /// <param name="row">row number of cell.</param>
        /// <param name="col">col number of cell.</param>
        /// <param name="commandType">command type of command.</param>
        /// <param name="content">the content of command.</param>
        public void AddUndo(int row, int col, CommandType commandType, string content)
        {
            ICommandInterface newCommand = new UndoCommand(commandType, this.spreadSheet[row, col], content);

            this.undoStack.Push((UndoCommand)newCommand);
        }