Nanook.QueenBee.EditorForm.lstQbItems_MouseUp C# (CSharp) Method

lstQbItems_MouseUp() private method

private lstQbItems_MouseUp ( object sender, MouseEventArgs e ) : void
sender object
e MouseEventArgs
return void
        private void lstQbItems_MouseUp(object sender, MouseEventArgs e)
        {
            try
            {
                if (lstQbItems.SelectedItems.Count == 0)
                    gboEdit.Controls.Clear();

                if (e.Button != MouseButtons.Right)
                    return;

                _addItemParent = null;
                _addItemSibling = null;

                int sIdx = lstQbItems.SelectedItems.Count > 0 ? lstQbItems.SelectedIndices[0] : -1;
                int pIdx; //parent index
                ToolStripMenuItem mi;
                QbItemType[] types = null;

                if (sIdx != -1)
                {
                    ListViewItem li = lstQbItems.SelectedItems[0];
                    _addItemSibling = (QbItemBase)li.Tag;

                    //find parent list item
                    if (li.IndentCount != 0)
                    {
                        pIdx = sIdx;
                        while (lstQbItems.Items[--pIdx].IndentCount != li.IndentCount - 1) ;
                        _addItemParent = (QbItemBase)lstQbItems.Items[pIdx].Tag;
                    }

                    for (int m = mnuInsertSibling.DropDownItems.Count - 1; m >= 0; m--)
                    {
                        mi = (ToolStripMenuItem)mnuInsertSibling.DropDownItems[m];
                        mi.Click -= new EventHandler(qbAddItemInsertSibling_Click);
                        mnuInsertSibling.DropDownItems.Remove(mi);
                    }

                    for (int m = mnuAddSibling.DropDownItems.Count - 1; m >= 0; m--)
                    {
                        mi = (ToolStripMenuItem)mnuAddSibling.DropDownItems[m];
                        mi.Click -= new EventHandler(qbAddItemAddSibling_Click);
                        mnuAddSibling.DropDownItems.Remove(mi);
                    }
                }

                //remove items and remove event handlers
                for (int m = mnuAddChild.DropDownItems.Count - 1; m >= 0; m--)
                {
                    mi = (ToolStripMenuItem)mnuAddChild.DropDownItems[m];
                    mi.Click -= new EventHandler(qbAddItemChild_Click);
                    mnuAddChild.DropDownItems.Remove(mi);
                }

                bool pasteAddChild = false;
                bool pasteInsertSibling = false;
                bool pasteAddSibling = false;

                //add new menu items
                types = QbFile.SupportedChildTypes(_addItemSibling == null ? QbItemType.Root : _addItemSibling.QbItemType);
                foreach (QbItemType qbt in types)
                {
                    string n = getMenuTypename(qbt);

                    if (_copyItem != null && qbt == _copyItem.QbItemType)
                        pasteAddChild = true;

                    mi = (ToolStripMenuItem)mnuAddChild.DropDownItems.Add(n, imgList.Images[getQbItemImageIndex(qbt)], qbAddItemChild_Click);
                    mi.Tag = qbt; // sIdx; //store selected index in list
                }

                if (sIdx != -1)
                {
                    types = QbFile.SupportedChildTypes(_addItemParent == null || _addItemSibling.QbItemType == QbItemType.Unknown ? QbItemType.Root : _addItemParent.QbItemType);
                    foreach (QbItemType qbt in types)
                    {
                        string n = getMenuTypename(qbt);

                        if (_copyItem != null && qbt == _copyItem.QbItemType)
                        {
                            pasteAddSibling = true;
                            if (_addItemSibling.QbItemType != QbItemType.Unknown) //cannot insert before unknown
                                pasteInsertSibling = true;
                        }

                        if (_addItemSibling.QbItemType != QbItemType.Unknown)
                        {
                            mi = (ToolStripMenuItem)mnuInsertSibling.DropDownItems.Add(n, imgList.Images[getQbItemImageIndex(qbt)], qbAddItemInsertSibling_Click);
                            mi.Tag = qbt; // sIdx; //store selected index in list
                        }
                        mi = (ToolStripMenuItem)mnuAddSibling.DropDownItems.Add(n, imgList.Images[getQbItemImageIndex(qbt)], qbAddItemAddSibling_Click);
                        mi.Tag = qbt; // sIdx; //store selected index in list
                    }
                }

                mnuQbEdit.Tag = sIdx;

                mnuAddChild.Enabled = (mnuAddChild.DropDownItems.Count != 0);
                mnuInsertSibling.Enabled = sIdx != -1 && (mnuInsertSibling.DropDownItems.Count != 0);
                mnuAddSibling.Enabled = sIdx != -1 && (mnuAddSibling.DropDownItems.Count != 0);
                mnuRemoveSibling.Enabled = _addItemSibling != null && _addItemSibling.QbItemType != QbItemType.Unknown;

                mnuPasteAddChild.Enabled = pasteAddChild && mnuAddChild.Enabled;
                mnuPasteInsertSibling.Enabled = pasteInsertSibling && mnuInsertSibling.Enabled;
                mnuPasteAddSibling.Enabled = pasteAddSibling && mnuAddSibling.Enabled;

                mnuCopyItems.Enabled = _addItemSibling != null && _addItemSibling.QbItemType != QbItemType.Unknown;
                if (_addItemSibling != null) //selected item
                    mnuCopyItems.Text = string.Format("Copy ({0})", getMenuTypename(_addItemSibling.QbItemType));
                else
                    mnuCopyItems.Text = "Copy";

                mnuPasteItems.Enabled = _copyItem != null;
                if (_copyItem != null)
                    mnuPasteItems.Text = string.Format("Paste ({0})", getMenuTypename(_copyItem.QbItemType));
                else
                    mnuPasteItems.Text = "Paste";

                mnuQbEdit.Show(lstQbItems, e.Location);
            }
            catch (Exception ex)
            {
                showException("Failed to create menu items.", ex);
                return;
            }
        }
EditorForm