// ----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
// 
// Copyright (c) Microsoft Corporation. All rights reserved.
// 
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
// ----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
// ----------------------------------------------------------------------------------

namespace Microsoft.Samples.WindowsAzureMmc.SnapIn.Forms
{
    using System;
    using System.Collections.ObjectModel;
    using System.Windows.Forms;
    using Microsoft.ManagementConsole;
    using Microsoft.Samples.WindowsAzureMmc.Model.Storage;
    using Microsoft.Samples.WindowsAzureMmc.SnapIn.Views;

    public partial class QueueControl : UserControl
    {
        private Collection<AzureMessage> displayedMessages = new Collection<AzureMessage>();
        private Collection<AzureQueue> displayedQueues = new Collection<AzureQueue>();
        private QueueFormView queueFormView = null;

        // Action items
        private Microsoft.ManagementConsole.Action _queueProperties = null;
        private Microsoft.ManagementConsole.Action _messageProperties = null;
        private Microsoft.ManagementConsole.Action _messageAdd = null;
        private Microsoft.ManagementConsole.Action _deleteQueue = null;
        private Microsoft.ManagementConsole.Action _purgeQueue = null;
        private ActionGroup _viewQueue = null;
        private ActionGroup _viewMessage = null;
        private ActionSeparator _sepMessage = null;
        private ActionSeparator _sepQueue = null;
        private ActionSeparator _sepViewQueue = null;

        private const int MINCOLSIZE = 40;
        private const int LVCOLPADDING = 4;

        private const string AUTOSIZEQUEUES = "Path";
        private const string AUTOSIZEMESSAGES = "ID";

        private ListViewColumnSorter.Types[] QUEUES_LV_TYPES = new ListViewColumnSorter.Types[] { ListViewColumnSorter.Types.String, ListViewColumnSorter.Types.String, ListViewColumnSorter.Types.Int, ListViewColumnSorter.Types.Int, ListViewColumnSorter.Types.Date };
        private ListViewColumnSorter.Types[] MESSAGES_LV_TYPES = new ListViewColumnSorter.Types[] { ListViewColumnSorter.Types.String, ListViewColumnSorter.Types.Date, ListViewColumnSorter.Types.String };

        public event ActionNamedItemDelegate GetMessagesForQueue;
        
        public event ActionDelegate UpdateQueues;
        
        public event ActionDelegate AddNewQueue;
        
        public event ActionNamedItemDelegate AddMessage;
        
        public event ActionNamedItemDelegate DeleteQueue;
        
        public event ActionNamedItemDelegate PurgeQueue;
        
        public event ActionShowRightClickDelegate ShowRightClick;

        public QueueControl()
        {
            InitializeComponent();
            queuesLV.ListViewItemSorter = new ListViewColumnSorter();
            messagesLV.ListViewItemSorter = new ListViewColumnSorter();
        }

        public void SetupView(FormView view)
        {
            // Add Message
            _messageAdd = new Microsoft.ManagementConsole.Action("Add Message", "Add a message to the current queue");
            _messageAdd.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(messageAdd_Triggered);
            view.SelectionData.ActionsPaneItems.Add(_messageAdd);

            // Add Queue
            Microsoft.ManagementConsole.Action addContainer = new Microsoft.ManagementConsole.Action("Add Queue", "Add a queue to Azure");
            addContainer.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(addQueue_Triggered);
            view.ScopeNode.ActionsPaneItems.Add(addContainer);

            // Purge Queue
            _purgeQueue = new Microsoft.ManagementConsole.Action("Purge", "Purge the selected queue messages");
            _purgeQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(purgeQueue_Triggered);
            view.SelectionData.ActionsPaneItems.Add(_purgeQueue);

            view.ScopeNode.ActionsPaneItems.Add(new ActionSeparator());

            // Refresh
            Microsoft.ManagementConsole.Action refreshContainers = new Microsoft.ManagementConsole.Action("Refresh", "Refresh the view");
            refreshContainers.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(refreshContainers_Triggered);
            view.ScopeNode.ActionsPaneItems.Add(refreshContainers);

            // Delete Queue
            _deleteQueue = new Microsoft.ManagementConsole.Action("Delete", "Delete the selected queue");
            _deleteQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(deleteQueue_Triggered);
            view.SelectionData.ActionsPaneItems.Add(_deleteQueue);

            // Separators
            _sepViewQueue = new ActionSeparator();
            view.SelectionData.ActionsPaneItems.Add(_sepViewQueue);

            // Setup Queue view actions
            _viewQueue = new ActionGroup("View", "Change the view type for the queue list");
            
            // Large Icon
            Microsoft.ManagementConsole.Action largeIconQueue = new Microsoft.ManagementConsole.Action("Large Icon", "Change the view to large icons");
            largeIconQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(largeIconQueue_Click);
            _viewQueue.Items.Add(largeIconQueue);
            
            // Small Icon
            Microsoft.ManagementConsole.Action smallIconQueue = new Microsoft.ManagementConsole.Action("Small Icon", "Change the view to small icons");
            smallIconQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(smallIconQueue_Click);
            _viewQueue.Items.Add(smallIconQueue);
            
            // List
            Microsoft.ManagementConsole.Action listQueue = new Microsoft.ManagementConsole.Action("List", "Change the view to list");
            listQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(listQueue_Click);
            _viewQueue.Items.Add(listQueue);
            
            // Details
            Microsoft.ManagementConsole.Action detailsQueue = new Microsoft.ManagementConsole.Action("Details", "Change the view to details");
            detailsQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(detailsQueue_Click);
            _viewQueue.Items.Add(detailsQueue);
            
            // tile
            Microsoft.ManagementConsole.Action tileQueue = new Microsoft.ManagementConsole.Action("Tile", "Change the view to tile");
            tileQueue.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(tileQueue_Click);
            _viewQueue.Items.Add(tileQueue);
            view.SelectionData.ActionsPaneItems.Add(_viewQueue);

            // Setup Message view actions
            _viewMessage = new ActionGroup("View", "Change the view type for the message list");
            
            // Large Icon
            Microsoft.ManagementConsole.Action largeIconMessage = new Microsoft.ManagementConsole.Action("Large Icon", "Change the view to large icons");
            largeIconMessage.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(largeIconMessage_Click);
            _viewMessage.Items.Add(largeIconMessage);
            
            // Small Icon
            Microsoft.ManagementConsole.Action smallIconMessage = new Microsoft.ManagementConsole.Action("Small Icon", "Change the view to small icons");
            smallIconMessage.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(smallIconMessage_Click);
            _viewMessage.Items.Add(smallIconMessage);
            
            // List
            Microsoft.ManagementConsole.Action listMessage = new Microsoft.ManagementConsole.Action("List", "Change the view to list");
            listMessage.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(listMessage_Click);
            _viewMessage.Items.Add(listMessage);
            
            // Details
            Microsoft.ManagementConsole.Action detailsMessage = new Microsoft.ManagementConsole.Action("Details", "Change the view to details");
            detailsMessage.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(detailsMessage_Click);
            _viewMessage.Items.Add(detailsMessage);
            
            // Tile
            Microsoft.ManagementConsole.Action tileMessage = new Microsoft.ManagementConsole.Action("Tile", "Change the view to tile");
            tileMessage.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(tileMessage_Click);
            _viewMessage.Items.Add(tileMessage);

            // Separators
            _sepMessage = new ActionSeparator();
            _sepQueue = new ActionSeparator();
            view.SelectionData.ActionsPaneItems.Add(_sepQueue);

            // Queue properties
            _queueProperties = new Microsoft.ManagementConsole.Action("Properties", "Get the properties for the selected queue");
            _queueProperties.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(queueProperties_Triggered);
            view.SelectionData.ActionsPaneItems.Add(_queueProperties);

            // Message Properties
            _messageProperties = new Microsoft.ManagementConsole.Action("Properties", "Get the properties for the selected message");
            _messageProperties.Triggered += new Microsoft.ManagementConsole.Action.ActionEventHandler(messageProperties_Triggered);

            //Set the view to the private variable, to be accessed later
            queueFormView = (QueueFormView)view;
        }

        private void largeIconQueue_Click(object sender, ActionEventArgs e)
        {
            queuesLV.View = System.Windows.Forms.View.LargeIcon;
        }

        private void smallIconQueue_Click(object sender, ActionEventArgs e)
        {
            queuesLV.View = System.Windows.Forms.View.SmallIcon;
        }

        private void detailsQueue_Click(object sender, ActionEventArgs e)
        {
            queuesLV.View = System.Windows.Forms.View.Details;
        }

        private void listQueue_Click(object sender, ActionEventArgs e)
        {
            queuesLV.View = System.Windows.Forms.View.List;
        }

        private void tileQueue_Click(object sender, ActionEventArgs e)
        {
            queuesLV.View = System.Windows.Forms.View.Tile;
        }

        private void largeIconMessage_Click(object sender, ActionEventArgs e)
        {
            messagesLV.View = System.Windows.Forms.View.LargeIcon;
        }

        private void smallIconMessage_Click(object sender, ActionEventArgs e)
        {
            messagesLV.View = System.Windows.Forms.View.SmallIcon;
        }

        private void detailsMessage_Click(object sender, ActionEventArgs e)
        {
            messagesLV.View = System.Windows.Forms.View.Details;
        }

        private void listMessage_Click(object sender, ActionEventArgs e)
        {
            messagesLV.View = System.Windows.Forms.View.List;
        }

        private void tileMessage_Click(object sender, ActionEventArgs e)
        {
            messagesLV.View = System.Windows.Forms.View.Tile;
        }
        
        private void purgeQueue_Triggered(object sender, ActionEventArgs e)
        {
            // Retrieve the selected queue
            AzureQueue queue = getSelectedQueue();
            if (queue != null)
            {

                if (MessageBox.Show(this, "Are you sure you want to delete all messages in this queue?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {

                    // Purge the queue
                    PurgeQueue(queue.Name);
                    UpdateQueues();
                    GetMessagesForQueue(queue.Name);
                }
            }
        }

        private void deleteQueue_Triggered(object sender, ActionEventArgs e)
        {
            // Retrieve the selected queue
            AzureQueue queue = getSelectedQueue();
            if (queue != null)
            {
                if (MessageBox.Show(this, "Are you sure you want to delete this queue and all messages it's messages?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DeleteQueue(queue.Name);
                    UpdateQueues();
                    GetMessagesForQueue(string.Empty);
                }
            }
        }

        private void addQueue_Triggered(object sender, ActionEventArgs e)
        {
            AddNewQueue();
            UpdateQueues();
        }

        private void refreshContainers_Triggered(object sender, ActionEventArgs e)
        {
            UpdateQueues();
        }

        private void queueProperties_Triggered(object sender, ActionEventArgs e)
        {
            if (queuesLV.SelectedItems.Count == 1)
            {

                AzureQueue q = getSelectedQueue();

                using (PropertiesViewer prop = new PropertiesViewer(q, true))
                {
                    if (prop.ShowDialog() == DialogResult.OK)
                    {
                        // save the changes to the queue properties
                        // if (SaveQueue != null)
                        //  SaveQueue(sender, q);
                    }
                    else
                    {
                        // better refresh in case they changed the object but did not click save
                        UpdateQueues();
                    }
                }
            }
        }
        
        private void messageProperties_Triggered(object sender, ActionEventArgs e)
        {
            if (messagesLV.SelectedItems.Count == 1)
            {

                using (PropertiesViewer prop = new PropertiesViewer(getSelectedMessage(), false))
                {
                    prop.ShowDialog();
                }
            }
        }

        private void messageAdd_Triggered(object sender, ActionEventArgs e)
        {
            //Retrieve the selected queue
            AzureQueue queue = getSelectedQueue();
            if (queue != null)
            {
                //Add a new queue
                AddMessage(queue.Name);
                //Get the messages for the queue
                GetMessagesForQueue(queue.Name);
            }
        }

        private void queuesLV_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (queuesLV.SelectedItems.Count == 0)
                queueFormView.SelectionData.Clear();
            if (queuesLV.SelectedItems.Count == 1)
            {
                // Get the currently selected queue
                AzureQueue queue = getSelectedQueue();

                // Update MMC with the current selection information
                queueFormView.SelectionData.Update(queue, false, null, null);

                if (queue != null)
                {
                    // Update the title of the selected data menu in the actions pane
                    queueFormView.SelectionData.DisplayName = "Queue: " + queue.Name;
                    // Get the messages for the selected queue
                    GetMessagesForQueue(queue.Name);
                    // Hide and Show required buttons
                    showQueueActions();
                }
            }
        }

        private void queuesLV_MouseClick(object sender, MouseEventArgs e)
        {
            // Get the currently selected queue
            AzureQueue queue = getSelectedQueue();

            // Update MMC with the current selection information
            queueFormView.SelectionData.Update(queue, false, null, null);

            if (queue != null)
            {
                // Update the title of the selected data menu in the actions pane
                queueFormView.SelectionData.DisplayName = "Queue: " + queue.Name;
                // Hide and Show required buttons
                showQueueActions();
            }
            if (e.Button == MouseButtons.Right)
            {
                ShowRightClick(queuesLV.PointToScreen(e.Location));
            }
        }

        private void messagesLV_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (messagesLV.SelectedItems.Count == 0)
            {
                queueFormView.SelectionData.Clear();
            }

            if (messagesLV.SelectedItems.Count == 1)
            {
                // Get the currently selected container
                AzureMessage msg = getSelectedMessage();
                if (msg != null)
                {
                    // Update MMC with the current selection information
                    queueFormView.SelectionData.Update(msg, false, null, null);
                    
                    // Update the title of the selected data menu in the actions pane
                    queueFormView.SelectionData.DisplayName = "Message: " + msg.Id;
                    
                    // Hide and Show required buttons
                    showMessageActions();
                }
            }
        }

        private void messagesLV_MouseClick(object sender, MouseEventArgs e)
        {
            // Get the currently selected container
            AzureMessage msg = getSelectedMessage();
            if (msg != null)
            {
                // Update MMC with the current selection information
                queueFormView.SelectionData.Update(msg, false, null, null);
                
                // Update the title of the selected data menu in the actions pane
                queueFormView.SelectionData.DisplayName = "Message: " + msg.Id;
                
                // Hide and Show required buttons
                showMessageActions();
            }

            if (e.Button == MouseButtons.Right)
            {
                ShowRightClick(messagesLV.PointToScreen(e.Location));
            }
        }

        private void queuesLV_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.F5:
                    refreshContainers_Triggered(sender, null);
                    break;

                case (Keys.Return | Keys.Alt):
                    queueProperties_Triggered(sender, null);
                    break;

                case Keys.Delete:
                    deleteQueue_Triggered(sender, null);
                    break;
            }
        }

        private void messagesLV_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            switch (e.KeyData)
            {
                case Keys.F5:
                    refreshContainers_Triggered(sender, null);
                    break;

                case (Keys.Return | Keys.Alt):
                    messageProperties_Triggered(sender, null);
                    break;
            }
        }

        private void queuesLV_SizeChanged(object sender, EventArgs e)
        {
            // Initialise the variable required
            int totalColumnWidth = 0;
            int autoSizeColumnIndex = 0;

            foreach (ColumnHeader col in queuesLV.Columns)
            {
                // Set the link to the index of the column to resize
                if (col.Text.Equals(AUTOSIZEQUEUES)) 
                {
                    autoSizeColumnIndex = col.Index;
                }
                
                // Update the total width with the current column
                totalColumnWidth += col.Width;
            }

            // Find the difference in the current width of the listview(including padding) and the width of all the columns
            int widthDiff = queuesLV.Width - totalColumnWidth - LVCOLPADDING;
            
            // Resize the resizable column to make it fit
            int colWidth = queuesLV.Columns[autoSizeColumnIndex].Width + widthDiff;
            queuesLV.Columns[autoSizeColumnIndex].Width = MINCOLSIZE > colWidth ? MINCOLSIZE : colWidth;
        }

        private void messagesLV_SizeChanged(object sender, EventArgs e)
        {
            //Initialise the variable required
            int totalColumnWidth = 0;
            int autoSizeColumnIndex = 0;

            foreach (ColumnHeader col in messagesLV.Columns)
            {
                // Set the link to the index of the column to resize
                if (col.Text.Equals(AUTOSIZEMESSAGES)) 
                {
                    autoSizeColumnIndex = col.Index;
                }
                
                // Update the total width with the current column
                totalColumnWidth += col.Width;
            }

            // Find the difference in the current width of the listview(including padding) and the width of all the columns
            int widthDiff = messagesLV.Width - totalColumnWidth - LVCOLPADDING;
            
            // Resize the resizable column to make it fit
            int colWidth = messagesLV.Columns[autoSizeColumnIndex].Width + widthDiff;
            messagesLV.Columns[autoSizeColumnIndex].Width = MINCOLSIZE > colWidth ? MINCOLSIZE : colWidth;
        }

        private void LV_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            // Get the currently selected listview
            ListView lvClicked = (ListView)sender;
            
            // Get the sorter that is being used.
            ListViewColumnSorter lvSorter = (ListViewColumnSorter)lvClicked.ListViewItemSorter;
            
            // Determine if clicked column is already the column that is being sorted.
            if (e.Column == lvSorter.SortColumn)
            {
                // Reverse the current sort direction for this column.
                if (lvSorter.Order == SortOrder.Ascending)
                {
                    lvSorter.Order = SortOrder.Descending;
                }
                else
                {
                    lvSorter.Order = SortOrder.Ascending;
                }
            }
            else
            {
                // Set the column number that is to be sorted; default to ascending.
                lvSorter.SortColumn = e.Column;
                lvSorter.Order = SortOrder.Ascending;
                // Set the column type for the column that is sorted.
                lvSorter.SortColumnType = lvClicked.Equals(queuesLV) ? QUEUES_LV_TYPES[e.Column] : MESSAGES_LV_TYPES[e.Column];
            }

            // Perform the sort with these new sort options.
            lvClicked.Sort();

        }

        public Collection<AzureMessage> DisplayedMessages
        {
            get
            {
                return displayedMessages;
            }
            set
            {
                displayedMessages = value;
                this.messagesLV.Items.Clear();
                if (displayedMessages != null)
                {
                    foreach (AzureMessage msg in displayedMessages)
                    {
                        ListViewItem li = this.messagesLV.Items.Add(new ListViewItem(new string[] { msg.Id, msg.InsertionTime.ToString("g"), msg.PopReceipt }, 2));
                        li.Tag = msg;
                    }
                }
                this.messagesLV.Refresh();
            }
        }

        public Collection<AzureQueue> DisplayedQueues
        {
            get
            {
                return displayedQueues;
            }
            set
            {
                displayedQueues = value;
                this.queuesLV.Items.Clear();
                if (displayedQueues != null)
                {
                    foreach (AzureQueue queue in displayedQueues)
                    {
                        ListViewItem li = this.queuesLV.Items.Add(new ListViewItem(new string[] { queue.Name, queue.Path.ToString(), queue.Length.ToString(), /*queue.PollInterval.ToString()*/string.Empty, queue.Timeout.ToString() }, 3));
                        li.Tag = queue;
                    }
                }
                this.queuesLV.Refresh();
            }
        }
       
        private AzureQueue getSelectedQueue()
        {
            if (queuesLV.SelectedItems.Count > 0)
            {
                return queuesLV.SelectedItems[0].Tag as AzureQueue;
            }

            return null;
        }

        private AzureMessage getSelectedMessage()
        {
            if (messagesLV.SelectedItems.Count > 0)
            {
                return messagesLV.SelectedItems[0].Tag as AzureMessage;
            }

            return null;
        }

        private void showQueueActions()
        {
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_messageAdd)) queueFormView.SelectionData.ActionsPaneItems.Add(_messageAdd);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_purgeQueue)) queueFormView.SelectionData.ActionsPaneItems.Add(_purgeQueue);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_deleteQueue)) queueFormView.SelectionData.ActionsPaneItems.Add(_deleteQueue);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_sepViewQueue)) queueFormView.SelectionData.ActionsPaneItems.Add(_sepViewQueue);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_viewQueue)) queueFormView.SelectionData.ActionsPaneItems.Add(_viewQueue);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_sepQueue)) queueFormView.SelectionData.ActionsPaneItems.Add(_sepQueue);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_queueProperties)) queueFormView.SelectionData.ActionsPaneItems.Add(_queueProperties);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_viewMessage)) queueFormView.SelectionData.ActionsPaneItems.Remove(_viewMessage);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_sepMessage)) queueFormView.SelectionData.ActionsPaneItems.Remove(_sepMessage);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_messageProperties)) queueFormView.SelectionData.ActionsPaneItems.Remove(_messageProperties);
        }

        private void showMessageActions()
        {
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_messageAdd)) queueFormView.SelectionData.ActionsPaneItems.Remove(_messageAdd);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_purgeQueue)) queueFormView.SelectionData.ActionsPaneItems.Remove(_purgeQueue);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_sepViewQueue)) queueFormView.SelectionData.ActionsPaneItems.Remove(_sepViewQueue);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_deleteQueue)) queueFormView.SelectionData.ActionsPaneItems.Remove(_deleteQueue);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_viewQueue)) queueFormView.SelectionData.ActionsPaneItems.Remove(_viewQueue);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_sepQueue)) queueFormView.SelectionData.ActionsPaneItems.Remove(_sepQueue);
            if (queueFormView.SelectionData.ActionsPaneItems.Contains(_queueProperties)) queueFormView.SelectionData.ActionsPaneItems.Remove(_queueProperties);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_viewMessage)) queueFormView.SelectionData.ActionsPaneItems.Add(_viewMessage);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_sepMessage)) queueFormView.SelectionData.ActionsPaneItems.Add(_sepMessage);
            if (!queueFormView.SelectionData.ActionsPaneItems.Contains(_messageProperties)) queueFormView.SelectionData.ActionsPaneItems.Add(_messageProperties);
        }
    }
}