1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //----------------------------------------------------------------
5 namespace System.Activities.Presentation.View
9 using System.Windows.Media;
10 using System.Windows.Media.Effects;
11 using System.Windows.Documents;
13 using System.Activities.Presentation;
14 using System.Activities.Presentation.View;
15 using System.Activities.Presentation.Hosting;
16 using System.Activities.Presentation.Model;
17 using System.Activities.Presentation.Documents;
18 using System.Activities.Presentation.Services;
19 using System.Collections.ObjectModel;
20 using System.Collections;
21 using System.Collections.Generic;
23 using System.ComponentModel.Design;
24 using System.Activities.Presentation.Annotations;
27 internal class WorkflowViewManager : ViewManager
29 const string KeywordForWorkflowDesignerHomePage = "DefaultWorkflowDesigner";
30 EditingContext context;
31 Selection oldSelection;
32 ModelService modelService;
33 System.Activities.Presentation.View.DesignerView view;
34 AttachedProperty<bool> isPrimarySelectionProperty;
35 AttachedProperty<bool> isSelectionProperty;
36 IIntegratedHelpService helpService;
38 public override System.Windows.Media.Visual View
45 public override void Initialize(EditingContext context)
47 this.context = context;
48 AttachedPropertiesService propertiesService = this.context.Services.GetService<AttachedPropertiesService>();
49 helpService = this.context.Services.GetService<IIntegratedHelpService>();
51 oldSelection = this.context.Items.GetValue<Selection>();
52 isPrimarySelectionProperty = new AttachedProperty<bool>()
54 Getter = (modelItem) => (this.context.Items.GetValue<Selection>().PrimarySelection == modelItem),
55 Name = "IsPrimarySelection",
56 OwnerType = typeof(Object)
59 isSelectionProperty = new AttachedProperty<bool>()
61 Getter = (modelItem) => (((IList)this.context.Items.GetValue<Selection>().SelectedObjects).Contains(modelItem)),
63 OwnerType = typeof(Object)
67 propertiesService.AddProperty(isPrimarySelectionProperty);
68 propertiesService.AddProperty(isSelectionProperty);
73 if (this.context.Services.GetService<ViewService>() == null)
75 view = new System.Activities.Presentation.View.DesignerView(this.context);
76 WorkflowViewService viewService = new WorkflowViewService(context);
77 WorkflowViewStateService viewStateService = new WorkflowViewStateService(context);
78 this.context.Services.Publish<ViewService>(viewService);
79 this.context.Services.Publish<VirtualizedContainerService>(new VirtualizedContainerService(this.context));
80 this.context.Services.Publish<ViewStateService>(viewStateService);
81 this.context.Services.Publish<DesignerView>(view);
83 WorkflowAnnotationAdornerService annotationService = new WorkflowAnnotationAdornerService();
84 annotationService.Initialize(this.context, view.scrollViewer);
85 this.context.Services.Publish<AnnotationAdornerService>(annotationService);
87 this.context.Services.Subscribe<ModelService>(delegate(ModelService modelService)
89 this.modelService = modelService;
90 if (modelService.Root != null)
92 view.MakeRootDesigner(modelService.Root);
94 view.RestoreDesignerStates();
95 this.context.Items.Subscribe<Selection>(new SubscribeContextCallback<Selection>(OnItemSelected));
99 if (helpService != null)
101 helpService.AddContextAttribute(string.Empty, KeywordForWorkflowDesignerHomePage, HelpKeywordType.F1Keyword);
105 internal static string GetF1HelpTypeKeyword(Type type)
107 Fx.Assert(type != null, "type is null");
108 if (type.IsGenericType)
110 Type genericTypeDefinition = type.GetGenericTypeDefinition();
111 return genericTypeDefinition.FullName;
113 return type.FullName;
116 void OnItemSelected(Selection newSelection)
118 Fx.Assert(newSelection != null, "newSelection is null");
119 IList<ModelItem> newSelectionObjects = newSelection.SelectedObjects as IList<ModelItem>;
120 IList<ModelItem> oldSelectionObjects = oldSelection.SelectedObjects as IList<ModelItem>;
122 //Call notifyPropertyChanged for IsPrimarySelection attached property.
123 if (newSelection.PrimarySelection != null && !newSelection.PrimarySelection.Equals(oldSelection.PrimarySelection))
125 isPrimarySelectionProperty.NotifyPropertyChanged(oldSelection.PrimarySelection);
126 isPrimarySelectionProperty.NotifyPropertyChanged(newSelection.PrimarySelection);
128 else if (newSelection.PrimarySelection == null)
130 isPrimarySelectionProperty.NotifyPropertyChanged(oldSelection.PrimarySelection);
134 //call NotifyPropertyChanged for IsSelection property on ModelItems that were added or removed from selection.
135 HashSet<ModelItem> selectionChangeSet = new HashSet<ModelItem>(oldSelectionObjects);
136 selectionChangeSet.SymmetricExceptWith(newSelectionObjects);
137 foreach (ModelItem selectionChangeMI in selectionChangeSet)
139 isSelectionProperty.NotifyPropertyChanged(selectionChangeMI);
142 if (helpService != null)
144 if (oldSelection.PrimarySelection != null)
146 helpService.RemoveContextAttribute(string.Empty, GetF1HelpTypeKeyword(oldSelection.PrimarySelection.ItemType));
149 if (newSelection.PrimarySelection != null)
151 helpService.AddContextAttribute(string.Empty, GetF1HelpTypeKeyword(newSelection.PrimarySelection.ItemType), HelpKeywordType.F1Keyword);
154 oldSelection = newSelection;