[reflection] Coop handles icalls in System.Reflection and System.RuntimeTypeHandle...
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / Internal / PropertyEditing / Editors / PropertyValueToDisplayNameConverter.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4 namespace System.Activities.Presentation.Internal.PropertyEditing.Editors 
5 {
6     using System;
7     using System.Collections.Generic;
8     using System.Diagnostics;
9     using System.Globalization;
10     using System.Text;
11     using System.Windows.Data;
12
13     using System.Activities.Presentation.PropertyEditing;
14     using System.Activities.Presentation;
15
16     using System.Activities.Presentation.Internal.PropertyEditing.Model;
17     using System.Activities.Presentation.Internal.Properties;
18
19     // <summary>
20     // ValueConverter that takes an instance of PropertyValue and returns a display name for
21     // it.  The returned name consists of the value Type name as well as its x:Name property
22     // if it is defined.
23     // </summary>
24     internal class PropertyValueToDisplayNameConverter : IValueConverter 
25     {
26
27         private static PropertyValueToDisplayNameConverter _instance;
28
29         // <summary>
30         // Static instance accessor for all non-XAML related conversion needs
31         // </summary>
32         public static PropertyValueToDisplayNameConverter Instance 
33         {
34             get {
35                 if (_instance == null)
36                 {
37                     _instance = new PropertyValueToDisplayNameConverter();
38                 }
39
40                 return _instance;
41             }
42         }
43
44         // Converts an instance of PropertyValue to its appropriate display name
45         public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
46         {
47
48             if (typeof(string).IsAssignableFrom(targetType)) 
49             {
50
51                 PropertyValue propertyValue = value as PropertyValue;
52                 if (propertyValue != null) 
53                 {
54
55                     ModelPropertyEntryBase propertyEntry = propertyValue.ParentProperty as ModelPropertyEntryBase;
56
57                     // Figure out the value type name
58                     string valueTypeName = string.Empty;
59                     if (propertyEntry != null)
60                     {
61                         valueTypeName = propertyEntry.CommonValueType == null ? string.Empty : propertyEntry.CommonValueType.Name;
62                     }
63                     else 
64                     {
65                         Debug.Fail("PropertyValueToDisplayNameConverter is being used for something other than ModelPropertyValues.  Re-evaluate the correctness of its logic.");
66
67                         // Fallback mechanism
68                         object rawPropertyValue = propertyValue.Value;
69                         if (rawPropertyValue != null)
70                         {
71                             valueTypeName = rawPropertyValue.GetType().Name;
72                         }
73                     }
74
75                     // See if there is a regular name
76                     string propertyName = ModelUtilities.GetPropertyName(propertyValue);
77
78                     if (string.IsNullOrEmpty(propertyName)) 
79                     {
80                         // Type only
81                         return string.Format(
82                             culture,
83                             Resources.PropertyEditing_CollectionItemDisplayFormatType,
84                             valueTypeName);
85                     }
86                     else 
87                     {
88                         // Type and name
89                         return string.Format(
90                             culture,
91                             Resources.PropertyEditing_CollectionItemDisplayFormatTypeAndName,
92                             valueTypeName,
93                             propertyName);
94                     }
95                 }
96             }
97
98             return string.Empty;
99         }
100
101         // This class is only a one-way converter
102         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
103         {
104             throw FxTrace.Exception.AsError(new NotImplementedException());
105         }
106     }
107 }