Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Windows.Forms.Design

Namespace UI.Design
    Public Class LineControlDesigner
        Inherits ControlDesigner

        Private LineControl As LineControl

        Public Overrides Sub Initialize(ByVal component As IComponent)
            MyBase.Initialize(component)
            LineControl = CType(component, LineControl)
            ActionLists.Add(New LineControlActionList(LineControl))
        End Sub

        Public Overrides Sub InitializeNewComponent(ByVal defaultValues As IDictionary)
            MyBase.InitializeNewComponent(defaultValues)
            LineControl.Text = ""
            LineControl.Height = 10
            LineControl.Width = 200
            LineControl.TabStop = False
        End Sub
    End Class

    Public Class SmartTagProviderDesigner
        Inherits ComponentDesigner

        Private DesignerActionUIService As DesignerActionUIService

        Public Overrides Sub Initialize(ByVal component As IComponent)
            component.Site.Name = GetType(SmartTagProvider).Name
            MyBase.Initialize(component)
            Dim componentChangeService As IComponentChangeService = CType(GetService(GetType(IComponentChangeService)), IComponentChangeService)
            AddHandler ComponentChangeService.ComponentAdded, AddressOf OnComponentAdded
            AddLists(component)
        End Sub

        Private Sub OnComponentAdded(ByVal sender As Object, ByVal e As ComponentEventArgs)
            AddLists(e.Component)
        End Sub

        Private Function ContainsList(ByVal designer As ControlDesigner, ByVal type As Type) As Boolean
            For Each i As DesignerActionList In designer.ActionLists
                If i.GetType Is type Then
                    Return True
                End If
            Next
        End Function

        Private Sub AddLists(ByVal c As IComponent)
            Dim host As IDesignerHost = CType(c.Site.GetService(GetType(IDesignerHost)), IDesignerHost)

            If TypeOf host.RootComponent Is Form Then
                Dim cd As ControlDesigner = CType(host.GetDesigner(host.RootComponent), ControlDesigner)

                If Not ContainsList(cd, GetType(FormActionList)) Then
                    cd.ActionLists.Add(New FormActionList(host.RootComponent))
                End If
            End If

            For Each i As Component In host.RootComponent.Site.Container.Components
                If TypeOf i Is Control Then
                    If TypeOf host.GetDesigner(i) Is ControlDesigner Then
                        If TypeOf i Is Button Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(ButtonActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New ButtonActionList(i))
                            End If
                        ElseIf TypeOf i Is Label Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(LabelActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New LabelActionList(i))
                            End If
                        ElseIf TypeOf i Is TextBox Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(TextBoxActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New TextBoxActionList(i))
                            End If
                        ElseIf TypeOf i Is GroupBox Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(GroupBoxActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New GroupBoxActionList(i))
                            End If
                        ElseIf TypeOf i Is ComboBox Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(ComboBoxActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New ComboBoxActionList(i))
                            End If
                        ElseIf TypeOf i Is CheckBox Then
                            Dim cd As ControlDesigner = CType(host.GetDesigner(i), ControlDesigner)

                            If Not ContainsList(cd, GetType(CheckBoxActionList)) Then
                                cd.ActionLists.Clear()
                                cd.ActionLists.Add(New CheckBoxActionList(i))
                            End If
                        End If
                    End If
                End If
            Next
        End Sub
    End Class
End Namespace