Exemple #1
0
        /// <summary>
        /// This method opens up a saved spread sheet from the specified file path
        /// </summary>
        private bool Load(JoinSessionResponse joinSessionResponse)
        {
            Spreadsheet spreadsheet;

            // Use the Spreadsheet constructor that will create a spreadsheet with all the data the .ss file contains.
            try
            {
                var spreadsheetElement = XElement.Parse(joinSessionResponse.Xml);
                spreadsheet = new Spreadsheet();
                //spreadsheet = new Spreadsheet(spreadsheetElement, IsValidCellName, NormalizeCellName, SpreadsheetXmlVersion);
            }
            catch (Exception ex)
            {
                ShowErrorMessage("Unable to open:\r\n" + ex.Message);
                return(false);
            }

            mySpreadsheet = spreadsheet;
            SetSpreadsheetName(joinSessionResponse.Name, joinSessionResponse.Version);

            CreateCells();

            // You need the names of all empty cells in order to display the non empty cells onto the data grid.
            var namesOfAllNonemptyCells = spreadsheet.GetNamesOfAllNonemptyCells();
            var rows           = MainDataGrid.Items; // The items in the MainDataGrid
            var rowCount       = rows.Count;
            var validCellNames = myValidCellNames;

            foreach (var cellName in namesOfAllNonemptyCells)
            {
                validCellNames.Add(cellName);

                var cellNumbers = GetCellNumbers(cellName); // Get the position of the cell
                var rowIndex    = cellNumbers.Item2 - 1;    // The row index

                // Add the data from the spreadsheet to the MainDataGrid UI
                while (rowCount <= rowIndex)
                {
                    rows.Add(CreateRow(rowCount++));
                }

                var row = (RowViewModel)rows[rowIndex];
                row.NotifyDataChanged();
            }

            // If no exceptions occured then tell that the file has been opened
            return(true);
        }
        private async void JoinButton_Click(object sender, RoutedEventArgs e)
        {
            var parameters = myParameters;

            parameters.Password = MainPasswordBox.Password;

            if (!IsValid(parameters))
            {
                return;
            }

            EnableUI(false);

            SpreadsheetServiceClient spreadsheetServiceClient = null;
            JoinSessionResponse      response = null;
            string errorMessage;

            try
            {
                spreadsheetServiceClient = new SpreadsheetServiceClient();
                await spreadsheetServiceClient.Connect(parameters.Server);

                response = await spreadsheetServiceClient.JoinSession(parameters.SpreadsheetName, parameters.Password);

                errorMessage = response.ErrorMessage;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            if (errorMessage == null)
            {
                new MainWindow(spreadsheetServiceClient, response).Show();
                Close();
            }
            else
            {
                spreadsheetServiceClient.Dispose();
                MessageBox.Show(this, "Unable to join the session:\r\n" + errorMessage, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
                EnableUI(true);
            }

            return;
        }
        public async Task <JoinSessionResponse> JoinSession(string name, string password)
        {
            myJoinSessionTaskCompletionSource = new TaskCompletionSource <JoinSessionResponse>();

            var ok = await SendText("JOIN\nName:" + name + "\nPassword:"******"\n");

            var taskCompletionSource = myJoinSessionTaskCompletionSource;

            if (!ok || taskCompletionSource == null)
            {
                var response = new JoinSessionResponse
                {
                    ErrorMessage = UnableToSendCommandErrorMessage
                };
                return(response);
            }

            return(await taskCompletionSource.Task);
        }
Exemple #4
0
        /// <summary>
        /// initializes the MainWindow
        /// </summary>
        internal MainWindow(SpreadsheetServiceClient spreadsheetServiceClient, JoinSessionResponse joinSessionResponse)
        {
            mySpreadsheetServiceClient            = spreadsheetServiceClient;
            spreadsheetServiceClient.CellUpdated += SpreadsheetServiceClient_CellUpdated;

            InitializeComponent();

            myUpdateRowTimer = new Timer(UpdateRow);

            var viewModel = new ViewModel(this);

            myViewModel = viewModel;

            New(); // Create a new spread sheet with the data grid cleared and the name set to 'Untitled'.

            DataContext = viewModel;

            myCurrentColumn = MainDataGrid.Columns[0];
            myCurrentRow    = viewModel.Rows[0];
            SetFocusOnCurrentCell();

            Dispatcher.BeginInvoke(new Action <JoinSessionResponse>(joinSessionResponse0 => Load(joinSessionResponse0)), joinSessionResponse);
        }
        protected override bool TryProcessCommand(string previousCommandName, string commandName, SortedDictionary <string, string> commandParameters)
        {
            const string createSessionSucceededCommandName     = "create ok";
            const string createSessionFailedCommandName        = "create fail";
            const string joinSessionSucceededCommandName       = "join ok";
            const string joinSessionFailedCommandName          = "join fail";
            const string changeCellSucceededCommandName        = "change ok";
            const string changeCellVersionOutOfDateCommandName = "change wait";
            const string changeCellFailedCommandName           = "change fail";
            const string undoSucceededCommandName        = "undo ok";
            const string undoNoUnsavedChangesCommandName = "undo end";
            const string undoVersionOutOfDateCommandName = "undo wait";
            const string undoFailedCommandName           = "undo fail";
            const string updateCommandName        = "update";
            const string saveSucceededCommandName = "save ok";
            const string saveFailedCommandName    = "save fail";

            const string passwordParameterName = "password";
            const string versionParameterName  = "version";
            const string cellParameterName     = "cell";
            const string lengthParameterName   = "length";

            string name;
            string password;
            string version;
            string cell;
            string length;

            switch (commandName)
            {
            case createSessionSucceededCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, passwordParameterName))
                {
                    var response = new CreateSessionResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myCreateSessionTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name) && commandParameters.TryGetValue(passwordParameterName, out password))
                {
                    var response = new CreateSessionResponse
                    {
                        Name     = name,
                        Password = password
                    };
                    return(TrySetResponse(myCreateSessionTaskCompletionSource, response));
                }

                return(false);

            case createSessionFailedCommandName:
                return(CommandFailedValidateParameters(commandParameters, myCreateSessionTaskCompletionSource));

            case joinSessionSucceededCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, lengthParameterName))
                {
                    var response = new JoinSessionResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myJoinSessionTaskCompletionSource, response));
                }
                return(false);

            case joinSessionFailedCommandName:
                return(CommandFailedValidateParameters(commandParameters, myJoinSessionTaskCompletionSource));

            case changeCellSucceededCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName))
                {
                    var response = new ChangeCellResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myChangeCellTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name) && commandParameters.TryGetValue(versionParameterName, out version))
                {
                    var response = new ChangeCellResponse
                    {
                        Name    = name,
                        Version = version
                    };
                    return(TrySetResponse(myChangeCellTaskCompletionSource, response));
                }

                return(false);

            case changeCellVersionOutOfDateCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName))
                {
                    var response = new ChangeCellResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myChangeCellTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name) && commandParameters.TryGetValue(versionParameterName, out version))
                {
                    var response = new ChangeCellResponse
                    {
                        Name             = name,
                        Version          = version,
                        VersionOutOfDate = true
                    };
                    return(TrySetResponse(myChangeCellTaskCompletionSource, response));
                }

                return(false);

            case changeCellFailedCommandName:
                return(CommandFailedValidateParameters(commandParameters, myChangeCellTaskCompletionSource));

            case undoSucceededCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, cellParameterName, lengthParameterName))
                {
                    var response = new UndoLastChangeResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                }
                return(false);

            case undoNoUnsavedChangesCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName))
                {
                    var response = new UndoLastChangeResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name) && commandParameters.TryGetValue(versionParameterName, out version))
                {
                    var response = new UndoLastChangeResponse
                    {
                        Name             = name,
                        Version          = version,
                        NoUnsavedChanges = true
                    };
                    return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                }

                return(false);

            case undoVersionOutOfDateCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName))
                {
                    var response = new UndoLastChangeResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name) && commandParameters.TryGetValue(versionParameterName, out version))
                {
                    var response = new UndoLastChangeResponse
                    {
                        Name             = name,
                        Version          = version,
                        VersionOutOfDate = true
                    };
                    return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                }

                return(false);

            case undoFailedCommandName:
                return(CommandFailedValidateParameters(commandParameters, myUndoLastChangeTaskCompletionSource));

            case updateCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, cellParameterName, lengthParameterName))
                {
                    return(true);
                }
                return(false);

            case saveSucceededCommandName:
                if (!ValidateParameters(commandParameters, NameParameterName))
                {
                    var response = new SaveResponse
                    {
                        ErrorMessage = InvalidResponseErrorMessage
                    };
                    return(TrySetResponse(mySaveTaskCompletionSource, response));
                }

                if (commandParameters.TryGetValue(NameParameterName, out name))
                {
                    var response = new SaveResponse
                    {
                        Name = name
                    };
                    return(TrySetResponse(mySaveTaskCompletionSource, response));
                }

                return(false);

            case saveFailedCommandName:
                return(CommandFailedValidateParameters(commandParameters, mySaveTaskCompletionSource));

            default:
                switch (previousCommandName)
                {
                case createSessionFailedCommandName:
                    return(CommandFailed(commandParameters, new CreateSessionResponse(), commandName, myCreateSessionTaskCompletionSource));

                case joinSessionSucceededCommandName:
                    if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, lengthParameterName))
                    {
                        var response = new CreateSessionResponse
                        {
                            ErrorMessage = InvalidResponseErrorMessage
                        };
                        return(TrySetResponse(myCreateSessionTaskCompletionSource, response));
                    }

                    if
                    (
                        commandParameters.TryGetValue(NameParameterName, out name) &&
                        commandParameters.TryGetValue(versionParameterName, out version) &&
                        commandParameters.TryGetValue(lengthParameterName, out length)
                    )
                    {
                        JoinSessionResponse response;
                        int lengthValue;

                        if (!int.TryParse(length, out lengthValue) || commandName.Length != lengthValue)
                        {
                            response = new JoinSessionResponse
                            {
                                ErrorMessage = InvalidResponseErrorMessage
                            };
                        }
                        else
                        {
                            response = new JoinSessionResponse
                            {
                                Name    = name,
                                Version = version,
                                Xml     = commandName
                            };
                        }

                        return(TrySetResponse(myJoinSessionTaskCompletionSource, response));
                    }

                    return(false);

                case joinSessionFailedCommandName:
                    return(CommandFailed(commandParameters, new JoinSessionResponse(), commandName, myJoinSessionTaskCompletionSource));

                case changeCellFailedCommandName:
                    return(CommandFailed(commandParameters, new ChangeCellResponse(), commandName, myChangeCellTaskCompletionSource));

                case undoSucceededCommandName:
                    if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, cellParameterName, lengthParameterName))
                    {
                        var response = new UndoLastChangeResponse
                        {
                            ErrorMessage = InvalidResponseErrorMessage
                        };
                        return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                    }

                    if
                    (
                        commandParameters.TryGetValue(NameParameterName, out name) &&
                        commandParameters.TryGetValue(versionParameterName, out version) &&
                        commandParameters.TryGetValue(cellParameterName, out cell) &&
                        commandParameters.TryGetValue(lengthParameterName, out length)
                    )
                    {
                        UndoLastChangeResponse response;
                        int lengthValue;

                        if (!int.TryParse(length, out lengthValue) || commandName.Length != lengthValue)
                        {
                            response = new UndoLastChangeResponse
                            {
                                ErrorMessage = InvalidResponseErrorMessage
                            };
                        }
                        else
                        {
                            response = new UndoLastChangeResponse
                            {
                                Name    = name,
                                Version = version,
                                Cell    = cell,
                                Content = commandName
                            };
                        }

                        return(TrySetResponse(myUndoLastChangeTaskCompletionSource, response));
                    }

                    return(false);

                case undoFailedCommandName:
                    return(CommandFailed(commandParameters, new UndoLastChangeResponse(), commandName, myUndoLastChangeTaskCompletionSource));

                case updateCommandName:
                    if (!ValidateParameters(commandParameters, NameParameterName, versionParameterName, cellParameterName, lengthParameterName))
                    {
                        return(true);
                    }

                    if
                    (
                        commandParameters.TryGetValue(NameParameterName, out name) &&
                        commandParameters.TryGetValue(versionParameterName, out version) &&
                        commandParameters.TryGetValue(cellParameterName, out cell) &&
                        commandParameters.TryGetValue(lengthParameterName, out length)
                    )
                    {
                        int lengthValue;

                        if (!int.TryParse(length, out lengthValue) || commandName.Length != lengthValue)
                        {
                            return(true);
                        }

                        var cellUpdated = CellUpdated;

                        if (cellUpdated == null)
                        {
                            return(true);
                        }

                        var updateEventArgs = new CellUpdatedEventArgs
                        {
                            Name    = name,
                            Version = version,
                            Cell    = cell,
                            Content = commandName
                        };

                        cellUpdated(this, updateEventArgs);

                        return(true);
                    }

                    return(false);

                case saveFailedCommandName:
                    return(CommandFailed(commandParameters, new SaveResponse(), commandName, mySaveTaskCompletionSource));

                default:
                    return(false);
                }
            }
        }