public void SetCommandName(MenuItem ctl, string value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].CommandName = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.CommandName = value;
         _sources.Add(ctl, props);
     }
 }
 public void SetDisableWhenUseless(MenuItem ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].DisableWhenUseless = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.DisableWhenUseless = value;
         _sources.Add(ctl, props);
     }
 }
 public void SetRebindAfterSave(MenuItem ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].RebindAfterSave = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.RebindAfterSave = value;
         _sources.Add(ctl, props);
     }
 }
 public void SetPostSaveAction(MenuItem ctl, PostSaveActionType value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].PostSaveAction = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.PostSaveAction = value;
         _sources.Add(ctl, props);
     }
 }
 public void SetActionType(MenuItem ctl, CslaFormAction value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].ActionType = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.ActionType = value;
         _sources.Add(ctl, props);
     }
 }
 public void SetDisableWhenClean(ToolBarButton ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].DisableWhenClean = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.DisableWhenClean = value;
         _sources.Add(ctl, props);
     }
 }
        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);
        }
        /// <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)
        }