Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Activities.Core.Presentation / System / Activities / Core / Presentation / PickValidationErrorSourceLocator.cs
1 // <copyright>
2 //   Copyright (c) Microsoft Corporation.  All rights reserved.
3 // </copyright>
4
5 namespace System.Activities.Core.Presentation
6 {
7     using System.Activities.Presentation.Validation;
8     using System.Activities.Statements;
9     using System.Collections.Generic;
10
11     internal class PickValidationErrorSourceLocator : IValidationErrorSourceLocator
12     {
13         public List<object> FindSourceDetailFromActivity(Activity errorSource, object errorSourceDetail)
14         {
15             if (errorSourceDetail == null)
16             {
17                 return new List<object> { errorSource };
18             }
19             else
20             {
21                 return FindRelativePath((Pick)errorSource, errorSourceDetail);
22             }
23         }
24
25         public void ReplaceParentChainWithSource(Activity parentActivity, List<object> parentChain)
26         {
27             Pick pick = (Pick)parentActivity;
28
29             if (parentChain.Count > 1)
30             {
31                 // assume last object in parentChain is PickBranchBody
32
33                 // remove PickBranchBody
34                 parentChain.RemoveAt(parentChain.Count - 1);
35
36                 Activity targetActivity = (Activity)parentChain[parentChain.Count - 1];
37
38                 // the targetActivity will be available in the path
39                 parentChain.RemoveAt(parentChain.Count - 1);
40
41                 List<object> path = FindRelativePath(pick, targetActivity);
42
43                 foreach (object pathObject in path)
44                 {
45                     parentChain.Add(pathObject);
46                 }
47             }
48         }
49
50         private static List<object> FindRelativePath(Pick pickActivity, object descendent)
51         {
52             List<object> path = FindDescendentFromPick(pickActivity, descendent);
53             path.Reverse();
54             return path;
55         }
56
57         private static List<object> FindDescendentFromPick(Pick pickActivity, object descendent)
58         {
59             List<object> path = new List<object>();
60             path.Add(pickActivity);
61             foreach (PickBranch branch in pickActivity.Branches)
62             {
63                 if (branch == descendent)
64                 {
65                     break;
66                 }
67                 else if (branch.Trigger == descendent)
68                 {
69                     path.Add(branch);
70                     break;
71                 }
72                 else if (branch.Action == descendent)
73                 {
74                     path.Add(branch);
75                     break;
76                 }
77                 else
78                 {
79                     bool isVariableError = false;
80                     foreach (Variable variable in branch.Variables)
81                     {
82                         if (variable.Default == descendent)
83                         {
84                             isVariableError = true;
85                             break;
86                         }
87                     }
88
89                     if (isVariableError)
90                     {
91                         path.Add(branch);
92                         break;
93                     }
94                 }
95             }
96
97             path.Add(descendent);
98             return path;
99         }       
100     }
101 }