protected override void OnDropDownOpening(EventArgs e)
        {
            base.DropDownItems.Clear();
            _isDropDownClosed = false;

            // Work around bug in tool kit where disabled menu items show their dropdown menus
            if (!Enabled)
            {
                ToolStripMenuItem emptyMenuItem = new ToolStripMenuItem(Messages.HOST_MENU_EMPTY);
                emptyMenuItem.Font    = Program.DefaultFont;
                emptyMenuItem.Enabled = false;
                base.DropDownItems.Add(emptyMenuItem);
                return;
            }

            VisualMenuItemAlignData.ParentStrip = this;
            IXenConnection connection = Command.GetSelection()[0].Connection;
            bool           wlb        = Helpers.WlbEnabled(connection);

            if (wlb)
            {
                base.DropDownItems.Add(new VMOperationToolStripMenuSubItem(Messages.WLB_OPT_MENU_OPTIMAL_SERVER, Images.StaticImages._000_ServerWlb_h32bit_16));
            }
            else
            {
                base.DropDownItems.Add(new VMOperationToolStripMenuSubItem(Messages.HOME_SERVER_MENU_ITEM, Images.StaticImages._000_ServerHome_h32bit_16));
            }

            List <Host> hosts = new List <Host>(connection.Cache.Hosts);

            hosts.Sort();
            foreach (Host host in hosts)
            {
                VMOperationToolStripMenuSubItem item = new VMOperationToolStripMenuSubItem(String.Format(Messages.MAINWINDOW_CONTEXT_UPDATING, host.name_label.EscapeAmpersands()), Images.StaticImages._000_ServerDisconnected_h32bit_16);
                item.Tag = host;
                base.DropDownItems.Add(item);
            }

            // start a new thread to evaluate which hosts can be used.
            ThreadPool.QueueUserWorkItem(delegate
            {
                SelectedItemCollection selection = Command.GetSelection();
                Session session = selection[0].Connection.DuplicateSession();
                WlbRecommendations recommendations = new WlbRecommendations(selection.AsXenObjects <VM>(), session);
                recommendations.Initialize();

                if (recommendations.IsError)
                {
                    EnableAppropriateHostsNoWlb(session);
                }
                else
                {
                    EnableAppropriateHostsWlb(session, recommendations);
                }
            });
        }
        private void EnableAppropriateHostsWlb(WlbRecommendations recommendations)
        {
            if (Stopped || DropDownItems.Count == 0)
            {
                return;
            }

            // set the first menu item to be the WLB optimal server menu item
            var firstItem = DropDownItems[0] as VMOperationToolStripMenuSubItem;

            if (firstItem == null)
            {
                return;
            }

            var selection = Command.GetSelection();

            var firstItemCmd = new VMOperationWlbOptimalServerCommand(Command.MainWindowCommandInterface,
                                                                      selection, _operation, recommendations);

            firstItem.Command = firstItemCmd;
            firstItem.Enabled = firstItemCmd.CanExecute();

            var hostMenuItems = new List <VMOperationToolStripMenuSubItem>();

            foreach (var item in DropDownItems)
            {
                var hostMenuItem = item as VMOperationToolStripMenuSubItem;
                if (hostMenuItem == null)
                {
                    continue;
                }

                var host = hostMenuItem.Tag as Host;
                if (host != null)
                {
                    var cmd = new VMOperationWlbHostCommand(Command.MainWindowCommandInterface, selection, host,
                                                            _operation, recommendations.GetStarRating(host));

                    hostMenuItem.Command = cmd;
                    hostMenuItem.Enabled = cmd.CanExecute();

                    hostMenuItems.Add(hostMenuItem);
                }
            }

            // sort the hostMenuItems by star rating
            hostMenuItems.Sort(new WlbHostStarCompare());

            // refresh the drop-down-items from the menuItems.
            foreach (VMOperationToolStripMenuSubItem menuItem in hostMenuItems)
            {
                DropDownItems.Insert(hostMenuItems.IndexOf(menuItem) + 1, menuItem);
            }
        }
        private void EnableAppropriateHostsWlb(Session session, WlbRecommendations recommendations)
        {
            SelectedItemCollection selection = Command.GetSelection();
            // set the first menu item to be the WLB optimal server menu item
            VMOperationToolStripMenuSubItem firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];
            var firstItemCmd           = new VMOperationWlbOptimalServerCommand(Command.MainWindowCommandInterface, selection, _operation, recommendations);
            var firstItemCmdCanExecute = firstItemCmd.CanExecute();

            Program.Invoke(Program.MainWindow, delegate
            {
                firstItem.Command = firstItemCmd;
                firstItem.Enabled = firstItemCmdCanExecute;
            });

            List <VMOperationToolStripMenuSubItem> hostMenuItems = new List <VMOperationToolStripMenuSubItem>();

            foreach (VMOperationToolStripMenuSubItem item in base.DropDownItems)
            {
                Host host = item.Tag as Host;
                if (host != null)
                {
                    var cmd        = new VMOperationWlbHostCommand(Command.MainWindowCommandInterface, selection, host, _operation, recommendations.GetStarRating(host));
                    var canExecute = cmd.CanExecute();

                    Program.Invoke(Program.MainWindow, delegate
                    {
                        item.Command = cmd;
                        item.Enabled = canExecute;
                    });
                    hostMenuItems.Add(item);
                }
            }

            // Shuffle the list to make it look cool
            // Helpers.ShuffleList(hostMenuItems);

            // sort the hostMenuItems by star rating
            hostMenuItems.Sort(new WlbHostStarCompare());

            // refresh the drop-down-items from the menuItems.
            foreach (VMOperationToolStripMenuSubItem menuItem in hostMenuItems)
            {
                Program.Invoke(Program.MainWindow, delegate()
                {
                    base.DropDownItems.Insert(hostMenuItems.IndexOf(menuItem) + 1, menuItem);
                });
            }

            Program.Invoke(Program.MainWindow, () => AddAdditionalMenuItems(selection));
        }
Ejemplo n.º 4
0
        public VMOperationWlbHostCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> vms, Host host, vm_operations operation, WlbRecommendations.WlbRecommendation recommendation)
            : base(mainWindow, vms, operation)
        {
            Util.ThrowIfParameterNull(recommendation, "recommendation");
            Util.ThrowIfParameterNull(host, "host");

            _host = host;
            _menuText = _host.Name.EscapeAmpersands();

            //Default or failure case, there is no score/star rating actually, just don't display star
            _secondImage = null;
            _menuImage = Images.StaticImages._000_ServerDisconnected_h32bit_16;
            _recommendation = recommendation;

            if (CanExecute())
            {
                _starRating = _recommendation.StarRating;
                _menuImage = Images.StaticImages._000_TreeConnected_h32bit_16;
                _secondImage = GetWLBStarImage(_starRating);
            }
            else
            {
                // get the failure reason if the dictionary is populated with reasons that are all the same.
                // otherwise don't display a reason - leave this to the error dialog.

                string reason = null;
                foreach (string r in _recommendation.CantExecuteReasons.Values)
                {
                    if (reason != null && r != reason)
                    {
                        return;
                    }
                    reason = r;
                }

                if (!string.IsNullOrEmpty(reason))
                {
                    _menuText = string.Format(Messages.MAINWINDOW_CONTEXT_REASON, _menuText, reason);
                }
            }
        }
Ejemplo n.º 5
0
        private void EnableAppropriateHostsWlb(Session session, WlbRecommendations recommendations)
        {
            SelectedItemCollection selection = Command.GetSelection();

            // set the first menu item to be the WLB optimal server menu item
            Program.Invoke(Program.MainWindow, delegate
            {
                VMOperationToolStripMenuSubItem firstItem = (VMOperationToolStripMenuSubItem)base.DropDownItems[0];
                firstItem.Command = new VMOperationWlbOptimalServerCommand(Command.MainWindowCommandInterface, selection, _operation, recommendations);
            });

            List <VMOperationToolStripMenuSubItem> hostMenuItems = new List <VMOperationToolStripMenuSubItem>();

            Program.Invoke(Program.MainWindow, delegate
            {
                foreach (VMOperationToolStripMenuSubItem item in base.DropDownItems)
                {
                    if (item.Tag is Host)
                    {
                        Host host    = (Host)item.Tag;
                        item.Command = new VMOperationWlbHostCommand(Command.MainWindowCommandInterface, selection, host, _operation, recommendations.GetStarRating(host));
                        hostMenuItems.Add(item);
                    }
                }
            });

            // Shuffle the list to make it look cool
            Helpers.ShuffleList(hostMenuItems);

            // sort the hostMenuItems by star rating
            hostMenuItems.Sort(new WlbHostStarCompare());

            // refresh the drop-down-items from the menuItems.
            Program.Invoke(Program.MainWindow, delegate()
            {
                foreach (VMOperationToolStripMenuSubItem menuItem in hostMenuItems)
                {
                    base.DropDownItems.Insert(hostMenuItems.IndexOf(menuItem) + 1, menuItem);
                }
            });
        }
        private void UpdateHostList()
        {
            Stopped = false;

            var selection  = Command.GetSelection();
            var connection = selection[0].Connection;

            if (Helpers.WlbEnabled(connection))
            {
                var vms = selection.AsXenObjects <VM>();
                var retrieveVmRecommendationsAction = new WlbRetrieveVmRecommendationsAction(connection, vms);
                retrieveVmRecommendationsAction.Completed += delegate
                {
                    if (Stopped || retrieveVmRecommendationsAction.Cancelled ||
                        !retrieveVmRecommendationsAction.Succeeded)
                    {
                        return;
                    }

                    var recommendations = new WlbRecommendations(vms, retrieveVmRecommendationsAction.Recommendations);

                    Program.Invoke(Program.MainWindow, delegate
                    {
                        if (recommendations.IsError)
                        {
                            EnableAppropriateHostsNoWlb();
                        }
                        else
                        {
                            EnableAppropriateHostsWlb(recommendations);
                        }
                    });
                };
                retrieveVmRecommendationsAction.RunAsync();
            }
            else
            {
                EnableAppropriateHostsNoWlb();
            }
        }
Ejemplo n.º 7
0
 public VMOperationWlbOptimalServerCommand(IMainWindow mainWindow, IEnumerable <SelectedItem> selection, vm_operations operation, WlbRecommendations recommendations)
     : base(mainWindow, selection, operation)
 {
     Util.ThrowIfParameterNull(recommendations, "recommendations");
     _recommendations = recommendations;
 }
 public VMOperationWlbOptimalServerCommand(IMainWindow mainWindow, IEnumerable<SelectedItem> selection, vm_operations operation, WlbRecommendations recommendations)
     : base(mainWindow, selection, operation)
 {
     Util.ThrowIfParameterNull(recommendations, "recommendations");
     _recommendations = recommendations;
 }