311c2172dd95e8345a6238d8d3e1fd7dd6150144
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / Model / ModelServiceImpl.cs
1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4
5 namespace System.Activities.Presentation.Model
6 {
7     using System.Collections.Generic;
8     using System.Diagnostics;
9     using System.Activities.Presentation.Services;
10     using System.Runtime;
11
12     // This is the implementaion of the ModelService, this is published by the ModelTreeManager
13     // on the editingContext. This is just a facade to the modelTreemanager methods.
14
15     class ModelServiceImpl : ModelService
16     {
17         ModelTreeManager modelTreeManager;
18
19         public ModelServiceImpl(ModelTreeManager modelTreeManager)
20         {
21             if (modelTreeManager == null)
22             {
23                 throw FxTrace.Exception.AsError(new ArgumentNullException("modelTreeManager"));
24             }
25             this.modelTreeManager = modelTreeManager;
26         }
27
28         public override event EventHandler<ModelChangedEventArgs> ModelChanged;
29
30         public override ModelItem Root
31         {
32             get
33             {
34                 return modelTreeManager.Root;
35             }
36         }
37
38         public override IEnumerable<ModelItem> Find(ModelItem startingItem, Predicate<Type> match)
39         {
40             return ModelTreeManager.Find(startingItem, delegate(ModelItem m) { return match(m.ItemType); }, false);
41         }
42
43         public override IEnumerable<ModelItem> Find(ModelItem startingItem, Type type)
44         {
45             if (startingItem == null)
46             {
47                 throw FxTrace.Exception.AsError(new ArgumentNullException("startingItem"));
48             }
49
50             if (type == null)
51             {
52                 throw FxTrace.Exception.AsError(new ArgumentNullException("type"));
53             }
54             Fx.Assert(!type.IsValueType, "hmm why would some one search for modelitems for value types?");
55             return ModelTreeManager.Find(startingItem, delegate(ModelItem modelItem)
56             {
57                 return type.IsAssignableFrom(modelItem.ItemType);
58             }, false);
59         }
60
61         public override ModelItem FromName(ModelItem scope, string name, StringComparison comparison)
62         {
63             // The workflow component model does not implement a unique named activity object right now
64             // so we cannot support this feature.
65             throw FxTrace.Exception.AsError(new NotSupportedException());
66         }
67
68         internal void OnModelItemAdded(ModelItem modelItem, ModelChangeInfo changeInfo)
69         {
70             Fx.Assert(modelItem != null, "modelItem should not be null");
71             if (ModelChanged != null)
72             {
73                 Fx.Assert(modelItem != null, "trying to add empty model item");
74                 List<ModelItem> modelItemsAdded = new List<ModelItem>(1);
75                 modelItemsAdded.Add(modelItem);
76                 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(modelItemsAdded, null, null, changeInfo));
77                 modelTreeManager.SyncModelAndText();
78             }
79         }
80
81         internal void OnModelItemRemoved(ModelItem modelItem, ModelChangeInfo changInfo)
82         {
83             Fx.Assert(modelItem != null, "modelItem should not be null");
84             if (ModelChanged != null)
85             {
86                 List<ModelItem> modelItemsRemoved = new List<ModelItem>(1);
87                 modelItemsRemoved.Add(modelItem);
88                 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, modelItemsRemoved, null, changInfo));
89                 modelTreeManager.SyncModelAndText();
90             }
91         }
92
93         internal void EmitModelChangeInfo(ModelChangeInfo changInfo)
94         {
95             Fx.Assert(changInfo != null, "changInfo should not be null");
96
97             if (ModelChanged != null)
98             {
99                 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, null, null, changInfo));
100                 modelTreeManager.SyncModelAndText();
101             }
102         }
103
104         internal void OnModelItemsRemoved(IEnumerable<ModelItem> modelItems)
105         {
106             Fx.Assert(modelItems != null, "modelItem should not be null");
107             if (ModelChanged != null)
108             {
109                 List<ModelItem> modelItemsRemoved = new List<ModelItem>();
110                 modelItemsRemoved.AddRange(modelItems);
111                 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, modelItemsRemoved, null));
112                 modelTreeManager.SyncModelAndText();
113             }
114         }
115
116         internal void OnModelPropertyChanged(ModelProperty property, ModelChangeInfo changeInfo)
117         {
118             Fx.Assert(property != null, "property cannot be null");
119             Fx.Assert(changeInfo != null, "changeInfo cannot be null");
120
121             if (ModelChanged != null)
122             {
123                 List<ModelProperty> propertiesChanged = new List<ModelProperty>(1);
124                 propertiesChanged.Add(property);
125                 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, null, propertiesChanged, changeInfo));
126                 modelTreeManager.SyncModelAndText();
127             }
128         }
129
130         protected override ModelItem CreateItem(object instance)
131         {
132             return modelTreeManager.CreateModelItem(null, instance);
133         }
134
135         protected override ModelItem CreateItem(Type itemType, CreateOptions options, params object[] arguments)
136         {
137             Object instance = Activator.CreateInstance(itemType, arguments);
138             return modelTreeManager.CreateModelItem(null, instance);
139         }
140
141         protected override ModelItem CreateStaticMemberItem(Type type, string memberName)
142         {
143             throw FxTrace.Exception.AsError(new NotSupportedException());
144         }
145
146         internal ModelItem WrapAsModelItem(object instance)
147         {
148             return CreateItem(instance);
149         }
150     }
151 }