Esempio n. 1
0
        void Core.IUndoableObject.CopyState(int parentEditLevel, bool parentBindingEdit)
        {
            if (this.EditLevel + 1 > parentEditLevel)
            {
                throw new UndoException(string.Format(Resources.EditLevelMismatchException, "CopyState"), this.GetType().Name, _parent != null ? _parent.GetType().Name : null, this.EditLevel, parentEditLevel - 1);
            }

#if (ANDROID || IOS) || NETFX_CORE || NETSTANDARD2_0
            SerializationInfo state = new SerializationInfo(0);
            OnGetState(state, StateMode.Undo);

            // This is used instead of a foreach because of some weird silverlight bug
            // throwing an unknown exception from a foreach here.
            for (var index = 0; index < _fieldData.Length; index++)
            {
                var item = _fieldData[index];
                if (item != null)
                {
                    var child = item.Value as IUndoableObject;
                    if (child != null)
                    {
                        // cascade call to child
                        child.CopyState(parentEditLevel, parentBindingEdit);
                    }
                }
            }

            _stateStack.Push(state);
#else
            IFieldData[] state = new IFieldData[_propertyList.Count];

            for (var index = 0; index < _fieldData.Length; index++)
            {
                var item = _fieldData[index];
                if (item != null)
                {
                    var child = item.Value as IUndoableObject;
                    if (child != null)
                    {
                        // cascade call to child
                        child.CopyState(parentEditLevel, parentBindingEdit);
                        // indicate that there was a value here
                        state[index] = new FieldData <bool>(item.Name);
                    }
                    else
                    {
                        // add the IFieldData object
                        state[index] = item;
                    }
                }
            }

            // serialize the state and stack it
            using (MemoryStream buffer = new MemoryStream())
            {
                var formatter = SerializationFormatterFactory.GetNativeFormatter();
                formatter.Serialize(buffer, state);
                _stateStack.Push(buffer.ToArray());
            }
#endif
        }
        /// <summary>
        /// Method invoked when the target control is clicked.
        /// </summary>
        /// <param name="sender">Object originating action.</param>
        /// <param name="e">Arguments.</param>
        protected void OnClick(object sender, EventArgs e)
        {
            MenuItem ctl = (MenuItem)sender;
            CslaActionExtenderProperties props = _sources[ctl];

            if (props.ActionType != CslaFormAction.None)
            {
                try
                {
                    bool raiseClicked = true;
                    CslaActionCancelEventArgs args = new CslaActionCancelEventArgs(false, props.CommandName);
                    OnClicking(args);
                    if (!args.Cancel)
                    {
                        ISavable      savableObject   = null;
                        ITrackStatus  trackableObject = null;
                        BindingSource source          = null;

                        var sourceObjectError = false;
                        if (_dataSource != null)
                        {
                            source = _dataSource as BindingSource;

                            if (source != null)
                            {
                                savableObject   = source.DataSource as ISavable;
                                trackableObject = source.DataSource as ITrackStatus;
                            }
                            else
                            {
                                OnErrorEncountered(new ErrorEncounteredEventArgs(props.CommandName, new InvalidCastException(Resources.ActionExtenderInvalidBindingSourceCast)));
                                sourceObjectError = true;
                            }

                            if (savableObject == null || trackableObject == null)
                            {
                                OnErrorEncountered(new ErrorEncounteredEventArgs(props.CommandName, new InvalidCastException(Resources.ActionExtenderInvalidBusinessObjectBaseCast)));
                                sourceObjectError = true;
                            }
                        }

                        if (!sourceObjectError)
                        {
                            DialogResult diagResult;

                            switch (props.ActionType)
                            {
                            case CslaFormAction.Save:
                                raiseClicked = ExecuteSaveAction(savableObject, trackableObject, props);
                                break;
                            // case CslaFormAction.Save

                            case CslaFormAction.Cancel:

                                diagResult = DialogResult.Yes;
                                if (_warnOnCancel && trackableObject.IsDirty)
                                {
                                    diagResult = MessageBox.Show(
                                        _warnOnCancelMessage, Resources.Warning,
                                        MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                }

                                if (diagResult == DialogResult.Yes)
                                {
                                    _bindingSourceTree.Cancel(savableObject);
                                }

                                break;
                            // case CslaFormAction.Cancel

                            case CslaFormAction.Close:

                                diagResult = DialogResult.Yes;
                                if (trackableObject.IsDirty || trackableObject.IsNew)
                                {
                                    if (_warnIfCloseOnDirty)
                                    {
                                        diagResult = MessageBox.Show(
                                            _dirtyWarningMessage + Environment.NewLine + Resources.ActionExtenderCloseConfirmation,
                                            Resources.Warning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                    }
                                }

                                if (diagResult == DialogResult.Yes)
                                {
                                    _bindingSourceTree.Close();
                                    _closeForm = true;
                                }

                                break;
                            // case CslaFormAction.Close

                            case CslaFormAction.Validate:

                                if (savableObject is BusinessBase)
                                {
                                    BusinessBase businessObject = savableObject as BusinessBase;
                                    if (!businessObject.IsValid)
                                    {
                                        // todo: add child broken rules
                                        string brokenRules = string.Empty;
                                        foreach (var brokenRule in businessObject.GetBrokenRules())
                                        {
                                            var lambdaBrokenRule = brokenRule;
                                            var friendlyName     =
                                                PropertyInfoManager.GetRegisteredProperties(businessObject.GetType()).Find(
                                                    c => c.Name == lambdaBrokenRule.Property).FriendlyName;
                                            brokenRules += string.Format("{0}: {1}{2}", friendlyName, brokenRule, Environment.NewLine);
                                        }
                                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    }
                                    else
                                    {
                                        MessageBox.Show(_objectIsValidMessage, Resources.ActionExtenderInformationCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    }
                                }

                                break;
                                //case CslaFormAction.Validate
                            } // switch (props.ActionType)

                            // raiseClicked is true if
                            // ActionType == CslaFormAction.Save and everything is ok
                            if (raiseClicked)
                            {
                                if (props.ActionType == CslaFormAction.Save && source != null)
                                {
                                    if (props.RebindAfterSave)
                                    {
                                        // For some strange reason, this has to be done down here.
                                        // Putting it in the Select Case AfterSave... does not work.
                                        _bindingSourceTree.ResetBindings(false);
                                        InitializeControls(true);
                                    }
                                }
                                else
                                {
                                    if (props.ActionType == CslaFormAction.Cancel)
                                    {
                                        InitializeControls(true);
                                    }
                                }

                                OnClicked(new CslaActionEventArgs(props.CommandName));
                            }
                        } // if (!sourceObjectError)
                    }     // if (!args.Cancel)

                    if (_closeForm)
                    {
                        CloseForm();
                    }
                }
                catch (Exception ex)
                {
                    OnErrorEncountered(new ErrorEncounteredEventArgs(props.CommandName, ex));
                }
            } // if (props.ActionType != CslaFormAction.None)
        }
        private bool ExecuteSaveAction(ISavable savableObject, ITrackStatus trackableObject, CslaActionExtenderProperties props)
        {
            var  result       = true;
            bool okToContinue = true;

            BusinessBase businessObject = null;
            bool         savableObjectIsBusinessBase = savableObject is BusinessBase;

            if (savableObjectIsBusinessBase)
            {
                businessObject = savableObject as BusinessBase;
            }

            if (savableObjectIsBusinessBase)
            {
                if (!businessObject.IsValid)
                {
                    HasBrokenRulesEventArgs argsHasBrokenRules = new HasBrokenRulesEventArgs(
                        props.CommandName,
                        businessObject.GetBrokenRules().ErrorCount > 0,
                        businessObject.GetBrokenRules().WarningCount > 0,
                        businessObject.GetBrokenRules().InformationCount > 0,
                        _autoShowBrokenRules);

                    OnHasBrokenRules(argsHasBrokenRules);

                    okToContinue = !argsHasBrokenRules.Cancel;
                    //in case the client changed it
                    _autoShowBrokenRules = argsHasBrokenRules.AutoShowBrokenRules;
                }
            }

            if (okToContinue)
            {
                if (savableObjectIsBusinessBase)
                {
                    if (_autoShowBrokenRules && !businessObject.IsValid)
                    {
                        // todo: add child broken rules
                        string brokenRules = string.Empty;
                        foreach (var brokenRule in businessObject.GetBrokenRules())
                        {
                            var lambdaBrokenRule = brokenRule;
                            var friendlyName     =
                                PropertyInfoManager.GetRegisteredProperties(businessObject.GetType()).Find(
                                    c => c.Name == lambdaBrokenRule.Property).FriendlyName;
                            brokenRules += string.Format("{0}: {1}{2}", friendlyName, brokenRule, Environment.NewLine);
                        }
                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }

                if (trackableObject.IsValid)
                {
                    CslaActionCancelEventArgs savingArgs = new CslaActionCancelEventArgs(false, props.CommandName);
                    OnObjectSaving(savingArgs);

                    if (!savingArgs.Cancel)
                    {
                        _bindingSourceTree.Apply();
                        ISavable objectToSave;

                        if (Csla.ApplicationContext.AutoCloneOnUpdate == false)
                        {
                            objectToSave = ((ICloneable)savableObject).Clone() as ISavable;// if not AutoClone, clone manually
                        }
                        else
                        {
                            objectToSave = savableObject;
                        }

                        if (objectToSave != null)
                        {
                            try
                            {
                                RemoveEventHooks(savableObject);
                                savableObject = savableObject.Save() as ISavable;

                                OnObjectSaved(new CslaActionEventArgs(props.CommandName));

                                switch (props.PostSaveAction)
                                {
                                case PostSaveActionType.None:

                                    if (props.RebindAfterSave)
                                    {
                                        _bindingSourceTree.Bind(savableObject);
                                        AddEventHooks(savableObject);
                                    }
                                    break;

                                case PostSaveActionType.AndClose:

                                    CloseForm();
                                    break;

                                case PostSaveActionType.AndNew:

                                    OnSetForNew(new CslaActionEventArgs(props.CommandName));
                                    AddEventHooks(savableObject);
                                    break;
                                }
                            }
                            catch (Exception ex)
                            {
                                _bindingSourceTree.Bind(objectToSave);
                                AddEventHooks(objectToSave);
                                OnErrorEncountered(new ErrorEncounteredEventArgs(props.CommandName, new ObjectSaveException(ex)));
                                // there was some problem
                                result = false;
                            }
                        }
                        else
                        {
                            // did not find bound object so don't bother raising the Clicked event
                            result = false;
                        }

                        _bindingSourceTree.SetEvents(true);
                    }
                }
                else
                {
                    OnBusinessObjectInvalid(new CslaActionEventArgs(props.CommandName));
                    // object not valid or has broken rules set to invalidate it due to this control's properties
                    result = false;
                }
            }
            else
            {
                // process was canceled from the HasBrokenRules event (okToContinue = false)
                result = false;
            }

            return(result);
        }
Esempio n. 4
0
        void Core.IUndoableObject.CopyState(int parentEditLevel, bool parentBindingEdit)
        {
            if (this.EditLevel + 1 > parentEditLevel)
            {
                throw new UndoException(string.Format(Resources.EditLevelMismatchException, "CopyState"), this.GetType().Name, _parent != null ? _parent.GetType().Name : null, this.EditLevel, parentEditLevel - 1);
            }

            IFieldData[] state = new IFieldData[_propertyList.Count];

            for (var index = 0; index < _fieldData.Length; index++)
            {
                var item = _fieldData[index];
                if (item != null)
                {
                    if (item.Value is IUndoableObject child)
                    {
                        // cascade call to child
                        child.CopyState(parentEditLevel, parentBindingEdit);
                        // indicate that there was a value here
                        state[index] = new FieldData <bool>(item.Name);
                    }
                    else
                    {
                        // add the IFieldData object
                        state[index] = item;
                    }
                }
            }

            // serialize the state and stack it
            using (MemoryStream buffer = new MemoryStream())
            {
                var formatter = SerializationFormatterFactory.GetFormatter();
                var stateList = new MobileList <IFieldData>(state.ToList());
                formatter.Serialize(buffer, stateList);
                _stateStack.Push(buffer.ToArray());
            }
        }