6d59f45506f898f8a83d7dae2a2cf09496698301
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Core / PropertyEditing / newitemfactory.cs
1 namespace System.Activities.Presentation.PropertyEditing {
2     using System;
3     using System.Windows;
4     using System.Reflection;
5     using System.IO;
6     using System.Windows.Markup;
7     using System.Windows.Media.Imaging;
8     using System.Windows.Controls;
9     using System.Activities.Presentation.Internal;
10     using System.Diagnostics.CodeAnalysis;
11     using System.Activities.Presentation;
12
13     /// <summary>
14     /// Base class that represents a factory for creating new items for a collection or
15     /// for a property value.  3rd party control developers may choose to derive from this class
16     /// to override the default behavior of specifying names and images that are used by
17     /// collection editor and sub-property editor when creating instances of custom controls.
18     /// </summary>
19     class NewItemFactory {
20
21         private Type[] NoTypes = new Type[0];
22
23         /// <summary>
24         /// Default constructor
25         /// </summary>
26         public NewItemFactory() { }
27
28         /// <summary>
29         /// Returns an object that can be set as the Content of a ContentControl 
30         /// and that will be used an icon for the requested type by the property editing host.
31         /// The default implementation of this method uses naming convention, searching for
32         /// the embedded resources in the same assembly as the control, that are named the 
33         /// same as the control (including namespace), followed by ".Icon", followed by
34         /// the extension for the file type itself.  Currently, only ".png", ".bmp", ".gif",
35         /// ".jpg", and ".jpeg" extensions are recognized.
36         /// </summary>
37         /// <param name="type">Type of the object to look up</param>
38         /// <param name="desiredSize">The desired size of the image to retrieve.  If multiple
39         /// images are available this method retrieves the image that most closely
40         /// resembles the requested size.  However, it is not guaranteed to return an image
41         /// that matches the desired size exactly.</param>
42         /// <returns>An image for the specified type.</returns>
43         /// <exception cref="ArgumentNullException">If type is null</exception>
44         public virtual object GetImage(Type type, Size desiredSize) {
45             if (type == null)
46                 throw FxTrace.Exception.ArgumentNull("type");
47
48             return ManifestImages.GetImage(type, desiredSize);
49         }
50
51         /// <summary>
52         /// Returns the name for the item this factory adds for the passed in type.  This is 
53         /// the name that will be used in the "Add Item" drop down to identify the type being added.
54         /// The default implementation returns the short type name.
55         /// </summary>
56         /// <param name="type">Type to retrieve the display name for.</param>
57         /// <returns>The display name for the specified type</returns>
58         /// <exception cref="ArgumentNullException">If type is null</exception>
59         [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "The intended usage in the larger scope of the class is the stronger type")]
60         public virtual string GetDisplayName(Type type)
61         {
62             if (type == null)
63                 throw FxTrace.Exception.ArgumentNull("type");
64
65             return type.Name;
66         }
67
68         /// <summary>
69         /// Returns an instance of an item that is added to the collection for the passed in Type.
70         /// The default implementation looks for public constructors that take no arguments.
71         /// If no such constructors are found, null is returned.
72         /// </summary>
73         /// <param name="type">Type of the object to create</param>
74         /// <returns>Instance of the specified type, or null if no appropriate constructor was found
75         /// </returns>
76         /// <exception cref="ArgumentNullException">If type is null</exception>
77         public virtual object CreateInstance(Type type) {
78             if (type == null)
79                 throw FxTrace.Exception.ArgumentNull("type");
80
81             ConstructorInfo ctor = type.GetConstructor(
82                 BindingFlags.Public | BindingFlags.Instance,
83                 null,
84                 NoTypes,
85                 null);
86
87             return ctor == null ? null : ctor.Invoke(null);
88         }
89     }
90 }