Пример #1
0
        /// <summary>
        /// Saves the form state.
        /// </summary>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application.
        /// </remarks>
        /// <returns>True if form state was successfully stored, false if an error
        /// occurred</returns>
        public bool SaveState()
        {
            if (Form.WindowState == FormWindowState.Minimized)
            {
                // don't save state if minimized
                return(false);
            }

            try
            {
                // get the current form state
                T formState = GetCurrentState();

                // write form state
                FormStatePersistence.WriteToXml(_fileName, formState, typeof(T));
            }
            catch (Exception ex)
            {
                _msg.Warn("Error saving form state", ex);

                // ignore errors
                return(false);
            }

            return(true);
        }
Пример #2
0
        /// <summary>
        /// Saves the window settings for the form.
        /// </summary>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application.
        /// </remarks>
        /// <returns>True if settings were successfully stored, false if an error
        /// occurred</returns>
        public bool SaveSettings()
        {
            if (_form.WindowState == FormWindowState.Minimized)
            {
                // don't save state if minimized
                return(false);
            }
            else
            {
                try
                {
                    // get the current settings for the form
                    WindowSettings settings = GetCurrentSettings();

                    // write settings
                    FormStatePersistence.WriteToXml(_fileName, settings, _settingsType);
                }
                catch (Exception ex)
                {
                    _msg.Warn("Error saving window settings", ex);

                    // ignore errors
                    return(false);
                }

                return(true);
            }
        }
Пример #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="FormStateManager&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="form">The form.</param>
        /// <param name="callingContextID">The calling context ID.</param>
        public FormStateManager([NotNull] Form form,
                                [CanBeNull] string callingContextID = null)
        {
            Assert.ArgumentNotNull(form, nameof(form));

            Form      = form;
            _fileName = FormStatePersistence.GetFileName(form.GetType().Name, callingContextID);
        }
Пример #4
0
        /// <summary>
        /// Restores the saved geometry settings (if existing) for the form and
        /// returns a value that indicates if the settings have been successfully
        /// restored.
        /// </summary>
        /// <param name="restoreOption">options for the restore.</param>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application. However, it is guaranteed that all
        /// changes are fully rolled back.
        /// </remarks>
        /// <returns>True if stored settings have been encountered and successfully
        /// applied, false if no stored settings existed for the form or an error
        /// occurred</returns>
        public bool RestoreSettings(FormStateRestoreOption restoreOption)
        {
            // read settings from file
            WindowSettings settings;

            try
            {
                settings = (WindowSettings)FormStatePersistence.ReadFromXml(
                    _fileName, _settingsType);
            }
            catch (Exception e)
            {
                _msg.Warn("Error reading stored settings", e);

                // ignore the exception
                return(false);
            }

            if (settings == null)
            {
                return(false);
            }
            else
            {
                // get the current settings for rollback in case of exception
                WindowSettings origSettings = GetCurrentSettings();

                try
                {
                    ApplySettings(settings, restoreOption);
                }
                catch (Exception e)
                {
                    // roll back and ignore the exception
                    try
                    {
                        ApplySettings(origSettings);
                    }
                    catch
                    {
                        _msg.Warn("Unable to roll back window settings", e);
                    }
                    finally
                    {
                        _msg.Warn("Error applying stored window settings", e);
                    }

                    return(false);
                }
            }

            return(true);
        }
Пример #5
0
        /// <summary>
        /// Restores the saved form state (if existing) for the form and
        /// returns a value that indicates if the form state has been successfully
        /// restored.
        /// </summary>
        /// <param name="restoreOption">options for the restore.</param>
        /// <remarks>
        /// All exceptions are ignored to make sure that this miscellaneous function
        /// does never break the application. However, it is guaranteed that all
        /// changes are fully rolled back.
        /// </remarks>
        /// <returns>True if stored form state has been encountered and successfully
        /// applied, false if no stored form state existed for the form or an error
        /// occurred</returns>
        public bool RestoreState(
            FormStateRestoreOption restoreOption = FormStateRestoreOption.Normal)
        {
            // read state from file
            T formState;

            try
            {
                formState = (T)FormStatePersistence.ReadFromXml(_fileName, typeof(T));
            }
            catch (Exception e)
            {
                _msg.Warn(
                    "Error restoring saved window state. Window state is reset to defaults.", e);

                // ignore the exception
                return(false);
            }

            if (formState == null)
            {
                return(false);
            }

            // get the current form state for rollback in case of exception
            T origFormState = GetCurrentState();

            try
            {
                ApplyFormState(formState, restoreOption);
            }
            catch (Exception e)
            {
                // roll back and ignore the exception
                try
                {
                    ApplyFormState(origFormState);
                }
                catch
                {
                    _msg.Warn("Unable to roll back form state", e);
                }
                finally
                {
                    _msg.Warn("Error applying stored form state", e);
                }

                return(false);
            }

            return(true);
        }