149981c37453ba02423255b29be5ae2f99c343cb
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Base / Interaction / Model / ModelFactory.cs
1
2 namespace System.Activities.Presentation.Model {
3
4     using System.Activities.Presentation.Services;
5     using System.Activities.Presentation;
6
7     using System;
8
9     /// <summary>
10     /// The ModelFactory class should be used to create instances 
11     /// of items in the designer. ModelFactory is designed to be 
12     /// a static API for convenience.  The underlying implementation 
13     /// of this API simply calls through to the ModelService\92
14     /// CreateItem method.
15     /// </summary>
16     public static class ModelFactory {
17
18         /// <summary>
19         /// Creates a new item for the given item type.
20         /// </summary>
21         /// <param name="context">
22         /// The designer's editing context.
23         /// </param>
24         /// <param name="itemType">
25         /// The type of item to create.
26         /// </param>
27         /// <param name="arguments">
28         /// An optional array of arguments that should be passed to the constructor of the item.
29         /// </param>
30         /// <returns>
31         /// The newly created item type.
32         /// </returns>
33         /// <exception cref="ArgumentNullException">if itemType or context is null.</exception>
34         /// <exception cref="InvalidOperationException">if there is no editing model in the context that can create new items.</exception>
35         public static ModelItem CreateItem(EditingContext context, Type itemType, params object[] arguments) {
36             return CreateItem(context, itemType, CreateOptions.None, arguments);
37         }
38
39         /// <summary>
40         /// Creates a new item for the given item type.
41         /// </summary>
42         /// <param name="context">
43         /// The designer's editing context.
44         /// </param>
45         /// <param name="itemType">
46         /// The type of item to create.
47         /// </param>
48         /// <param name="options">
49         /// A set of create options to use when creating the item.  The default value is CreateOptions.None.
50         /// </param>
51         /// <param name="arguments">
52         /// An optional array of arguments that should be passed to the constructor of the item.
53         /// </param>
54         /// <returns>
55         /// The newly created item type.
56         /// </returns>
57         /// <exception cref="ArgumentNullException">if itemType or context is null.</exception>
58         /// <exception cref="InvalidOperationException">if there is no editing model in the context that can create new items.</exception>
59         public static ModelItem CreateItem(EditingContext context, Type itemType, CreateOptions options, params object[] arguments) {
60             if (context == null) throw FxTrace.Exception.ArgumentNull("context");
61             if (itemType == null) throw FxTrace.Exception.ArgumentNull("itemType");
62             if (!EnumValidator.IsValid(options)) throw FxTrace.Exception.AsError(new ArgumentOutOfRangeException("options"));
63
64             ModelService ms = context.Services.GetRequiredService<ModelService>();
65             return ms.InvokeCreateItem(itemType, options, arguments);
66         }
67
68         /// <summary>
69         /// Creates a new model item by creating a deep copy of the isntance provided.
70         /// </summary>
71         /// <param name="context">
72         /// The designer's editing context.
73         /// </param>
74         /// <param name="item">
75         /// The item to clone.
76         /// </param>
77         /// <returns>
78         /// The newly created item.
79         /// </returns>
80         public static ModelItem CreateItem(EditingContext context, object item) {
81             if (context == null) throw FxTrace.Exception.ArgumentNull("context");
82             if (item == null) throw FxTrace.Exception.ArgumentNull("item");
83
84             ModelService ms = context.Services.GetRequiredService<ModelService>();
85             return ms.InvokeCreateItem(item);
86         }
87
88         /// <summary>
89         /// Create a new model item that represents a the value of a static member of a the given class.
90         /// For example, to add a reference to Brushes.Red to the model call this methods with 
91         /// typeof(Brushes) and the string "Red". This will be serialized into XAML as 
92         /// {x:Static Brushes.Red}.
93         /// </summary>
94         /// <param name="context">
95         /// The designer's editing context.
96         /// </param>
97         /// <param name="type">
98         /// The type that contains the static member being referenced.
99         /// </param>
100         /// <param name="memberName">
101         /// The name of the static member being referenced.
102         /// </param>
103         /// <returns></returns>
104         public static ModelItem CreateStaticMemberItem(EditingContext context, Type type, string memberName) {
105             if (context == null) throw FxTrace.Exception.ArgumentNull("context");
106             if (type == null) throw FxTrace.Exception.ArgumentNull("type");
107             if (memberName == null) throw FxTrace.Exception.ArgumentNull("memberName");
108
109             ModelService ms = context.Services.GetRequiredService<ModelService>();
110             return ms.InvokeCreateStaticMemberItem(type, memberName);
111         }
112     }
113 }