示例#1
0
 public void SetDisableWhenUseless(Control ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].DisableWhenUseless = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.DisableWhenUseless = value;
         _sources.Add(ctl, props);
     }
 }
示例#2
0
 public void SetCommandName(Control ctl, string value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].CommandName = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.CommandName = value;
         _sources.Add(ctl, props);
     }
 }
示例#3
0
 public void SetPostSaveAction(Control ctl, PostSaveActionType value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].PostSaveAction = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.PostSaveAction = value;
         _sources.Add(ctl, props);
     }
 }
示例#4
0
 public void SetRebindAfterSave(Control ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].RebindAfterSave = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.RebindAfterSave = value;
         _sources.Add(ctl, props);
     }
 }
示例#5
0
 public void SetDisableWhenClean(ToolStripButton ctl, bool value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].DisableWhenClean = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.DisableWhenClean = value;
         _sources.Add(ctl, props);
     }
 }
示例#6
0
 public void SetActionType(Control ctl, CslaFormAction value)
 {
     if (_sources.ContainsKey(ctl))
     {
         _sources[ctl].ActionType = value;
     }
     else
     {
         CslaActionExtenderProperties props = new CslaActionExtenderProperties();
         props.ActionType = value;
         _sources.Add(ctl, props);
     }
 }
示例#7
0
        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);
                        }
#if !WEBGUI
                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                        MessageBoxButtons.OK, MessageBoxIcon.Error,
                                        messageBoxClosedHandler, false);
#endif
                    }
                }

                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);
        }
示例#8
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)
        {
            Control ctl = (Control)sender;
            CslaActionExtenderProperties props = _sources[ctl];

            if (props.ActionType != CslaFormAction.None)
            {
                try
                {
#if WEBGUI
                    var pendingUserInput = false;
#endif
                    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)
                        {
#if !WEBGUI
                            DialogResult diagResult;
#endif

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

                            case CslaFormAction.Cancel:

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

                                if (diagResult == DialogResult.Yes)
                                {
                                    _bindingSourceTree.Cancel(savableObject);
                                }
#else
                                if (_warnOnCancel && trackableObject.IsDirty)
                                {
                                    pendingUserInput = true;
                                    ConfirmCancel(savableObject, props.CommandName);
                                }
                                else
                                {
                                    _bindingSourceTree.Cancel(savableObject);
                                }
#endif

                                break;
                            // case CslaFormAction.Cancel

                            case CslaFormAction.Close:

#if !WEBGUI
                                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;
                                }
#else
                                if (trackableObject.IsDirty || trackableObject.IsNew)
                                {
                                    if (_warnIfCloseOnDirty)
                                    {
                                        pendingUserInput = true;
                                        ConfirmClose(props.CommandName);
                                    }
                                }
                                else
                                {
                                    _bindingSourceTree.Close();
                                    _closeForm = true;
                                }
#endif

                                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);
                                        }
#if !WEBGUI
                                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
                                        MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Error,
                                                        messageBoxClosedHandler, false);
#endif
                                    }
                                    else
                                    {
#if !WEBGUI
                                        MessageBox.Show(_objectIsValidMessage, Resources.ActionExtenderInformationCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Information);
#else
                                        MessageBox.Show(_objectIsValidMessage, Resources.ActionExtenderInformationCaption,
                                                        MessageBoxButtons.OK, MessageBoxIcon.Information,
                                                        messageBoxClosedHandler, false);
#endif
                                    }
                                }

                                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);
                                    }
                                }
#if !WEBGUI
                                else
                                {
                                    if (props.ActionType == CslaFormAction.Cancel)
                                    {
                                        InitializeControls(true);
                                    }
                                }

                                OnClicked(new CslaActionEventArgs(props.CommandName));
#else
                                if (!pendingUserInput)
                                {
                                    if (props.ActionType == CslaFormAction.Cancel)
                                    {
                                        InitializeControls(true);
                                    }

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

#if !WEBGUI
                    if (_closeForm)
                    {
                        CloseForm();
                    }
#else
                    if (!pendingUserInput)
                    {
                        if (_closeForm)
                        {
                            CloseForm();
                        }
                    }
#endif
                }
                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);
            }
#if !WEBGUI
            MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
              MessageBoxButtons.OK, MessageBoxIcon.Error);
#else
            MessageBox.Show(brokenRules, Resources.ActionExtenderErrorCaption,
              MessageBoxButtons.OK, MessageBoxIcon.Error,
              messageBoxClosedHandler, false);
#endif
          }
        }

        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;
    }
 public void SetCommandName(Control 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(Control 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(Control 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(Control 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(Control 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);
   }
 }