Imports System.ComponentModel
Imports System.Drawing.Design

Imports Stax.UI

Namespace ComponentModel
    Public Class ListEditorForm
        Inherits Form

#Region " Windows Form Designer generated code "
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        Friend WithEvents lb As System.Windows.Forms.ListBox
        Friend WithEvents bCancel As System.Windows.Forms.Button
        Friend WithEvents bOK As System.Windows.Forms.Button
        Friend WithEvents pEditor As System.Windows.Forms.Panel
        Friend WithEvents bMore As Button
        Friend WithEvents bDown As System.Windows.Forms.Button
        Friend WithEvents bUp As System.Windows.Forms.Button
        Friend WithEvents bRemove As System.Windows.Forms.Button
        Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
        Friend WithEvents cmsOptions As System.Windows.Forms.ContextMenuStrip
        Friend WithEvents CutToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
        Friend WithEvents CopyToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
        Friend WithEvents PasteToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
        Friend WithEvents bAdd As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.lb = New System.Windows.Forms.ListBox
            Me.bCancel = New System.Windows.Forms.Button
            Me.bOK = New System.Windows.Forms.Button
            Me.pEditor = New System.Windows.Forms.Panel
            Me.bMore = New System.Windows.Forms.Button
            Me.bDown = New System.Windows.Forms.Button
            Me.bUp = New System.Windows.Forms.Button
            Me.bRemove = New System.Windows.Forms.Button
            Me.bAdd = New System.Windows.Forms.Button
            Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
            Me.cmsOptions = New System.Windows.Forms.ContextMenuStrip(Me.components)
            Me.CutToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
            Me.CopyToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
            Me.PasteToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
            Me.cmsOptions.SuspendLayout()
            Me.SuspendLayout()
            '
            'lb
            '
            Me.lb.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
            Me.lb.FormattingEnabled = True
            Me.lb.IntegralHeight = False
            Me.lb.Location = New System.Drawing.Point(13, 13)
            Me.lb.Name = "lb"
            Me.lb.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended
            Me.lb.Size = New System.Drawing.Size(251, 361)
            Me.lb.TabIndex = 0
            '
            'bCancel
            '
            Me.bCancel.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.bCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel
            Me.bCancel.Location = New System.Drawing.Point(451, 380)
            Me.bCancel.Name = "bCancel"
            Me.bCancel.Size = New System.Drawing.Size(75, 24)
            Me.bCancel.TabIndex = 2
            Me.bCancel.Text = "Cancel"
            '
            'bOK
            '
            Me.bOK.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.bOK.DialogResult = System.Windows.Forms.DialogResult.OK
            Me.bOK.Location = New System.Drawing.Point(370, 380)
            Me.bOK.Name = "bOK"
            Me.bOK.Size = New System.Drawing.Size(75, 24)
            Me.bOK.TabIndex = 3
            Me.bOK.Text = "OK"
            '
            'pEditor
            '
            Me.pEditor.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.pEditor.Location = New System.Drawing.Point(272, 13)
            Me.pEditor.Name = "pEditor"
            Me.pEditor.Size = New System.Drawing.Size(254, 361)
            Me.pEditor.TabIndex = 4
            '
            'bMore
            '
            Me.bMore.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
            Me.bMore.Location = New System.Drawing.Point(240, 380)
            Me.bMore.Name = "bMore"
            Me.bMore.Size = New System.Drawing.Size(24, 24)
            Me.bMore.TabIndex = 9
            '
            'bDown
            '
            Me.bDown.Image = Stax.My.Resources.Resources.Down16
            Me.bDown.Location = New System.Drawing.Point(135, 380)
            Me.bDown.Name = "bDown"
            Me.bDown.Size = New System.Drawing.Size(24, 24)
            Me.bDown.TabIndex = 14
            '
            'bUp
            '
            Me.bUp.Image = Stax.My.Resources.Resources.Up16
            Me.bUp.Location = New System.Drawing.Point(105, 380)
            Me.bUp.Name = "bUp"
            Me.bUp.Size = New System.Drawing.Size(24, 24)
            Me.bUp.TabIndex = 13
            '
            'bRemove
            '
            Me.bRemove.Image = Stax.My.Resources.Resources.Minus16
            Me.bRemove.Location = New System.Drawing.Point(42, 380)
            Me.bRemove.Name = "bRemove"
            Me.bRemove.Size = New System.Drawing.Size(24, 24)
            Me.bRemove.TabIndex = 12
            '
            'bAdd
            '
            Me.bAdd.Image = Stax.My.Resources.Resources.Plus16
            Me.bAdd.Location = New System.Drawing.Point(12, 380)
            Me.bAdd.Name = "bAdd"
            Me.bAdd.Size = New System.Drawing.Size(24, 24)
            Me.bAdd.TabIndex = 11
            '
            'cmsOptions
            '
            Me.cmsOptions.Enabled = True
            Me.cmsOptions.GripMargin = New System.Windows.Forms.Padding(2)
            Me.cmsOptions.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.CutToolStripMenuItem, Me.CopyToolStripMenuItem, Me.PasteToolStripMenuItem})
            Me.cmsOptions.Location = New System.Drawing.Point(24, 65)
            Me.cmsOptions.Name = "cmsOptions"
            Me.cmsOptions.RightToLeft = System.Windows.Forms.RightToLeft.No
            Me.cmsOptions.ShowImageMargin = False
            Me.cmsOptions.Size = New System.Drawing.Size(66, 70)
            '
            'CutToolStripMenuItem
            '
            Me.CutToolStripMenuItem.Name = "CutToolStripMenuItem"
            Me.CutToolStripMenuItem.Text = "Cut"
            '
            'CopyToolStripMenuItem
            '
            Me.CopyToolStripMenuItem.Name = "CopyToolStripMenuItem"
            Me.CopyToolStripMenuItem.Text = "Copy"
            '
            'PasteToolStripMenuItem
            '
            Me.PasteToolStripMenuItem.Name = "PasteToolStripMenuItem"
            Me.PasteToolStripMenuItem.Text = "Paste"
            '
            'ListEditorForm
            '
            Me.ClientSize = New System.Drawing.Size(538, 416)
            Me.Controls.Add(Me.bDown)
            Me.Controls.Add(Me.bUp)
            Me.Controls.Add(Me.bRemove)
            Me.Controls.Add(Me.bAdd)
            Me.Controls.Add(Me.bMore)
            Me.Controls.Add(Me.pEditor)
            Me.Controls.Add(Me.bOK)
            Me.Controls.Add(Me.bCancel)
            Me.Controls.Add(Me.lb)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "ListEditorForm"
            Me.ShowInTaskbar = False
            Me.Text = "Collection Editor"
            Me.cmsOptions.ResumeLayout(False)
            Me.ResumeLayout(False)

        End Sub

#End Region

        Private Editor As Control
        Private BlockListBoxSelectedIndexChanged As Boolean

        Public Sub New(ByVal list As IList, ByVal itemType As Type, ByVal title As String)
            MyBase.New()
            InitializeComponent()

            Text = title

            For Each i As Object In list
                lb.Items.Add(i)
            Next

            If lb.Items.Count > 0 Then
                lb.SelectedIndex = 0
            End If

            Me.ItemType = itemType

            UpdateControls()
        End Sub

        Private ItemTypeValue As Type

        Public Property ItemType() As Type
            Get
                Return ItemTypeValue
            End Get
            Set(ByVal Value As Type)
                ItemTypeValue = Value
            End Set
        End Property

        Private Sub lb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lb.SelectedIndexChanged
            If BlockListBoxSelectedIndexChanged Then
                Exit Sub
            End If

            UpdateControls()

            If Not lb.SelectedItem Is Nothing Then
                Dim a As ListItemEditorAttribute = CType(TypeDescriptor.GetAttributes(lb.SelectedItem).Item(GetType(ListItemEditorAttribute)), ListItemEditorAttribute)

                If a Is Nothing Then
                    a = New ListItemEditorAttribute(GetType(DefaultListEditorControl))
                End If

                If Editor Is Nothing Then
                    Dim c As Control = CType(Activator.CreateInstance(a.EditorType, New Object() {lb.SelectedItem.GetType}), Control)
                    c.Dock = DockStyle.Fill
                    c.BringToFront()
                    pEditor.Controls.Add(c)
                    Editor = c
                    AddHandler CType(c, IEditor).ValueChanged, AddressOf EditorValueChangedHandler
                End If

                If Not Editor.Visible Then
                    Editor.Visible = True
                End If

                CType(Editor, IEditor).SetSelectedObjects(New ArrayList(lb.SelectedItems).ToArray)
            End If
        End Sub

        Public Sub EditorValueChangedHandler()
            BlockListBoxSelectedIndexChanged = True

            For i As Integer = 0 To lb.Items.Count - 1
                If lb.SelectedIndices.Contains(i) Then
                    lb.Items(i) = lb.Items(i)
                    lb.SetSelected(i, True)
                End If
            Next

            BlockListBoxSelectedIndexChanged = False
        End Sub

        Private Sub bMore_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bMore.Click
            cmsOptions.Show(bMore, 0, bMore.Height)
        End Sub

        Private Sub UpdateControls()
            bRemove.Enabled = lb.SelectedIndex > -1
            bUp.Enabled = ListBoxHelp.CanMoveUp(lb)
            bDown.Enabled = ListBoxHelp.CanMoveDown(lb)
            CutToolStripMenuItem.Enabled = lb.SelectedIndex > -1
            CopyToolStripMenuItem.Enabled = lb.SelectedIndex > -1
        End Sub

        Private Sub bAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bAdd.Click
            lb.Items.Add(Activator.CreateInstance(ItemType))
            lb.SelectedIndex = -1
            lb.SelectedIndex = lb.Items.Count - 1
        End Sub

        Private Sub bDel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bRemove.Click
            ListBoxHelp.RemoveSelection(lb)

            If lb.Items.Count = 0 Then
                Editor.Visible = False
            End If
        End Sub

        Private Sub bUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bUp.Click
            ListBoxHelp.MoveSelectionUp(lb)
            bUp.Enabled = ListBoxHelp.CanMoveUp(lb)
            bDown.Enabled = ListBoxHelp.CanMoveDown(lb)
        End Sub

        Private Sub bDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bDown.Click
            ListBoxHelp.MoveSelectionDown(lb)
            bUp.Enabled = ListBoxHelp.CanMoveUp(lb)
            bDown.Enabled = ListBoxHelp.CanMoveDown(lb)
        End Sub

        Private Sub bMore_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles bMore.Paint
            ButtonHelp.DrawDropDownGlyph(bMore, e.Graphics, HorizontalAlignment.Center)
        End Sub

        Private Sub CutToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CutToolStripMenuItem.Click
            CopyToolStripMenuItem.PerformClick()
            bRemove.PerformClick()
        End Sub

        Private Sub CopyToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CopyToolStripMenuItem.Click
            If lb.SelectedItems.Count > 0 Then
                Clipboard.SetDataObject(New ArrayList(lb.SelectedItems))
            End If
        End Sub

        Private Sub PasteToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PasteToolStripMenuItem.Click
            If Clipboard.GetDataObject.GetDataPresent(GetType(ArrayList)) Then
                Dim al As ArrayList = CType(Clipboard.GetDataObject.GetData( _
                    GetType(ArrayList)), ArrayList)

                For Each i As Object In al
                    If i.GetType Is ItemType Then
                        lb.Items.Add(i)
                    End If
                Next
            End If
        End Sub
    End Class
End Namespace