Beispiel #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
            {
                var many = o as ICollection;

                if (many != null)
                {
                    var menu = new ContextMenuStrip();

                    var factory = new AtomicCommandUIFactory(_activator);

                    if (many.Cast <object>().All(d => d is IDisableable))
                    {
                        var mi = factory.CreateMenuItem(new ExecuteCommandDisableOrEnable(_activator, many.Cast <IDisableable>().ToArray()));
                        menu.Items.Add(mi);
                    }

                    if (many.Cast <object>().All(d => d is IDeleteable))
                    {
                        var mi = factory.CreateMenuItem(new ExecuteCommandDelete(_activator, many.Cast <IDeleteable>().ToArray()));
                        mi.ShortcutKeys = Keys.Delete;
                        menu.Items.Add(mi);
                    }
                    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);
            }
        }
Beispiel #2
0
        private ContextMenuStrip GetMenuIfExists(object o)
        {
            var many = o as ICollection;

            if (many != null)
            {
                var menu = new ContextMenuStrip();

                var factory = new AtomicCommandUIFactory(_activator);

                if (many.Cast <object>().All(d => d is IDeleteable))
                {
                    var mi = factory.CreateMenuItem(new ExecuteCommandDelete(_activator, many.Cast <IDeleteable>().ToList()));
                    mi.ShortcutKeys = Keys.Delete;
                    menu.Items.Add(mi);
                }

                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";
                        }
                    }

                    return(menu);
                }

                //no compatible menus so just return default menu
                var defaultMenu = new RDMPContextMenuStrip(new RDMPContextMenuStripArgs(_activator, Tree, o), o);
                defaultMenu.AddCommonMenuItems(this);
                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);
        }