1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Activities.Presentation.Model
7 using System.Collections.Generic;
8 using System.Diagnostics;
9 using System.Activities.Presentation.Services;
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.
15 class ModelServiceImpl : ModelService
17 ModelTreeManager modelTreeManager;
19 public ModelServiceImpl(ModelTreeManager modelTreeManager)
21 if (modelTreeManager == null)
23 throw FxTrace.Exception.AsError(new ArgumentNullException("modelTreeManager"));
25 this.modelTreeManager = modelTreeManager;
28 public override event EventHandler<ModelChangedEventArgs> ModelChanged;
30 public override ModelItem Root
34 return modelTreeManager.Root;
38 public override IEnumerable<ModelItem> Find(ModelItem startingItem, Predicate<Type> match)
40 return ModelTreeManager.Find(startingItem, delegate(ModelItem m) { return match(m.ItemType); }, false);
43 public override IEnumerable<ModelItem> Find(ModelItem startingItem, Type type)
45 if (startingItem == null)
47 throw FxTrace.Exception.AsError(new ArgumentNullException("startingItem"));
52 throw FxTrace.Exception.AsError(new ArgumentNullException("type"));
54 Fx.Assert(!type.IsValueType, "hmm why would some one search for modelitems for value types?");
55 return ModelTreeManager.Find(startingItem, delegate(ModelItem modelItem)
57 return type.IsAssignableFrom(modelItem.ItemType);
61 public override ModelItem FromName(ModelItem scope, string name, StringComparison comparison)
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());
68 internal void OnModelItemAdded(ModelItem modelItem, ModelChangeInfo changeInfo)
70 Fx.Assert(modelItem != null, "modelItem should not be null");
71 if (ModelChanged != null)
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();
81 internal void OnModelItemRemoved(ModelItem modelItem, ModelChangeInfo changInfo)
83 Fx.Assert(modelItem != null, "modelItem should not be null");
84 if (ModelChanged != null)
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();
93 internal void EmitModelChangeInfo(ModelChangeInfo changInfo)
95 Fx.Assert(changInfo != null, "changInfo should not be null");
97 if (ModelChanged != null)
99 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, null, null, changInfo));
100 modelTreeManager.SyncModelAndText();
104 internal void OnModelItemsRemoved(IEnumerable<ModelItem> modelItems)
106 Fx.Assert(modelItems != null, "modelItem should not be null");
107 if (ModelChanged != null)
109 List<ModelItem> modelItemsRemoved = new List<ModelItem>();
110 modelItemsRemoved.AddRange(modelItems);
111 ModelChanged.Invoke(this, new ModelChangedEventArgsImpl(null, modelItemsRemoved, null));
112 modelTreeManager.SyncModelAndText();
116 internal void OnModelPropertyChanged(ModelProperty property, ModelChangeInfo changeInfo)
118 Fx.Assert(property != null, "property cannot be null");
119 Fx.Assert(changeInfo != null, "changeInfo cannot be null");
121 if (ModelChanged != null)
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();
130 protected override ModelItem CreateItem(object instance)
132 return modelTreeManager.CreateModelItem(null, instance);
135 protected override ModelItem CreateItem(Type itemType, CreateOptions options, params object[] arguments)
137 Object instance = Activator.CreateInstance(itemType, arguments);
138 return modelTreeManager.CreateModelItem(null, instance);
141 protected override ModelItem CreateStaticMemberItem(Type type, string memberName)
143 throw FxTrace.Exception.AsError(new NotSupportedException());
146 internal ModelItem WrapAsModelItem(object instance)
148 return CreateItem(instance);