[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 / FromExpression / Framework / PropertyInspector / NewItemFactoryTypeModel.cs
1 // -------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 // -------------------------------------------------------------------
4 //From \\authoring\Sparkle\Source\1.0.1083.0\Common\Source\Framework\Properties
5 namespace System.Activities.Presentation.Internal.PropertyEditing.FromExpression.Framework.PropertyInspector
6 {
7     using System;
8     using System.Windows;
9     using System.Activities.Presentation.PropertyEditing;
10     using System.ComponentModel;
11     using System.Windows.Media;
12     using System.Globalization;
13
14     internal class NewItemFactoryTypeModel
15     {
16         private Type type;
17         private NewItemFactory factory;
18         private Size desiredSize;
19         private IMessageLogger exceptionLogger;
20
21         public NewItemFactoryTypeModel(Type type, NewItemFactory factory)
22         {
23             this.type = type;
24             this.factory = factory;
25             this.desiredSize = new Size(0, 0);
26             this.exceptionLogger = null;
27         }
28
29         public NewItemFactoryTypeModel(Type type, NewItemFactory factory, IMessageLogger exceptionLogger) : this(type, factory)
30         {
31             this.exceptionLogger = exceptionLogger;
32         }
33
34         public string DisplayName
35         {
36             get { return this.factory.GetDisplayName(this.type); }
37         }
38
39         public Type Type
40         {
41             get { return this.type; }
42         }
43
44         public object Image
45         {
46             get
47             {
48                 object image = this.factory.GetImage(this.type, this.desiredSize);
49                 ImageSource imageSource = image as ImageSource;
50                 if (imageSource != null && imageSource is ISupportInitialize)
51                 {
52                     try
53                     {
54                         double dummyHeight = imageSource.Height;
55                     }
56                     catch (InvalidOperationException exception)
57                     {
58                         this.ReportException(string.Format(CultureInfo.CurrentCulture, ExceptionStringTable.NewItemFactoryIconLoadFailed, this.factory.GetType().Name, exception.Message));
59                     }
60                 }
61                 return image;
62             }
63         }
64
65         public Size DesiredSize
66         {
67             get { return this.desiredSize; }
68             set { this.desiredSize = value; }
69         }
70
71         public NewItemFactory ItemFactory
72         {
73             get { return this.factory; }
74         }
75
76         public object CreateInstance()
77         {
78             return this.factory.CreateInstance(this.type);
79         }
80
81         private void ReportException(string message)
82         {
83             if (this.exceptionLogger != null)
84             {
85                 this.exceptionLogger.WriteLine(message);
86             }
87         }
88
89         // <summary>
90         //  Seems like the ComboBoxAutomation peer, calls the object.ToString() to read out
91         //  the item, if the item doesnt have its content set when queried by AutomationClient.
92         //  As a result, when this NewItemFactoryTypeModel,
93         //  is added to the combo-box in a SubPropertyEditor, we need to return the DisplayName
94         //  property in the ToString() implementation so that that the AutomationClient
95         //  reads out the correct value instead of the type of the object.
96         // </summary>
97         // <returns></returns>
98         public override string ToString()
99         {
100             return DisplayName;
101         }
102     }
103 }