Imports System.Reflection
Imports System.ComponentModel
Imports System.Globalization

Imports Stax.DynamicObject
Imports Stax.ComponentModel
Imports Stax.AppFramework

Namespace UI
    Public Class GenericMenuEditor
        Inherits ExtendedForm

#Region " Windows Form Designer generated code "
        'Form overrides dispose to clean up the component list.
        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.
        Public WithEvents lParams As System.Windows.Forms.Label
        Public WithEvents pg As System.Windows.Forms.PropertyGrid
        Public WithEvents cbMethod As System.Windows.Forms.ComboBox
        Public WithEvents lText As System.Windows.Forms.Label
        Public WithEvents bCancel As System.Windows.Forms.Button
        Public WithEvents bOK As System.Windows.Forms.Button
        Public WithEvents lShortcutKey As System.Windows.Forms.Label
        Public WithEvents lAction As System.Windows.Forms.Label
        Private WithEvents tbText As System.Windows.Forms.TextBox
        Friend WithEvents TipProvider As Stax.UI.TipProvider
        Friend WithEvents LineControl1 As Stax.UI.LineControl
        Friend WithEvents tv As Stax.UI.ExtendedTreeView
        Friend WithEvents SmartTagProvider As Stax.UI.Design.SmartTagProvider
        Friend WithEvents tbHotkey As System.Windows.Forms.TextBox
        Friend WithEvents ToolStrip As System.Windows.Forms.ToolStrip
        Friend WithEvents tsbNew As System.Windows.Forms.ToolStripButton
        Friend WithEvents ToolsToolStripDropDownButton As System.Windows.Forms.ToolStripDropDownButton
        Friend WithEvents NewFromDefaultsToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
        Friend WithEvents tsbCut As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbCopy As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbPaste As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbRemove As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbHelp As System.Windows.Forms.ToolStripButton
        Friend WithEvents ToolStripSeparator3 As System.Windows.Forms.ToolStripSeparator
        Friend WithEvents ToolStripSeparator4 As System.Windows.Forms.ToolStripSeparator
        Friend WithEvents ResetToolStripMenuItem As System.Windows.Forms.ToolStripMenuItem
        Friend WithEvents ToolStripItemExtender As Stax.UI.Design.ToolStripItemExtender
        Friend WithEvents tsbMoveLeft As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbMoveRight As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbMoveDown As System.Windows.Forms.ToolStripButton
        Friend WithEvents tsbMoveUp As System.Windows.Forms.ToolStripButton
        Friend WithEvents ToolStripSeparator1 As System.Windows.Forms.ToolStripSeparator
        Friend WithEvents ToolStripSeparator5 As System.Windows.Forms.ToolStripSeparator
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.components = New System.ComponentModel.Container
            Me.lShortcutKey = New System.Windows.Forms.Label
            Me.lParams = New System.Windows.Forms.Label
            Me.pg = New System.Windows.Forms.PropertyGrid
            Me.cbMethod = New System.Windows.Forms.ComboBox
            Me.tbText = New System.Windows.Forms.TextBox
            Me.lText = New System.Windows.Forms.Label
            Me.bCancel = New System.Windows.Forms.Button
            Me.bOK = New System.Windows.Forms.Button
            Me.lAction = New System.Windows.Forms.Label
            Me.LineControl1 = New Stax.UI.LineControl
            Me.tv = New Stax.UI.ExtendedTreeView
            Me.TipProvider = New Stax.UI.TipProvider(Me.components)
            Me.tbHotkey = New System.Windows.Forms.TextBox
            Me.SmartTagProvider = New Stax.UI.Design.SmartTagProvider
            Me.ToolStrip = New System.Windows.Forms.ToolStrip
            Me.tsbNew = New System.Windows.Forms.ToolStripButton
            Me.ToolStripSeparator3 = New System.Windows.Forms.ToolStripSeparator
            Me.tsbCut = New System.Windows.Forms.ToolStripButton
            Me.tsbCopy = New System.Windows.Forms.ToolStripButton
            Me.tsbPaste = New System.Windows.Forms.ToolStripButton
            Me.ToolStripSeparator4 = New System.Windows.Forms.ToolStripSeparator
            Me.tsbMoveLeft = New System.Windows.Forms.ToolStripButton
            Me.tsbMoveRight = New System.Windows.Forms.ToolStripButton
            Me.tsbMoveUp = New System.Windows.Forms.ToolStripButton
            Me.tsbMoveDown = New System.Windows.Forms.ToolStripButton
            Me.ToolStripSeparator1 = New System.Windows.Forms.ToolStripSeparator
            Me.tsbRemove = New System.Windows.Forms.ToolStripButton
            Me.ToolsToolStripDropDownButton = New System.Windows.Forms.ToolStripDropDownButton
            Me.NewFromDefaultsToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
            Me.ResetToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem
            Me.ToolStripSeparator5 = New System.Windows.Forms.ToolStripSeparator
            Me.tsbHelp = New System.Windows.Forms.ToolStripButton
            Me.ToolStripItemExtender = New Stax.UI.Design.ToolStripItemExtender
            CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.ToolStrip.SuspendLayout()
            CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).BeginInit()
            Me.SuspendLayout()
            '
            'lShortcutKey
            '
            Me.lShortcutKey.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.lShortcutKey.AutoSize = True
            Me.lShortcutKey.Location = New System.Drawing.Point(277, 77)
            Me.lShortcutKey.Name = "lShortcutKey"
            Me.lShortcutKey.Size = New System.Drawing.Size(67, 13)
            Me.lShortcutKey.TabIndex = 4
            Me.lShortcutKey.Text = "Shortcut Key:"
            '
            'lParams
            '
            Me.lParams.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.lParams.AutoSize = True
            Me.lParams.Location = New System.Drawing.Point(279, 173)
            Me.lParams.Name = "lParams"
            Me.lParams.Size = New System.Drawing.Size(59, 13)
            Me.lParams.TabIndex = 8
            Me.lParams.Text = "Parameters:"
            Me.lParams.Visible = False
            '
            'pg
            '
            Me.pg.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.pg.LineColor = System.Drawing.SystemColors.ScrollBar
            Me.pg.Location = New System.Drawing.Point(279, 189)
            Me.pg.Name = "pg"
            Me.pg.PropertySort = System.Windows.Forms.PropertySort.NoSort
            Me.pg.Size = New System.Drawing.Size(265, 205)
            Me.pg.TabIndex = 9
            Me.pg.ToolbarVisible = False
            Me.pg.Visible = False
            '
            'cbMethod
            '
            Me.cbMethod.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.cbMethod.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
            Me.cbMethod.FormattingEnabled = True
            Me.cbMethod.Location = New System.Drawing.Point(279, 141)
            Me.cbMethod.MaxDropDownItems = 30
            Me.cbMethod.Name = "cbMethod"
            Me.cbMethod.Size = New System.Drawing.Size(265, 21)
            Me.cbMethod.Sorted = True
            Me.cbMethod.TabIndex = 7
            Me.TipProvider.SetTipName(Me.cbMethod, "Action")
            Me.TipProvider.SetTipText(Me.cbMethod, "The action to be performed by the menu item.")
            '
            'tbText
            '
            Me.tbText.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.tbText.Location = New System.Drawing.Point(278, 46)
            Me.tbText.Name = "tbText"
            Me.tbText.Size = New System.Drawing.Size(266, 20)
            Me.tbText.TabIndex = 3
            Me.TipProvider.SetTipName(Me.tbText, "Text")
            Me.TipProvider.SetTipText(Me.tbText, "The display text. Enter minus to create a separator.")
            '
            'lText
            '
            Me.lText.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.lText.AutoSize = True
            Me.lText.Location = New System.Drawing.Point(279, 29)
            Me.lText.Name = "lText"
            Me.lText.Size = New System.Drawing.Size(27, 13)
            Me.lText.TabIndex = 2
            Me.lText.Text = "Text:"
            '
            '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(469, 416)
            Me.bCancel.Name = "bCancel"
            Me.bCancel.Size = New System.Drawing.Size(75, 24)
            Me.bCancel.TabIndex = 12
            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(388, 416)
            Me.bOK.Name = "bOK"
            Me.bOK.Size = New System.Drawing.Size(75, 24)
            Me.bOK.TabIndex = 11
            Me.bOK.Text = "OK"
            '
            'lAction
            '
            Me.lAction.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.lAction.AutoSize = True
            Me.lAction.Location = New System.Drawing.Point(279, 125)
            Me.lAction.Name = "lAction"
            Me.lAction.Size = New System.Drawing.Size(36, 13)
            Me.lAction.TabIndex = 6
            Me.lAction.Text = "Action:"
            '
            'LineControl1
            '
            Me.LineControl1.Anchor = CType(((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.LineControl1.Location = New System.Drawing.Point(279, 400)
            Me.LineControl1.Name = "LineControl1"
            Me.LineControl1.Size = New System.Drawing.Size(265, 10)
            Me.LineControl1.TabIndex = 10
            '
            'tv
            '
            Me.tv.AllowDrop = True
            Me.tv.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                        Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
            Me.tv.HideSelection = False
            Me.tv.Location = New System.Drawing.Point(13, 29)
            Me.tv.Name = "tv"
            Me.tv.SelectOnMouseDown = True
            Me.tv.Size = New System.Drawing.Size(259, 378)
            Me.tv.TabIndex = 1
            '
            'tbHotkey
            '
            Me.tbHotkey.AcceptsReturn = True
            Me.tbHotkey.AcceptsTab = True
            Me.tbHotkey.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
                        Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
            Me.tbHotkey.Location = New System.Drawing.Point(278, 93)
            Me.tbHotkey.Name = "tbHotkey"
            Me.tbHotkey.Size = New System.Drawing.Size(266, 20)
            Me.tbHotkey.TabIndex = 5
            Me.TipProvider.SetTipName(Me.tbHotkey, "Shortcut Key")
            Me.TipProvider.SetTipText(Me.tbHotkey, "Shortcut Key of the menu item. A key can be deleted by pressing it two times.")
            '
            'ToolStrip
            '
            Me.ToolStrip.Items.AddRange(New System.Windows.Forms.ToolStripItem() {Me.tsbNew, Me.ToolStripSeparator3, Me.tsbCut, Me.tsbCopy, Me.tsbPaste, Me.ToolStripSeparator4, Me.tsbMoveLeft, Me.tsbMoveRight, Me.tsbMoveUp, Me.tsbMoveDown, Me.ToolStripSeparator1, Me.tsbRemove, Me.ToolsToolStripDropDownButton, Me.ToolStripSeparator5, Me.tsbHelp})
            Me.ToolStrip.Location = New System.Drawing.Point(0, 0)
            Me.ToolStrip.Name = "ToolStrip"
            Me.ToolStrip.Size = New System.Drawing.Size(556, 25)
            Me.ToolStrip.TabIndex = 0
            Me.ToolStrip.Text = "ToolStrip"
            '
            'tsbNew
            '
            Me.tsbNew.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbNew.Image = Stax.My.Resources.Resources.New16
            Me.tsbNew.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbNew, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.N), System.Windows.Forms.Keys))
            Me.tsbNew.Name = "tsbNew"
            Me.tsbNew.Text = "New"
            Me.ToolStripItemExtender.SetTipText(Me.tsbNew, "Adds a new menu item as sub-item of the selected menu item.")
            Me.tsbNew.ToolTipText = "Adds a new menu item"
            '
            'ToolStripSeparator3
            '
            Me.ToolStripSeparator3.Name = "ToolStripSeparator3"
            '
            'tsbCut
            '
            Me.tsbCut.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbCut.Image = Stax.My.Resources.Resources.Cut16
            Me.tsbCut.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbCut, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.X), System.Windows.Forms.Keys))
            Me.tsbCut.Name = "tsbCut"
            Me.tsbCut.Text = "Cut"
            '
            'tsbCopy
            '
            Me.tsbCopy.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbCopy.Image = Stax.My.Resources.Resources.Copy16
            Me.tsbCopy.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbCopy, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.C), System.Windows.Forms.Keys))
            Me.tsbCopy.Name = "tsbCopy"
            Me.tsbCopy.Text = "Copy"
            '
            'tsbPaste
            '
            Me.tsbPaste.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbPaste.Image = Stax.My.Resources.Resources.Paste16
            Me.tsbPaste.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbPaste, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.V), System.Windows.Forms.Keys))
            Me.tsbPaste.Name = "tsbPaste"
            Me.tsbPaste.Text = "Paste"
            '
            'ToolStripSeparator4
            '
            Me.ToolStripSeparator4.Name = "ToolStripSeparator4"
            '
            'tsbMoveLeft
            '
            Me.tsbMoveLeft.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbMoveLeft.Image = Stax.My.Resources.Resources.Left16
            Me.tsbMoveLeft.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbMoveLeft, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Left), System.Windows.Forms.Keys))
            Me.tsbMoveLeft.Name = "tsbMoveLeft"
            Me.tsbMoveLeft.Text = "Move Left"
            Me.ToolStripItemExtender.SetTipText(Me.tsbMoveLeft, "Moves the selected menu item to the left.")
            '
            'tsbMoveRight
            '
            Me.tsbMoveRight.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbMoveRight.Image = Stax.My.Resources.Resources.Right16
            Me.tsbMoveRight.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbMoveRight, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Right), System.Windows.Forms.Keys))
            Me.tsbMoveRight.Name = "tsbMoveRight"
            Me.tsbMoveRight.Text = "Move Right"
            Me.ToolStripItemExtender.SetTipText(Me.tsbMoveRight, "Moves the selected menu item to the right.")
            '
            'tsbMoveUp
            '
            Me.tsbMoveUp.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbMoveUp.Image = Stax.My.Resources.Resources.Up16
            Me.tsbMoveUp.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbMoveUp, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Up), System.Windows.Forms.Keys))
            Me.tsbMoveUp.Name = "tsbMoveUp"
            Me.tsbMoveUp.Text = "Move Up"
            Me.ToolStripItemExtender.SetTipText(Me.tsbMoveUp, "Moves the selected menu item up.")
            '
            'tsbMoveDown
            '
            Me.tsbMoveDown.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbMoveDown.Image = Stax.My.Resources.Resources.Down16
            Me.tsbMoveDown.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbMoveDown, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Down), System.Windows.Forms.Keys))
            Me.tsbMoveDown.Name = "tsbMoveDown"
            Me.tsbMoveDown.Text = "Move Down"
            Me.ToolStripItemExtender.SetTipText(Me.tsbMoveDown, "Moves the selected menu item down.")
            '
            'ToolStripSeparator1
            '
            Me.ToolStripSeparator1.Name = "ToolStripSeparator1"
            '
            'tsbRemove
            '
            Me.tsbRemove.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbRemove.Image = Stax.My.Resources.Resources.Trash16
            Me.tsbRemove.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbRemove, CType((System.Windows.Forms.Keys.Control Or System.Windows.Forms.Keys.Delete), System.Windows.Forms.Keys))
            Me.tsbRemove.Name = "tsbRemove"
            Me.tsbRemove.Text = "Remove"
            Me.ToolStripItemExtender.SetTipText(Me.tsbRemove, "Removes the selected menu item.")
            '
            'ToolsToolStripDropDownButton
            '
            Me.ToolsToolStripDropDownButton.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.ToolsToolStripDropDownButton.DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem() {Me.NewFromDefaultsToolStripMenuItem, Me.ResetToolStripMenuItem})
            Me.ToolsToolStripDropDownButton.Image = Stax.My.Resources.Resources.Config16
            Me.ToolsToolStripDropDownButton.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolsToolStripDropDownButton.Name = "ToolsToolStripDropDownButton"
            Me.ToolsToolStripDropDownButton.Text = "Tools"
            '
            'NewFromDefaultsToolStripMenuItem
            '
            Me.NewFromDefaultsToolStripMenuItem.Name = "NewFromDefaultsToolStripMenuItem"
            Me.NewFromDefaultsToolStripMenuItem.Text = "New From Defaults..."
            Me.ToolStripItemExtender.SetTipText(Me.NewFromDefaultsToolStripMenuItem, "Creates a new menu item based on defaults.")
            '
            'ResetToolStripMenuItem
            '
            Me.ResetToolStripMenuItem.Name = "ResetToolStripMenuItem"
            Me.ResetToolStripMenuItem.Text = "Reset Everything"
            '
            'ToolStripSeparator5
            '
            Me.ToolStripSeparator5.Name = "ToolStripSeparator5"
            '
            'tsbHelp
            '
            Me.tsbHelp.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image
            Me.tsbHelp.Image = Stax.My.Resources.Resources.Help16
            Me.tsbHelp.ImageTransparentColor = System.Drawing.Color.Magenta
            Me.ToolStripItemExtender.SetKeyData(Me.tsbHelp, System.Windows.Forms.Keys.F1)
            Me.tsbHelp.Name = "tsbHelp"
            Me.tsbHelp.Text = "Help"
            '
            'ToolStripItemExtender
            '
            Me.ToolStripItemExtender.Owner = Me.tv
            '
            'GenericMenuEditor
            '
            Me.CancelButton = Me.bCancel
            Me.ClientSize = New System.Drawing.Size(556, 452)
            Me.Controls.Add(Me.ToolStrip)
            Me.Controls.Add(Me.tbHotkey)
            Me.Controls.Add(Me.tbText)
            Me.Controls.Add(Me.lAction)
            Me.Controls.Add(Me.lShortcutKey)
            Me.Controls.Add(Me.bOK)
            Me.Controls.Add(Me.lParams)
            Me.Controls.Add(Me.bCancel)
            Me.Controls.Add(Me.pg)
            Me.Controls.Add(Me.lText)
            Me.Controls.Add(Me.tv)
            Me.Controls.Add(Me.cbMethod)
            Me.Controls.Add(Me.LineControl1)
            Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
            Me.HelpButton = True
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Name = "GenericMenuEditor"
            Me.ShowInTaskbar = False
            Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent
            Me.Text = "Menu Editor"
            CType(Me.TipProvider, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ToolStrip.ResumeLayout(False)
            CType(Me.ToolStripItemExtender, System.ComponentModel.ISupportInitialize).EndInit()
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub

#End Region

        Private Block As Boolean
        Private GridTypeDescriptor As GridTypeDescriptor
        Private ClipboardNode As TreeNode

        Public Sub New(ByVal menu As GenericMenu)
            MyBase.New()
            InitializeComponent()

            GenericMenu = menu
            GridTypeDescriptor = New GridTypeDescriptor
            cbMethod.Items.Add(New MethodItem)
            cbMethod.Items.AddRange(New ArrayList(menu.MethodItems).ToArray)
            PopulateTreeView(menu.MenuItem.GetClone, Nothing)
            tv.ExpandAll()
        End Sub

        Private GenericMenuValue As GenericMenu

        Public Property GenericMenu() As GenericMenu
            Get
                Return GenericMenuValue
            End Get
            Set(ByVal Value As GenericMenu)
                GenericMenuValue = Value
            End Set
        End Property

        Private Sub tv_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles tv.DragDrop
            Block = False
        End Sub

        Private Sub tv_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs) Handles tv.DragEnter
            If e.Data.GetDataPresent(GetType(TreeNode)) Then
                e.Effect = DragDropEffects.Move
            End If
        End Sub

        Private Sub tv_ItemDrag(ByVal sender As Object, ByVal e As ItemDragEventArgs) Handles tv.ItemDrag
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Block = True
                DoDragDrop(e.Item, DragDropEffects.Move)
            End If
        End Sub

        Private Sub tv_DragOver(ByVal sender As Object, ByVal e As DragEventArgs) Handles tv.DragOver
            If e.Data.GetDataPresent(GetType(TreeNode)) Then
                Dim node As TreeNode = CType(e.Data.GetData(GetType(TreeNode)), TreeNode)
                Dim destNode As TreeNode = tv.GetNodeAt(tv.PointToClient(New Point(e.X, e.Y)))
                Dim nodes As New ArrayList
                AddNodes(node, nodes)

                If Not destNode Is node AndAlso Not destNode Is Nothing AndAlso _
                    Not nodes.Contains(destNode) Then

                    If Control.ModifierKeys = Keys.Control Then
                        If Not destNode.Nodes.Contains(node) AndAlso _
                            Not destNode.Text = "-" Then

                            node.Remove()
                            destNode.Nodes.Add(node)
                            destNode.ExpandAll()
                            tv.SelectedNode = node
                        End If
                    Else
                        If Not destNode.Parent Is Nothing Then
                            If destNode.Bounds.Top < node.Bounds.Top Then
                                node.Remove()

                                destNode.Parent.Nodes.Insert( _
                                    destNode.Parent.Nodes.IndexOf(destNode), node)

                                destNode.ExpandAll()
                                tv.SelectedNode = node
                            Else
                                node.Remove()

                                destNode.Parent.Nodes.Insert( _
                                    destNode.Parent.Nodes.IndexOf(destNode) + 1, node)

                                destNode.ExpandAll()
                                tv.SelectedNode = node
                            End If
                        End If
                    End If
                End If
            End If
        End Sub

        Private Sub UpdateControls()
            If Not Block Then
                Block = True

                Dim n As TreeNode = tv.SelectedNode

                If n Is Nothing Then
                    Exit Sub
                End If

                Dim item As GenericMenuItem = CType(n.Tag, GenericMenuItem)

                tbText.Text = item.Text
                tbHotkey.Text = KeysHelp.GetKeyString(item.KeyData)

                If Not cbMethod.SelectedItem Is Nothing AndAlso _
                    Not DirectCast(cbMethod.SelectedItem, MethodItem).MethodInfo Is Nothing AndAlso _
                    DirectCast(cbMethod.SelectedItem, MethodItem).MethodInfo.Name = item.MethodName Then

                    cbMethod_SelectedIndexChanged(Nothing, Nothing)
                Else
                    Dim found As Boolean

                    For Each i As MethodItem In GenericMenu.MethodItems
                        If i.MethodInfo.Name = item.MethodName Then
                            cbMethod.SelectedItem = i
                            found = True
                        End If
                    Next

                    If Not found Then
                        cbMethod.SelectedIndex = -1
                    End If
                End If

                Dim notRoot As Boolean = Not n.Parent Is Nothing
                tbText.Enabled = notRoot
                tbHotkey.Enabled = notRoot
                cbMethod.Enabled = notRoot
                tsbRemove.Enabled = notRoot
                tsbCut.Enabled = notRoot
                tsbCopy.Enabled = notRoot
                tsbPaste.Enabled = Not ClipboardNode Is Nothing AndAlso item.Text <> "-"
                Block = False
            End If
        End Sub

        Private Sub tbText_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles tbText.TextChanged
            If Not Block AndAlso Not tv.SelectedNode Is Nothing Then
                Dim item As GenericMenuItem = CType(tv.SelectedNode.Tag, _
                    GenericMenuItem)

                cbMethod.Enabled = tbText.Text <> "-"
                tbHotkey.Enabled = cbMethod.Enabled

                If tbText.Text = "-" Then
                    cbMethod.Text = ""
                    item.KeyData = Keys.None
                End If

                item.Text = tbText.Text
                tv.SelectedNode.Text = item.Text
            End If
        End Sub

        Public Function GetDescription(ByVal paramInfo As ParameterInfo) As String
            For Each i As Object In paramInfo.GetCustomAttributes(False)
                If TypeOf i Is DescriptionAttribute Then
                    Return CType(i, DescriptionAttribute).Description
                End If
            Next

            Return ""
        End Function

        Public Function GetName(ByVal paramInfo As ParameterInfo) As String
            For Each i As Object In paramInfo.GetCustomAttributes(False)
                If TypeOf i Is NameAttribute Then
                    Return CType(i, NameAttribute).Value
                End If
            Next

            Return ""
        End Function

        Public Function GetEditor(ByVal paramInfo As ParameterInfo) As Type
            For Each i As Object In paramInfo.GetCustomAttributes(False)
                If TypeOf i Is EditorAttribute Then
                    Return Type.GetType(CType(i, EditorAttribute).EditorTypeName)
                End If
            Next

            Return Nothing
        End Function

        Private Sub cbMethod_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles cbMethod.SelectedIndexChanged
            If Not tv.SelectedNode Is Nothing Then
                Dim item As GenericMenuItem = CType(tv.SelectedNode.Tag, GenericMenuItem)

                If Not Block Then
                    Dim selectedItem As MethodItem = DirectCast(cbMethod.SelectedItem, MethodItem)

                    If selectedItem.MethodInfo Is Nothing Then
                        item.MethodName = ""
                        item.Parameters = New List(Of Object)
                    Else
                        item.MethodName = selectedItem.MethodInfo.Name
                        item.Parameters = selectedItem.DefaultParams
                    End If
                End If

                Dim mi As MethodInfo = GenericMenu.GetMethod(item.MethodName)

                pg.Visible = Not mi Is Nothing AndAlso _
                    mi.GetParameters.Length > 0

                lParams.Visible = pg.Visible

                PopulateGrid(item)
            End If
        End Sub

        Private Sub PopulateGrid(ByVal item As GenericMenuItem)
            GridTypeDescriptor.Items.Clear()
            Dim mi As MethodInfo = GenericMenu.GetMethod(item.MethodName)

            If Not mi Is Nothing Then
                Dim params As ParameterInfo() = mi.GetParameters

                For i As Integer = 0 To params.Length - 1
                    Dim gp As New GridProperty
                    gp.Name = GetName(params(i))
                    gp.Value = item.Parameters(i)
                    gp.Description = GetDescription(params(i))
                    gp.TypeEditor = GetEditor(params(i))

                    GridTypeDescriptor.Add(gp)
                Next
            End If

            pg.SelectedObject = GridTypeDescriptor
        End Sub

        Private Sub pg_PropertyValueChanged(ByVal s As Object, ByVal e As PropertyValueChangedEventArgs) Handles pg.PropertyValueChanged
            If Not tv.SelectedNode Is Nothing Then
                Dim item As GenericMenuItem = CType(tv.SelectedNode.Tag, GenericMenuItem)
                item.Parameters.Clear()

                For Each i As DictionaryEntry In GridTypeDescriptor.Items
                    item.Parameters.Add(CType(i.Value, GridProperty).Value)
                Next
            End If
        End Sub

        Private Sub AddNodes(ByVal node As TreeNode, ByVal list As ArrayList)
            For Each i As TreeNode In node.Nodes
                list.Add(i)
                AddNodes(i, list)
            Next
        End Sub

        Private WasParamNodeSelected As Boolean

        Private Sub PopulateTreeView(ByVal item As GenericMenuItem, ByVal node As TreeNode)
            Dim newNode As New TreeNode(item.Text)
            newNode.Tag = item

            If node Is Nothing Then
                tv.Nodes.Add(newNode)
            Else
                node.Nodes.Add(newNode)
            End If

            If Not WasParamNodeSelected AndAlso item.Parameters.Count > 0 Then
                tv.SelectedNode = newNode
                WasParamNodeSelected = True
            End If

            For Each i As GenericMenuItem In item.SubItems
                PopulateTreeView(i, newNode)
            Next
        End Sub

        Public Function GetState() As GenericMenuItem
            Dim item As GenericMenuItem = CType(tv.Nodes(0).Tag, GenericMenuItem)
            BuildState(item, tv.Nodes(0))
            Return item
        End Function

        Private Sub BuildState(ByVal item As GenericMenuItem, ByVal node As TreeNode)
            item.SubItems.Clear()

            For Each i As TreeNode In node.Nodes
                Dim subItem As GenericMenuItem = CType(i.Tag, GenericMenuItem)
                item.SubItems.Add(subItem)
                BuildState(subItem, i)
            Next
        End Sub

        Protected Overridable Function GetTemplateForm(ByVal item As GenericMenuItem) As MenuTemplateForm
            Return New MenuTemplateForm(item)
        End Function

        Private Sub NewFromDefaultsToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles NewFromDefaultsToolStripMenuItem.Click
            If Not tv.SelectedNode Is Nothing Then
                Dim f As MenuTemplateForm = GetTemplateForm(GenericMenu.DefaultMenu.Invoke)

                If f.ShowDialog = Windows.Forms.DialogResult.OK Then
                    tv.SelectedNode.Nodes.Add(CType(f.TreeNode.Clone, TreeNode))
                    tv.SelectedNode.ExpandAll()
                End If
            End If
        End Sub

        Private Sub RemoveSelectedItem()
            If Not tv.SelectedNode Is Nothing AndAlso _
                Not tv.SelectedNode.Parent Is Nothing Then

                tv.SelectedNode.Remove()
            End If
        End Sub

        Private Sub tv_KeyUp(ByVal sender As Object, ByVal e As KeyEventArgs) Handles tv.KeyUp
            UpdateControls()
        End Sub

        Private Sub tbHotkey_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles tbHotkey.KeyDown
            If Not Block AndAlso Not tv.SelectedNode Is Nothing AndAlso _
                Not e.KeyCode = Keys.ControlKey AndAlso _
                Not e.KeyCode = Keys.Menu AndAlso _
                Not e.KeyCode = Keys.ShiftKey Then

                Dim item As GenericMenuItem = CType(tv.SelectedNode.Tag, GenericMenuItem)

                If item.KeyData = e.KeyData Then
                    item.KeyData = Keys.None
                    tbHotkey.Text = KeysHelp.GetKeyString(item.KeyData)
                Else
                    item.KeyData = e.KeyData
                    tbHotkey.Text = KeysHelp.GetKeyString(item.KeyData)

                    For Each i As TreeNode In TreeViewHelp.GetNodes(tv.Nodes)
                        If TypeOf i.Tag Is GenericMenuItem Then
                            Dim current As GenericMenuItem = CType(i.Tag, GenericMenuItem)

                            If current.KeyData = item.KeyData AndAlso Not item Is current Then
                                current.KeyData = Keys.None
                                Msg.Show(KeysHelp.GetKeyString(item.KeyData) + " detached from " + current.Text.TrimEnd("."c) + " and assigned to " + item.Text.TrimEnd("."c) + " instead.", MsgIcon.Info)
                            End If
                        End If
                    Next
                End If
            End If

            e.Handled = True
        End Sub

        Private Sub tbHotkey_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles tbHotkey.KeyPress
            e.Handled = True
        End Sub

        Private Sub tv_AfterSelect(ByVal sender As Object, ByVal e As TreeViewEventArgs) Handles tv.AfterSelect
            UpdateControls()
        End Sub

        Private Sub tsbNew_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbNew.Click
            If Not tv.SelectedNode Is Nothing Then
                Dim newNode As New TreeNode
                newNode.Text = "???"
                newNode.Tag = New GenericMenuItem("???")
                tv.SelectedNode.Nodes.Add(newNode)
                tv.SelectedNode = newNode
            End If
        End Sub

        Private Sub tsbCut_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbCut.Click
            If Not tv.SelectedNode Is Nothing Then
                tsbCopy.PerformClick()
                RemoveSelectedItem()
            End If
        End Sub

        Private Sub tsbCopy_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbCopy.Click
            If Not tv.SelectedNode Is Nothing Then
                ClipboardNode = CType(ObjectHelp.GetCopy( _
                    tv.SelectedNode), TreeNode)

                UpdateControls()
            End If
        End Sub

        Private Sub tsbPaste_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbPaste.Click
            If Not tv.SelectedNode Is Nothing Then
                tv.SelectedNode.Nodes.Add(CType(ObjectHelp.GetCopy(ClipboardNode), TreeNode))
                tv.SelectedNode.ExpandAll()
            End If
        End Sub

        Private Sub tsbRemove_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbRemove.Click
            If Msg.Show("Please confirm to remove the menu item.", "Remove", _
                MsgIcon.Question, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then

                RemoveSelectedItem()
            End If
        End Sub

        Private Sub tsbHelp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbHelp.Click
            Dim methods As New StringPairList

            For Each i As MethodItem In Me.GenericMenu.MethodItems
                If Not i.Attribute.Description Is Nothing Then
                    methods.Add(New StringPair(i.Attribute.Name, i.Attribute.Description))
                End If
            Next

            Dim f As New HelpForm()
            f.Document.WriteStart(Text)
            f.Document.WriteParagraph("The menu editor allows to customize the text, location, shortcut key and action of a menu item. Menu items can be rearranged with **Drag &amp; Drop**. Pressing Ctrl while dragging moves as sub-item.")
            f.Document.WriteTips(ToolStripItemExtender.GetTips, TipProvider.GetTips)
            f.Document.WriteTable("Actions", methods)
            f.Document.WriteTable("Keys", ToolStripItemExtender.GetKeys, False)
            f.Show()
        End Sub

        Private Sub ResetToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ResetToolStripMenuItem.Click
            If Msg.Show("Please confirm to reset the entire menu.", "Reset", _
                MsgIcon.Question, MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK Then

                tv.Nodes.Clear()
                PopulateTreeView(GenericMenu.DefaultMenu.Invoke, Nothing)
                tv.ExpandAll()
                tv.SelectedNode = tv.Nodes(0)
            End If
        End Sub

        Private Sub tsbMoveLeft_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbMoveLeft.Click
            ShowDragAndDropHint()

            Dim n As TreeNode = tv.SelectedNode

            If Not n Is Nothing AndAlso n.Parent.Parent Is Nothing Then
                Exit Sub
            End If

            tv.MoveSelectionLeft()
        End Sub

        Private Sub tsbMoveRight_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbMoveRight.Click
            ShowDragAndDropHint()
            tv.MoveSelectionRight()
        End Sub

        Private Sub tsbMoveUp_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbMoveUp.Click
            ShowDragAndDropHint()

            Dim n As TreeNode = tv.SelectedNode

            If Not n Is Nothing AndAlso _
                Not n.Parent Is Nothing AndAlso _
                n.Parent.Parent Is Nothing AndAlso _
                n.Index = 0 Then

                Exit Sub
            End If

            tv.MoveSelectionUp()
        End Sub

        Private Sub tsbMoveDown_Click(ByVal sender As Object, ByVal e As EventArgs) Handles tsbMoveDown.Click
            ShowDragAndDropHint()

            Dim n As TreeNode = tv.SelectedNode

            If Not n Is Nothing AndAlso _
                n.Parent.Parent Is Nothing AndAlso _
                n.NextNode Is Nothing Then

                Exit Sub
            End If

            tv.MoveSelectionDown()
        End Sub

        Private TipCounter As Integer

        Private Sub ShowDragAndDropHint()
            TipCounter += 1

            If TipCounter > 5 Then
                Msg.Show("Note that menu items can be rearranged with Drag & Drop." + vbCrLf + "Pressing Ctrl while dragging moves as sub-item.", "Useability Tip", StaxApplication.StringList)
            End If
        End Sub

        Private Sub GenericMenuEditor_ActivatedFirstTime() Handles Me.ActivatedFirstTime
            If Not StaxApplication.StringList.Contains(Me.GetType.FullName) Then
                tsbHelp.PerformClick()
                StaxApplication.StringList.Add(Me.GetType.FullName)
            End If
        End Sub
    End Class
End Namespace