コード例 #1
0
        /// <summary>
        /// Creates a menu compatible with object <paramref name="o"/>.  Returns null if no compatible menu exists.
        /// Errors are reported to <see cref="IBasicActivateItems.GlobalErrorCheckNotifier"/> (if set up).
        ///
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public ContextMenuStrip GetMenuIfExists(object o)
        {
            try
            {
                if (o is ICollection many)
                {
                    var menu = new RDMPContextMenuStrip(new RDMPContextMenuStripArgs(_activator, Tree, many), many);

                    var factory = new AtomicCommandUIFactory(_activator);

                    if (many.Cast <object>().All(d => d is IMapsDirectlyToDatabaseTable))
                    {
                        menu.Items.Add(factory.CreateMenuItem(new ExecuteCommandStartSession(_activator, many.Cast <IMapsDirectlyToDatabaseTable>().ToArray())));
                        menu.Items.Add(factory.CreateMenuItem(new ExecuteCommandAddToSession(_activator, many.Cast <IMapsDirectlyToDatabaseTable>().ToArray(), null)));
                    }

                    var cmdFactory = new AtomicCommandFactory(_activator);
                    foreach (var p in cmdFactory.CreateManyObjectCommands(many))
                    {
                        menu.Add(p);
                    }

                    MenuBuilt?.Invoke(this, new MenuBuiltEventArgs(menu, o));
                    return(menu);
                }

                if (o != null)
                {
                    //is o masquerading as someone else?
                    IMasqueradeAs masquerader = o as IMasqueradeAs;

                    //if so this is who he is pretending to be
                    object masqueradingAs = null;

                    if (masquerader != null)
                    {
                        masqueradingAs = masquerader.MasqueradingAs(); //yes he is masquerading!
                    }
                    var menu = GetMenuWithCompatibleConstructorIfExists(o);

                    //If no menu takes the object o try checking the object it is masquerading as as a secondary preference
                    if (menu == null && masqueradingAs != null)
                    {
                        menu = GetMenuWithCompatibleConstructorIfExists(masqueradingAs, masquerader);
                    }

                    //found a menu with compatible constructor arguments
                    if (menu != null)
                    {
                        if (!Settings.AllowPinning)
                        {
                            var miPin = menu.Items.OfType <AtomicCommandMenuItem>().SingleOrDefault(mi => mi.Tag is ExecuteCommandPin);

                            if (miPin != null)
                            {
                                miPin.Enabled     = false;
                                miPin.ToolTipText = "Pinning is disabled in this collection";
                            }
                        }

                        MenuBuilt?.Invoke(this, new MenuBuiltEventArgs(menu, o));
                        return(menu);
                    }

                    //no compatible menus so just return default menu
                    var defaultMenu = new RDMPContextMenuStrip(new RDMPContextMenuStripArgs(_activator, Tree, o), o);
                    defaultMenu.AddCommonMenuItems(this);

                    MenuBuilt?.Invoke(this, new MenuBuiltEventArgs(defaultMenu, o));
                    return(defaultMenu);
                }
                else
                {
                    //it's a right click in whitespace (nothing right clicked)

                    AtomicCommandUIFactory factory = new AtomicCommandUIFactory(_activator);

                    if (WhitespaceRightClickMenuCommandsGetter != null)
                    {
                        var menu = factory.CreateMenu(_activator, Tree, _collection, WhitespaceRightClickMenuCommandsGetter(_activator));
                        menu.AddCommonMenuItems(this);
                        return(menu);
                    }
                }

                return(null);
            }
            catch (Exception ex)
            {
                if (_activator?.GlobalErrorCheckNotifier == null)
                {
                    throw;
                }

                _activator.GlobalErrorCheckNotifier.OnCheckPerformed(new CheckEventArgs($"Failed to build menu for {o} of Type {o?.GetType()}", CheckResult.Fail, ex));
                return(null);
            }
        }