Ejemplo n.º 1
0
        /// <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)
        {
            ToolStripButton ctl = (ToolStripButton)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)
                                    {
                                        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)
        }