added samples
[windows-sources.git] / sdk / samples / WFSamples / Applications / TrackingProfileDesigner / vb / WorkflowDesignerControl / Services.vb
blob863adcf4b0dc77c606c230b690a918f527942e5b
1 '---------------------------------------------------------------------
2 ' This file is part of the Windows Workflow Foundation SDK Code Samples.
3 '
4 ' Copyright (C) Microsoft Corporation. All rights reserved.
5 '
6 ' This source code is intended only as a supplement to Microsoft
7 ' Development Tools and/or on-line documentation. See these other
8 ' materials for detailed information regarding Microsoft code samples.
9 '
10 ' THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
11 ' KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
12 ' IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
13 ' PARTICULAR PURPOSE.
14 '---------------------------------------------------------------------
16 Imports System.ComponentModel
17 Imports System.ComponentModel.Design
18 Imports System.Text
19 Imports System.Workflow.ComponentModel
20 Imports System.Workflow.ComponentModel.Compiler
21 Imports System.Workflow.ComponentModel.Design
22 Imports System.Globalization
24 #Region "IdentifierCreationService"
25 Friend NotInheritable Class IdentifierCreationService
26 Implements IIdentifierCreationService
27 Dim serviceProvider As IServiceProvider = Nothing
29 Friend Sub New(ByVal serviceProvider As IServiceProvider)
30 Me.serviceProvider = serviceProvider
31 End Sub
33 Sub ValidateIdentifier(ByVal activity As Activity, ByVal identifier As String) Implements IIdentifierCreationService.ValidateIdentifier
34 If identifier Is Nothing Then
35 Throw New ArgumentNullException("identifier")
36 End If
37 If activity Is Nothing Then
38 Throw New ArgumentNullException("activity")
39 End If
42 If (activity.Name.ToLower(CultureInfo.InvariantCulture).Equals(identifier.ToLower(CultureInfo.InvariantCulture))) Then
43 Return
44 End If
46 Dim identifiers As New ArrayList()
47 Dim rootActivity As Activity = GetRootActivity(activity)
48 identifiers.AddRange(GetIdentifiersInCompositeActivity(TryCast(rootActivity, CompositeActivity)))
49 identifiers.Sort()
50 If identifiers.BinarySearch(identifier.ToLower(CultureInfo.InvariantCulture), StringComparer.OrdinalIgnoreCase) >= 0 Then
51 Throw New ArgumentException(String.Format("Duplicate Component Identifier {0}", identifier))
52 End If
53 End Sub
55 Sub EnsureUniqueIdentifiers(ByVal parentActivity As CompositeActivity, ByVal childActivities As ICollection) Implements IIdentifierCreationService.EnsureUniqueIdentifiers
56 If parentActivity Is Nothing Then
57 Throw New ArgumentNullException("parentActivity")
58 End If
59 If childActivities Is Nothing Then
60 Throw New ArgumentNullException("childActivities")
61 End If
63 Dim allActivities As New ArrayList()
65 Dim activities As New Queue(childActivities)
66 While (activities.Count > 0)
67 Dim activity As Activity = CType(activities.Dequeue(), Activity)
68 If (TypeOf activity Is CompositeActivity) Then
69 For Each child As Activity In (CType(activity, CompositeActivity)).Activities
70 activities.Enqueue(child)
71 Next
72 End If
74 ' If we are moving activities, we need not regenerate their identifiers
75 If (CType(activity, IComponent)).Site IsNot Nothing Then
76 Continue While
77 End If
78 allActivities.Add(activity)
79 End While
81 ' get the root activity
82 Dim rootActivity As CompositeActivity = TryCast(GetRootActivity(parentActivity), CompositeActivity)
83 Dim identifiers As New ArrayList() ' all the identifiers in the workflow
84 identifiers.AddRange(GetIdentifiersInCompositeActivity(rootActivity))
86 For Each activity As Activity In allActivities
87 Dim finalIdentifier As String = activity.Name
89 ' now loop until we find a identifier that hasn't been used.
90 Dim baseIdentifier As String = GetBaseIdentifier(activity)
91 Dim index As Integer = 0
93 identifiers.Sort()
94 While finalIdentifier Is Nothing Or finalIdentifier.Length = 0 Or identifiers.BinarySearch(finalIdentifier.ToLower(CultureInfo.InvariantCulture), StringComparer.OrdinalIgnoreCase) >= 0
95 index += 1
96 finalIdentifier = String.Format("{0}{1}", baseIdentifier, index)
97 End While
99 ' add new identifier to collection
100 identifiers.Add(finalIdentifier)
101 activity.Name = finalIdentifier
102 Next
103 End Sub
105 Shared Function GetIdentifiersInCompositeActivity(ByVal compositeActivity As CompositeActivity) As IList
106 Dim identifiers As New ArrayList()
107 If compositeActivity IsNot Nothing Then
108 identifiers.Add(compositeActivity.Name)
109 Dim allChildren As IList(Of Activity) = GetAllNestedActivities(compositeActivity)
110 For Each activity As Activity In allChildren
111 identifiers.Add(activity.Name)
112 Next
113 End If
114 Return ArrayList.ReadOnly(identifiers)
115 End Function
117 Private Shared Function GetBaseIdentifier(ByVal activity As Activity) As String
118 Dim baseIdentifier As String = activity.GetType().Name
119 Dim b As New StringBuilder(baseIdentifier.Length)
120 For i As Integer = 0 To baseIdentifier.Length - 1
121 If (Char.IsUpper(baseIdentifier(i)) And (i = 0 Or i = baseIdentifier.Length - 1 Or Char.IsUpper(baseIdentifier(i + 1)))) Then
122 b.Append(Char.ToLower(baseIdentifier(i), CultureInfo.InvariantCulture))
123 Else
124 b.Append(baseIdentifier.Substring(i))
125 End If
126 next
127 Return b.ToString()
128 End Function
130 Shared Function GetRootActivity(ByVal activity As Activity) As Activity
131 If activity Is Nothing Then
132 Throw New ArgumentException("activity")
133 End If
135 While activity.Parent IsNot Nothing
136 activity = activity.Parent
137 End While
139 Return activity
140 End Function
142 Shared Function GetAllNestedActivities(ByVal compositeActivity As CompositeActivity) As Activity()
143 If compositeActivity Is Nothing Then
144 Throw New ArgumentNullException("compositeActivity")
145 End If
147 Dim nestedActivities As New ArrayList()
148 Dim compositeActivities As New Queue()
149 compositeActivities.Enqueue(compositeActivity)
150 While compositeActivities.Count > 0
151 Dim compositeActivity2 As CompositeActivity = CType(compositeActivities.Dequeue(), CompositeActivity)
153 For Each activity As Activity In compositeActivity2.Activities
154 nestedActivities.Add(activity)
155 If TypeOf activity Is CompositeActivity Then
156 compositeActivities.Enqueue(activity)
157 End If
158 next
160 For Each activity As Activity In compositeActivity2.EnabledActivities
161 If Not (nestedActivities.Contains(activity)) Then
162 nestedActivities.Add(activity)
163 If TypeOf activity Is CompositeActivity Then
164 compositeActivities.Enqueue(activity)
165 End If
166 End If
167 next
168 End While
169 Return CType(nestedActivities.ToArray(GetType(Activity)), Activity())
170 End Function
172 End Class
173 #End Region
175 #Region "WorkflowCompilerOptionsService"
176 Friend Class WorkflowCompilerOptionsService
177 Implements IWorkflowCompilerOptionsService
179 Public Sub New()
181 End Sub
183 #Region "IWorkflowCompilerOptionsService Members"
184 Public ReadOnly Property RootNamespace() As String Implements IWorkflowCompilerOptionsService.RootNamespace
186 Return String.Empty
187 End Get
188 End Property
190 Public ReadOnly Property Language() As String Implements IWorkflowCompilerOptionsService.Language
192 Return "VisualBasic"
193 End Get
194 End Property
196 Public ReadOnly Property CheckTypes() As Boolean Implements IWorkflowCompilerOptionsService.CheckTypes
198 Throw New Exception("The method or operation is not implemented.")
199 End Get
200 End Property
201 #End Region
202 End Class
204 #End Region
206 #Region "Class EventBindingService"
207 Friend Class EventBindingService
208 Implements IEventBindingService
210 Public Sub New()
211 End Sub
213 Public Function CreateUniqueMethodName(ByVal component As IComponent, ByVal e As EventDescriptor) As String Implements IEventBindingService.CreateUniqueMethodName
214 Return e.DisplayName
215 End Function
217 Public Function GetCompatibleMethods(ByVal e As EventDescriptor) As ICollection Implements IEventBindingService.GetCompatibleMethods
218 Return New ArrayList()
219 End Function
221 Public Function GetEvent(ByVal propertyDescriptor As PropertyDescriptor) As EventDescriptor Implements IEventBindingService.GetEvent
222 Return iif(TypeOf propertyDescriptor Is EventPropertyDescriptor, (CType(propertyDescriptor, EventPropertyDescriptor)).EventDescriptor, Nothing)
223 End Function
225 Public Function GetEventProperties(ByVal events As EventDescriptorCollection) As PropertyDescriptorCollection Implements IEventBindingService.GetEventProperties
226 Dim descriptors() As PropertyDescriptor = Nothing
227 Return New PropertyDescriptorCollection(descriptors, True)
228 End Function
230 Public Function GetEventProperty(ByVal e As EventDescriptor) As PropertyDescriptor Implements IEventBindingService.GetEventProperty
231 Return New EventPropertyDescriptor(e)
232 End Function
234 Public Function ShowCode() As Boolean Implements IEventBindingService.ShowCode
235 Return False
236 End Function
238 Public Function ShowCode(ByVal lineNumber As Integer) As Boolean Implements IEventBindingService.ShowCode
239 Return False
240 End Function
242 Public Function ShowCode(ByVal component As IComponent, ByVal e As EventDescriptor) As Boolean Implements IEventBindingService.ShowCode
243 Return False
244 End Function
246 Private Class EventPropertyDescriptor
247 Inherits PropertyDescriptor
249 Dim eventDescriptorValue As EventDescriptor
251 Public ReadOnly Property EventDescriptor() As EventDescriptor
253 Return Me.eventDescriptorValue
254 End Get
255 End Property
258 Public Sub New(ByVal eventDescriptor As EventDescriptor)
259 MyBase.New(eventDescriptor, Nothing)
260 Me.eventDescriptorValue = eventDescriptor
261 End Sub
263 Public Overrides ReadOnly Property ComponentType() As Type
265 Return Me.eventDescriptorValue.ComponentType
266 End Get
267 End Property
269 Public Overrides ReadOnly Property PropertyType() As Type
271 Return Me.eventDescriptorValue.EventType
272 End Get
273 End Property
275 Public Overrides ReadOnly Property IsReadOnly() As Boolean
277 Return True
278 End Get
279 End Property
281 Public Overrides Function CanResetValue(ByVal Component As Object) As Boolean
282 Return False
283 End Function
285 Public Overrides Function GetValue(ByVal component As Object) As Object
286 Return Nothing
287 End Function
289 Public Overrides Sub ResetValue(ByVal component As Object)
290 End Sub
292 Public Overrides Sub SetValue(ByVal component As Object, ByVal value As Object)
293 End Sub
295 Public Overrides Function ShouldSerializeValue(ByVal component As Object) As Boolean
296 Return False
297 End Function
298 End Class
300 End Class
302 #End Region