[amd64/tramp] hide interpreter specific trampoline behind ifdef
[mono.git] / mcs / class / referencesource / System.Activities.Presentation / System.Activities.Presentation / System / Activities / Presentation / View / WorkflowViewManager.cs
1 //----------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //----------------------------------------------------------------
4
5 namespace System.Activities.Presentation.View
6 {
7     using System.Windows;
8
9     using System.Windows.Media;
10     using System.Windows.Media.Effects;
11     using System.Windows.Documents;
12
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;
22     using System.Runtime;
23     using System.ComponentModel.Design;
24     using System.Activities.Presentation.Annotations;
25
26
27     internal class WorkflowViewManager : ViewManager
28     {
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;
37
38         public override System.Windows.Media.Visual View
39         {
40             get
41             {
42                 return view;
43             }
44         }
45         public override void Initialize(EditingContext context)
46         {
47             this.context = context;
48             AttachedPropertiesService propertiesService = this.context.Services.GetService<AttachedPropertiesService>();
49             helpService = this.context.Services.GetService<IIntegratedHelpService>();
50
51             oldSelection = this.context.Items.GetValue<Selection>();
52             isPrimarySelectionProperty = new AttachedProperty<bool>()
53                 {
54                     Getter = (modelItem) => (this.context.Items.GetValue<Selection>().PrimarySelection == modelItem),
55                     Name = "IsPrimarySelection",
56                     OwnerType = typeof(Object)
57                 };
58
59             isSelectionProperty = new AttachedProperty<bool>()
60             {
61                 Getter = (modelItem) => (((IList)this.context.Items.GetValue<Selection>().SelectedObjects).Contains(modelItem)),
62                 Name = "IsSelection",
63                 OwnerType = typeof(Object)
64             };
65
66
67             propertiesService.AddProperty(isPrimarySelectionProperty);
68             propertiesService.AddProperty(isSelectionProperty);
69             
70
71
72
73             if (this.context.Services.GetService<ViewService>() == null)
74             {
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);
82
83                 WorkflowAnnotationAdornerService annotationService = new WorkflowAnnotationAdornerService();
84                 annotationService.Initialize(this.context, view.scrollViewer);
85                 this.context.Services.Publish<AnnotationAdornerService>(annotationService);
86
87                 this.context.Services.Subscribe<ModelService>(delegate(ModelService modelService)
88                 {
89                     this.modelService = modelService;
90                     if (modelService.Root != null)
91                     {
92                         view.MakeRootDesigner(modelService.Root);
93                     }
94                     view.RestoreDesignerStates();
95                     this.context.Items.Subscribe<Selection>(new SubscribeContextCallback<Selection>(OnItemSelected));
96                 });
97             }
98
99             if (helpService != null)
100             {
101                 helpService.AddContextAttribute(string.Empty, KeywordForWorkflowDesignerHomePage, HelpKeywordType.F1Keyword); 
102             }
103         }
104
105         internal static string GetF1HelpTypeKeyword(Type type)
106         {
107             Fx.Assert(type != null, "type is null");
108             if (type.IsGenericType)
109             {
110                 Type genericTypeDefinition = type.GetGenericTypeDefinition();
111                 return genericTypeDefinition.FullName;
112             }
113             return type.FullName;
114         }
115
116         void OnItemSelected(Selection newSelection)
117         {
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>;
121
122             //Call notifyPropertyChanged for IsPrimarySelection attached property.
123             if (newSelection.PrimarySelection != null && !newSelection.PrimarySelection.Equals(oldSelection.PrimarySelection))
124             {
125                 isPrimarySelectionProperty.NotifyPropertyChanged(oldSelection.PrimarySelection);
126                 isPrimarySelectionProperty.NotifyPropertyChanged(newSelection.PrimarySelection);
127             }
128             else if (newSelection.PrimarySelection == null)
129             {
130                 isPrimarySelectionProperty.NotifyPropertyChanged(oldSelection.PrimarySelection);
131             }
132
133
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)
138             {
139                 isSelectionProperty.NotifyPropertyChanged(selectionChangeMI);
140             }
141                         
142             if (helpService != null)
143             {
144                 if (oldSelection.PrimarySelection != null)
145                 {         
146                     helpService.RemoveContextAttribute(string.Empty, GetF1HelpTypeKeyword(oldSelection.PrimarySelection.ItemType));
147                 }
148
149                 if (newSelection.PrimarySelection != null)
150                 {
151                     helpService.AddContextAttribute(string.Empty, GetF1HelpTypeKeyword(newSelection.PrimarySelection.ItemType), HelpKeywordType.F1Keyword);
152                 }
153             }
154             oldSelection = newSelection;
155         }
156
157
158     }
159
160
161 }