Imports System
Imports System.Drawing
Imports System.Drawing.Design
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms
Imports System.Text
Imports System.Reflection
Imports System.Windows.Forms.Design
Imports System.Text.RegularExpressions

Namespace UI.Design
    Public Class FormActionList
        Inherits DesignerActionList

        Public Form As Form

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
            Form = DirectCast(component, Form)
        End Sub

        Protected Function GetPropertyByName(ByVal propName As String) As PropertyDescriptor
            Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(Form)(propName)

            If pd Is Nothing Then
                Throw New ArgumentException("Matching property not found!", propName)
            Else
                Return pd
            End If
        End Function

        Public Sub SetDialogDefaults()
            GetPropertyByName("FormBorderStyle").SetValue(Form, FormBorderStyle.FixedDialog)
            GetPropertyByName("StartPosition").SetValue(Form, FormStartPosition.CenterParent)
            GetPropertyByName("MaximizeBox").SetValue(Form, False)
            GetPropertyByName("MinimizeBox").SetValue(Form, False)
            GetPropertyByName("ShowIcon").SetValue(Form, False)
            GetPropertyByName("ShowInTaskbar").SetValue(Form, False)
        End Sub

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionMethodItem(Me, "SetDialogDefaults", "Set Dialog Defaults", True))

            Return items
        End Function
    End Class

    Public Class ControlActionList
        Inherits DesignerActionList

        Protected Control As Control
        Protected DesignerActionService As DesignerActionService

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
            Control = DirectCast(component, Control)
            DesignerActionService = DirectCast(GetService(GetType(DesignerActionService)), DesignerActionService)
        End Sub

        Protected Sub FixControlName(ByRef value As String)
            For Each i As Char In ". :()"
                If value.Contains(i) Then
                    value = value.Replace(i, "")
                End If
            Next
        End Sub

        Protected Sub RefreshComponent()
            DirectCast(GetService(GetType(DesignerActionUIService)), DesignerActionUIService).Refresh(Component)
        End Sub

        Protected Sub SetPropertyValue(ByVal propertyName As String, ByVal value As Object)
            SetPropertyValue(Control, propertyName, value)
        End Sub

        Protected Sub SetPropertyValue(ByVal control As Control, ByVal propertyName As String, ByVal value As Object)
            Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(control)(propertyName)
            pd.SetValue(control, value)
        End Sub

        Protected Function GetPropertyValue(ByVal propertyName As String) As Object
            Dim pd As PropertyDescriptor = TypeDescriptor.GetProperties(Control)(propertyName)
            Return pd.GetValue(Control)
        End Function
    End Class

    Public Class TextBoxActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property Multiline() As Boolean
            Get
                Return DirectCast(Control, TextBox).Multiline
            End Get
            Set(ByVal value As Boolean)
                SetPropertyValue("Multiline", value)
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("Multiline", "Multiline"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))

            Return items
        End Function
    End Class

    Public Class ButtonActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Sub SetSize75x24()
            SetPropertyValue("Width", 75)
            SetPropertyValue("Height", 24)
        End Sub

        Public Sub OK()
            SetSize75x24()
            SetPropertyValue("Text", "OK")
            SetPropertyValue("Anchor", AnchorStyles.Right Or AnchorStyles.Bottom)
            SetPropertyValue("DialogResult", DialogResult.OK)
            SetPropertyValue("Name", "bOK")
            SetPropertyValue(Control.FindForm, "AcceptButton", Control)
            RefreshComponent()
        End Sub

        Public Sub Cancel()
            SetSize75x24()
            SetPropertyValue("Text", "Cancel")
            SetPropertyValue("Anchor", AnchorStyles.Right Or AnchorStyles.Bottom)
            SetPropertyValue("DialogResult", DialogResult.Cancel)
            SetPropertyValue("Name", "bCancel")
            SetPropertyValue(Control.FindForm, "CancelButton", Control)
            RefreshComponent()
        End Sub

        Public Sub Help()
            SetSize75x24()
            SetPropertyValue("Text", "Help")
            SetPropertyValue("Name", "bHelp")
            SetPropertyValue("TextImageRelation", TextImageRelation.ImageBeforeText)
            RefreshComponent()
        End Sub

        Public Sub Size24x24()
            SetPropertyValue("Width", 24)
            SetPropertyValue("Height", 24)
        End Sub

        Public Sub Defaults()
            SetSize75x24()
        End Sub

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property Text() As String
            Get
                Return Control.Text
            End Get
            Set(ByVal Value As String)
                SetPropertyValue("Text", Value)
                Dim name As String = "b" + Value
                FixControlName(name)
                SetPropertyValue("Name", name)
                RefreshComponent()
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Text", "Text"))
            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))
            items.Add(New DesignerActionMethodItem(Me, "OK", "OK", True))
            items.Add(New DesignerActionMethodItem(Me, "Cancel", "Cancel", True))
            items.Add(New DesignerActionMethodItem(Me, "Help", "Help", True))
            items.Add(New DesignerActionMethodItem(Me, "Size24x24", "24x24", True))
            items.Add(New DesignerActionMethodItem(Me, "SetSize75x24", "75x24", True))

            Return items
        End Function
    End Class

    Public Class LabelActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property Text() As String
            Get
                Return Control.Text
            End Get
            Set(ByVal Value As String)
                SetPropertyValue("Text", Value)
                Dim name As String = "l" + Value
                FixControlName(name)
                SetPropertyValue("Name", name)
                RefreshComponent()
            End Set
        End Property

        Public Property TextAlign() As ContentAlignment
            Get
                Return DirectCast(Control, Label).TextAlign
            End Get
            Set(ByVal Value As ContentAlignment)
                SetPropertyValue("TextAlign", Value)
            End Set
        End Property

        Public Property AutoSize() As Boolean
            Get
                Return DirectCast(Control, Label).AutoSize
            End Get
            Set(ByVal Value As Boolean)
                SetPropertyValue("AutoSize", Value)
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Text", "Text"))
            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("TextAlign", "TextAlign"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))
            items.Add(New DesignerActionPropertyItem("AutoSize", "AutoSize"))

            Return items
        End Function
    End Class

    Public Class CheckBoxActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property Text() As String
            Get
                Return Control.Text
            End Get
            Set(ByVal Value As String)
                SetPropertyValue("Text", Value)
                Dim name As String = "cb" + Value
                FixControlName(name)
                SetPropertyValue("Name", name)
                RefreshComponent()
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Text", "Text"))
            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))

            Return items
        End Function
    End Class

    Public Class GroupBoxActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Text() As String
            Get
                Return Control.Text
            End Get
            Set(ByVal Value As String)
                SetPropertyValue("Text", Value)
                Dim name As String = "gb" + Value
                FixControlName(name)
                SetPropertyValue("Name", name)
                RefreshComponent()
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Text", "Text"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))

            Return items
        End Function
    End Class

    Public Class ComboBoxActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property DropDownStyle() As ComboBoxStyle
            Get
                Return DirectCast(Control, ComboBox).DropDownStyle
            End Get
            Set(ByVal value As ComboBoxStyle)
                SetPropertyValue("DropDownStyle", value)
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("DropDownStyle", "DropDownStyle"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))

            Return items
        End Function
    End Class

    Public Class LineControlActionList
        Inherits ControlActionList

        Public Sub New(ByVal component As IComponent)
            MyBase.New(component)
        End Sub

        Public Property Text() As String
            Get
                Return Control.Text
            End Get
            Set(ByVal Value As String)
                SetPropertyValue("Text", Value)
                Dim name As String = "lc" + Value
                FixControlName(name)
                SetPropertyValue("Name", name)
                RefreshComponent()
            End Set
        End Property

        Public Property Name() As String
            Get
                Return Control.Name
            End Get
            Set(ByVal value As String)
                SetPropertyValue("Name", value)
            End Set
        End Property

        Public Property Width() As Integer
            Get
                Return Control.Width
            End Get
            Set(ByVal Value As Integer)
                SetPropertyValue("Width", Value)
            End Set
        End Property

        Public Property Height() As Integer
            Get
                Return Control.Height
            End Get
            Set(ByVal Value As Integer)
                SetPropertyValue("Height", Value)
            End Set
        End Property

        Public Property Anchor() As AnchorStyles
            Get
                Return Control.Anchor
            End Get
            Set(ByVal Value As AnchorStyles)
                SetPropertyValue("Anchor", Value)
            End Set
        End Property

        Public Sub FlipOrientation()
            Dim h As Integer = CInt(GetPropertyValue("Height"))
            Dim w As Integer = CInt(GetPropertyValue("Width"))
            SetPropertyValue("Height", w)
            SetPropertyValue("Width", h)
        End Sub

        Public Overrides Function GetSortedActionItems() As DesignerActionItemCollection
            Dim items As New DesignerActionItemCollection()

            items.Add(New DesignerActionPropertyItem("Text", "Text"))
            items.Add(New DesignerActionPropertyItem("Name", "Name"))
            items.Add(New DesignerActionPropertyItem("Width", "Width"))
            items.Add(New DesignerActionPropertyItem("Height", "Height"))
            items.Add(New DesignerActionPropertyItem("Anchor", "Anchor"))
            items.Add(New DesignerActionMethodItem(Me, "FlipOrientation", "Flip Orientation", True))

            Return items
        End Function
    End Class
End Namespace