db8d4d73f1791a830a41688ae83cf9ee3117177c
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Converters / ArgumentToExpressionModelItemConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Converters
6 {
7     using System.Diagnostics;
8     using System.Globalization;
9     using System.Windows.Data;
10     using System.Activities.Presentation.Model;
11     using System.Activities.Presentation.Internal.PropertyEditing.Model;
12
13     // This converter converts from Argument to Argument.Expression ModelItem using the ModelPropertyEntry instance.
14     // on the reverse direction it creates  a new Argument and sets the expression on it with the value
15     // The direction of the created argument is provided by the converter parameter.
16     //1st binding is to the PropertyValue
17     //2nd binding is to the ModelProprtyEntry (to get the modelItem).
18     public class ArgumentToExpressionModelItemConverter : IMultiValueConverter
19     {
20         public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
21         {
22             ModelItem convertedValue = null;
23             if (values[1] != null)
24             {
25                 ModelPropertyEntry modelPropertyEntry = values[1] as ModelPropertyEntry;
26                 if (modelPropertyEntry != null)
27                 {
28                     ModelProperty property = modelPropertyEntry.FirstModelProperty;
29                     if (property != null)
30                     {
31                         ModelItem argumentModelItem = property.Value;
32                         if (argumentModelItem != null &&
33                                 argumentModelItem.Properties["Expression"] != null &&
34                                 argumentModelItem.Properties["Expression"].Value != null)
35                         {
36                             convertedValue = argumentModelItem.Properties["Expression"].Value;
37                         }
38                     }
39                 }
40             }
41             return convertedValue;
42         }
43
44         public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
45         {
46             Argument target = null;
47             ArgumentDirection direction = ArgumentDirection.In;
48             string directionString = parameter as string;
49             if (!string.IsNullOrEmpty(directionString))
50             {
51                 direction = (ArgumentDirection)Enum.Parse(typeof(ArgumentDirection), directionString);
52             }
53
54             Activity expression = null;
55             ModelItem valueExpressionModelItem = value as ModelItem;
56             if (valueExpressionModelItem != null && typeof(Activity).IsAssignableFrom(valueExpressionModelItem.ItemType))
57             {
58                 expression = (Activity)valueExpressionModelItem.GetCurrentValue();
59             }
60             ActivityWithResult activityWithResult = expression as ActivityWithResult;
61             if (expression != null && activityWithResult != null)
62             {
63                 // In the In case the expression is of type Activity<T> so we want to create InArgument<T>
64                 // In Out and InOut case the expresion is Activity<Location<T>> we want to create OutArgument<T>,InOutArgument<T>
65                 Type argumentType;
66                 if (direction == ArgumentDirection.In)
67                 {
68                     argumentType = activityWithResult.ResultType;
69                 }
70                 else
71                 {
72                     // if expression type is Location<T> argument type is T
73                     argumentType = activityWithResult.ResultType.GetGenericArguments()[0];
74                 }
75                 target = Argument.Create(argumentType, direction);
76                 target.Expression = activityWithResult;
77             }
78             return new object[] { target };
79         }
80
81     }
82 }