-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImmediateValidationController.cs
71 lines (69 loc) · 3.21 KB
/
ImmediateValidationController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Actions;
using DevExpress.Persistent.Base;
using DevExpress.ExpressApp.Validation;
using System.Collections;
using DevExpress.Persistent.Validation;
using DevExpress.ExpressApp.Editors;
namespace ValidateHighlight.Module {
public class ImmediateValidationTargetObjectsSelector : ValidationTargetObjectSelector {
protected override bool NeedToValidateObject(IObjectSpace objectSpace, object targetObject) {
return true;
}
}
public class ImmediateValidationController : ViewController {
protected override void OnActivated() {
base.OnActivated();
ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged;
ObjectSpace.ObjectReloaded += ObjectSpace_ObjectReloaded;
View.CurrentObjectChanged += View_CurrentObjectChanged;
}
protected override void OnViewControlsCreated() {
base.OnViewControlsCreated();
ValidateViewObjects();
}
void View_CurrentObjectChanged(object sender, EventArgs e) {
ValidateViewObjects();
}
private void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) {
ValidateViewObjects();
}
private void ObjectSpace_ObjectReloaded(object sender, ObjectManipulatingEventArgs e) {
ValidateViewObjects();
}
private void ValidateViewObjects() {
if (View is ListView) {
if (!((ListView)View).CollectionSource.IsServerMode) {
ValidateObjects(((ListView)View).CollectionSource.List);
}
} else if (View is DetailView) {
ImmediateValidationTargetObjectsSelector objectsSelector = new ImmediateValidationTargetObjectsSelector();
ValidateObjects(objectsSelector.GetObjectsToValidate(View.ObjectSpace, View.CurrentObject));
}
}
private void ValidateObjects(IEnumerable targets) {
if (targets == null) return;
ResultsHighlightController resultsHighlightController = Frame.GetController<ResultsHighlightController>();
if (resultsHighlightController != null) {
IRuleSet ruleSet = Validator.GetService(Application.ServiceProvider);
RuleSetValidationResult result = ruleSet.ValidateAllTargets(ObjectSpace, targets, DefaultContexts.Save);
if (result.ValidationOutcome == ValidationOutcome.Error || result.ValidationOutcome == ValidationOutcome.Warning || result.ValidationOutcome == ValidationOutcome.Information) {
resultsHighlightController.HighlightResults(result);
} else {
resultsHighlightController.ClearHighlighting();
}
}
}
protected override void OnDeactivated() {
base.OnDeactivated();
ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged;
ObjectSpace.ObjectReloaded -= ObjectSpace_ObjectReloaded;
View.CurrentObjectChanged -= View_CurrentObjectChanged;
}
}
}