protected override bool HasValueChanged() { // Is this a child object/list that keeps track of status ITrackStatus child = Value as ITrackStatus; if (child != null) { return(child.IsDirty); } // else check single field var hasValueChanged = false; if (this.OriginalValue == null && this.Value == null) { hasValueChanged = false; } else if (this.OriginalValue != null) { hasValueChanged = !this.OriginalValue.Equals(this.Value); } else { hasValueChanged = true; } return(hasValueChanged); }
private void CancelMethod(object parameter) { ITrackStatus trackable = Model as ITrackStatus; if (trackable != null) { DoCancel(); if (trackable.IsNew) { OnAfterCancelNew(); } } }
protected override bool HasValueChanged() { // Is this a child object/list that keeps track of status ITrackStatus child = Value as ITrackStatus; if (child != null) { return(child.IsDirty); } return(this.Value != null ? this.OriginalValue != this.Value.GetHashCode() : this.OriginalValue != 0); }
protected override void BeginSave() { if (Model != null && Model is ITrackStatus) { ITrackStatus trackable = Model as ITrackStatus; if (trackable.IsValid) { ShowPleaseWaitMessage(); base.BeginSave(); } else { MessageBox.Show("Please correct invalid values before saving.", "Error", MessageBoxButton.OK); } } }
private void InitializeControl(Control ctl, KeyValuePair<Control, CslaActionExtenderProperties> pair) { if (pair.Value.DisableWhenUseless || (pair.Value.DisableWhenClean && !ctl.Enabled)) { ISavable businessObject = GetBusinessObject(); if (businessObject != null) { ITrackStatus trackableObject = businessObject as ITrackStatus; if (trackableObject != null) { if (pair.Value.ActionType == CslaFormAction.Cancel || pair.Value.DisableWhenClean) ChangeEnabled(ctl, trackableObject.IsNew || trackableObject.IsDirty || trackableObject.IsDeleted); if (pair.Value.ActionType == CslaFormAction.Save) ChangeEnabled(ctl, (trackableObject.IsNew || trackableObject.IsDirty || trackableObject.IsDeleted) && trackableObject.IsValid); } } } }
protected override void BeginSave() { _selectedIndex = -1; if (Model != null && Model is ITrackStatus) { ITrackStatus trackable = Model as ITrackStatus; if (trackable.IsValid) { if (_selectedItem != null && IsBoundToList) { IEnumerable list = Model as IEnumerable; _selectedIndex = (from object one in list select one).ToList().IndexOf(_selectedItem); } base.BeginSave(); } else { MessageBox.Show("Please correct invalid values.", "Rolodex", MessageBoxButton.OK); _savingAtClose = false; } } }
public virtual void CancelMethod(object parameter) { ITrackStatus trackable = Model as ITrackStatus; if (trackable != null) { _selectedIndex = -1; if (_selectedItem != null && IsBoundToList) { IEnumerable list = Model as IEnumerable; _selectedIndex = (from object one in list select one).ToList().IndexOf(_selectedItem); } DoCancel(); if (trackable.IsNew) { OnAfterCancelNew(); } if (_selectedIndex >= 0) { var itemList = (from object one in ((IEnumerable)Model) select one).ToList(); if (itemList.Count > _selectedIndex) { SelectedItem = itemList.Skip(_selectedIndex).Take(1).First(); } else { SelectFirstItem(); } } else { SelectFirstItem(); } } RefreshCommands(); }
private void SetProperties() { ITrackStatus targetObject = Model as ITrackStatus; ICollection list = Model as ICollection; INotifyBusy busyObject = Model as INotifyBusy; var isObjectBusy = false; if (busyObject != null && busyObject.IsBusy) { isObjectBusy = true; } // Does Model instance implement ITrackStatus if (targetObject != null) { var canDeleteInstance = BusinessRules.HasPermission(AuthorizationActions.DeleteObject, targetObject); IsDirty = targetObject.IsDirty; IsValid = targetObject.IsValid; CanSave = CanEditObject && targetObject.IsSavable && !isObjectBusy; CanCancel = CanEditObject && targetObject.IsDirty && !isObjectBusy; CanCreate = CanCreateObject && !targetObject.IsDirty && !isObjectBusy; CanDelete = CanDeleteObject && !isObjectBusy && canDeleteInstance; CanFetch = CanGetObject && !targetObject.IsDirty && !isObjectBusy; // Set properties for List if (list == null) { CanRemove = false; CanAddNew = false; } else { Type itemType = Csla.Utilities.GetChildItemType(Model.GetType()); if (itemType == null) { CanAddNew = false; CanRemove = false; } else { CanRemove = BusinessRules.HasPermission(AuthorizationActions.DeleteObject, itemType) && list.Count > 0 && !isObjectBusy; CanAddNew = BusinessRules.HasPermission(AuthorizationActions.CreateObject, itemType) && !isObjectBusy; } } } // Else if Model instance implement ICollection else if (list != null) { Type itemType = Csla.Utilities.GetChildItemType(Model.GetType()); if (itemType == null) { CanAddNew = false; CanRemove = false; } else { CanRemove = BusinessRules.HasPermission(AuthorizationActions.DeleteObject, itemType) && list.Count > 0 && !isObjectBusy; CanAddNew = BusinessRules.HasPermission(AuthorizationActions.CreateObject, itemType) && !isObjectBusy; } } else { IsDirty = false; IsValid = false; CanCancel = false; CanCreate = CanCreateObject; CanDelete = false; CanFetch = CanGetObject && !IsBusy; CanSave = false; CanRemove = false; CanAddNew = false; } }
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) }
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; }
private void RefreshCanOperationsValues() { ITrackStatus targetObject = this.Data as ITrackStatus; ICollection list = this.Data as ICollection; INotifyBusy busyObject = this.Data as INotifyBusy; bool isObjectBusy = false; if (busyObject != null && busyObject.IsBusy) { isObjectBusy = true; } if (this.Data != null && targetObject != null) { if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, this.Data) && targetObject.IsSavable) { this.CanSave = true; } else { this.CanSave = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.EditObject, this.Data) && targetObject.IsDirty && !isObjectBusy) { this.CanCancel = true; } else { this.CanCancel = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, this.Data) && !targetObject.IsDirty && !isObjectBusy) { this.CanCreate = true; } else { this.CanCreate = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, this.Data) && !isObjectBusy) { this.CanDelete = true; } else { this.CanDelete = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.GetObject, this.Data) && !targetObject.IsDirty && !isObjectBusy) { this.CanFetch = true; } else { this.CanFetch = false; } if (list != null) { Type itemType = Csla.Utilities.GetChildItemType(this.Data.GetType()); if (itemType != null) { if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, itemType) && ((ICollection)this.Data).Count > 0 && !isObjectBusy) { this.CanRemoveItem = true; } else { this.CanRemoveItem = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, itemType) && !isObjectBusy) { this.CanAddNewItem = true; } else { this.CanAddNewItem = false; } } else { this.CanAddNewItem = false; this.CanRemoveItem = false; } } else { this.CanRemoveItem = false; this.CanAddNewItem = false; } } else if (list != null) { Type itemType = Csla.Utilities.GetChildItemType(this.Data.GetType()); if (itemType != null) { if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.DeleteObject, itemType) && ((ICollection)this.Data).Count > 0 && !isObjectBusy) { this.CanRemoveItem = true; } else { this.CanRemoveItem = false; } if (Csla.Rules.BusinessRules.HasPermission(Rules.AuthorizationActions.CreateObject, itemType) && !isObjectBusy) { this.CanAddNewItem = true; } else { this.CanAddNewItem = false; } } else { this.CanAddNewItem = false; this.CanRemoveItem = false; } } else { this.CanCancel = false; this.CanCreate = false; this.CanDelete = false; this.CanFetch = !this.IsBusy; this.CanSave = false; this.CanRemoveItem = false; this.CanAddNewItem = false; } }