示例#1
0
        private static EnumCommandGroupSpec CreateEnumCommandGroup <TCmdEnum>(IXCommandManager cmdMgr, CommandGroupSpec parent, int id)
            where TCmdEnum : Enum
        {
            var cmdGroupType = typeof(TCmdEnum);

            if (id == -1)
            {
                id = GetEnumCommandGroupId(cmdMgr, cmdGroupType);
            }

            if (parent != null)
            {
                if (parent.Id == id)
                {
                    throw new ParentGroupCircularDependencyException($"{parent.Title} ({parent.Id})");
                }
            }

            var bar = new EnumCommandGroupSpec(cmdGroupType, id);

            bar.Parent = parent;

            bar.InitFromEnum <TCmdEnum>();

            bar.Commands = Enum.GetValues(cmdGroupType).Cast <TCmdEnum>().Select(
                c => CreateCommand(c)).ToArray();

            return(bar);
        }
示例#2
0
        private static int GetEnumCommandGroupId(IXCommandManager cmdMgr, Type cmdGroupType)
        {
            var nextGroupId = 0;

            if (cmdMgr.CommandGroups.Any())
            {
                nextGroupId = cmdMgr.CommandGroups.Max(g => g.Spec.Id) + 1;
            }

            CommandGroupInfoAttribute grpInfoAtt = null;

            var id = 0;

            if (cmdGroupType.TryGetAttribute <CommandGroupInfoAttribute>(x => grpInfoAtt = x))
            {
                if (grpInfoAtt.UserId != -1)
                {
                    id = grpInfoAtt.UserId;
                }
                else
                {
                    id = nextGroupId;
                }
            }
            else
            {
                id = nextGroupId;
            }

            return(id);
        }
示例#3
0
        private static int GetEnumCommandGroupId(IXCommandManager cmdMgr, Type cmdGroupType, out string tabName)
        {
            if (!TryGetUserAssignedGroupId(cmdGroupType, out tabName, out int id))
            {
                var nextGroupId = 1;

                if (cmdMgr.CommandGroups.Any())
                {
                    var usedIds = cmdMgr.CommandGroups.Select(g => g.Spec.Id).OrderBy(x => x).ToArray();

                    for (int i = 0; i < cmdMgr.CommandGroups.Length; i++)
                    {
                        if (usedIds[i] != nextGroupId)
                        {
                            break;
                        }

                        nextGroupId++;
                    }
                }

                id = nextGroupId;
            }

            return(id);
        }
示例#4
0
        /// <summary>
        /// Adds context menu based on the enumeration
        /// </summary>
        /// <param name="owner">Context menu owner</param>
        ///<inheritdoc cref="AddCommandGroup{TCmdEnum}(IXCommandManager)"/>
        public static IEnumCommandGroup <TCmdEnum> AddContextMenu <TCmdEnum>(this IXCommandManager cmdMgr, SelectType_e?owner = null)
            where TCmdEnum : Enum
        {
            var enumGrp = CreateEnumCommandGroup <TCmdEnum>(cmdMgr, GetEnumCommandGroupParent(cmdMgr, typeof(TCmdEnum)), -1);

            var cmdGrp = cmdMgr.AddContextMenu(enumGrp, owner);

            return(new EnumCommandGroup <TCmdEnum>(cmdGrp));
        }
示例#5
0
        /// <summary>
        /// Adds command group based on the enumeration where each enumeration field represents the command button
        /// </summary>
        /// <typeparam name="TCmdEnum">Enumeration with commands</typeparam>
        /// <param name="cmdMgr">Command manager</param>
        /// <returns>Created command group</returns>
        /// <remarks>Decorate enumeration and fields with <see cref="TitleAttribute"/>, <see cref="IconAttribute"/>, <see cref="DescriptionAttribute"/>, <see cref="CommandItemInfoAttribute"/> to customized look and feel of commands</remarks>
        public static IEnumCommandGroup <TCmdEnum> AddCommandGroup <TCmdEnum>(this IXCommandManager cmdMgr)
            where TCmdEnum : Enum
        {
            var enumGrp = CreateEnumCommandGroup <TCmdEnum>(cmdMgr, GetEnumCommandGroupParent(cmdMgr, typeof(TCmdEnum)), -1);

            var cmdGrp = cmdMgr.AddCommandGroup(enumGrp);

            return(new EnumCommandGroup <TCmdEnum>(cmdGrp));
        }
示例#6
0
        /// <summary>
        /// Adds command group based on the enumeration where each enumeration field represents the command button
        /// </summary>
        /// <typeparam name="TCmdEnum">Enumeration with commands</typeparam>
        /// <param name="cmdMgr">Command manager</param>
        /// <returns>Created command group</returns>
        /// <remarks>Decorate enumeration and fields with <see cref="TitleAttribute"/>, <see cref="IconAttribute"/>, <see cref="DescriptionAttribute"/>, <see cref="CommandItemInfoAttribute"/> to customized look and feel of commands</remarks>
        public static IEnumCommandGroup <TCmdEnum> AddCommandGroup <TCmdEnum>(this IXCommandManager cmdMgr)
            where TCmdEnum : Enum
        {
            var id = GetEnumCommandGroupId(cmdMgr, typeof(TCmdEnum), out string tabName);

            var enumGrp = CreateEnumCommandGroup <TCmdEnum>(cmdMgr, GetEnumCommandGroupParent(cmdMgr, typeof(TCmdEnum)), tabName, id);

            var cmdGrp = cmdMgr.AddCommandGroup(enumGrp);

            return(new EnumCommandGroup <TCmdEnum>(cmdGrp));
        }
示例#7
0
        private static CommandGroupSpec GetEnumCommandGroupParent(IXCommandManager cmdMgr, Type cmdGroupType)
        {
            CommandGroupSpec parent = null;

            CommandGroupParentAttribute grpParentAtt = null;

            if (cmdGroupType.TryGetAttribute <CommandGroupParentAttribute>(x => grpParentAtt = x))
            {
                var groups = cmdMgr.CommandGroups.Select(c => c.Spec);

                if (grpParentAtt.ParentGroupType != null)
                {
                    var parentGrpSpec = groups.OfType <EnumCommandGroupSpec>()
                                        .FirstOrDefault(g => g.CmdGrpEnumType == grpParentAtt.ParentGroupType);

                    if (parentGrpSpec == null)
                    {
                        throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupType.FullName, cmdGroupType.FullName);
                    }

                    if (grpParentAtt.ParentGroupType == cmdGroupType)
                    {
                        throw new ParentGroupCircularDependencyException(grpParentAtt.ParentGroupType.FullName);
                    }

                    parent = parentGrpSpec;
                }
                else
                {
                    var parentGrpSpec = groups.OfType <CommandGroupSpec>()
                                        .FirstOrDefault(g => g.Id == grpParentAtt.ParentGroupUserId);

                    if (parentGrpSpec == null)
                    {
                        throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupUserId.ToString(), cmdGroupType.FullName);
                    }

                    parent = parentGrpSpec;
                }
            }

            return(parent);
        }
示例#8
0
        public static EnumCommandGroupSpec CreateSpecFromEnum <TCmdEnum>(this IXCommandManager cmdMgr, CommandGroupSpec parent, int?id)
            where TCmdEnum : Enum
        {
            var isUserIdAssigned = TryGetUserAssignedGroupId(typeof(TCmdEnum), out string tabName, out int userId);

            if (!id.HasValue)
            {
                if (isUserIdAssigned)
                {
                    id = userId;
                }
                else
                {
                    throw new GroupUserIdNotAssignedException();
                }
            }

            return(CreateEnumCommandGroup <TCmdEnum>(cmdMgr, parent, tabName, id.Value));
        }
示例#9
0
        public static IEnumCommandBar <TCmdEnum> AddCommandGroup <TCmdEnum>(this IXCommandManager cmdMgr)
            where TCmdEnum : Enum
        {
            int GetNextAvailableGroupId()
            {
                if (cmdMgr.CommandGroups.Any())
                {
                    return(cmdMgr.CommandGroups.Max(g => g.Spec.Id) + 1);
                }
                else
                {
                    return(0);
                }
            }

            var barSpec = CreateCommandBar <TCmdEnum>(GetNextAvailableGroupId(), cmdMgr.CommandGroups.Select(c => c.Spec));

            var bar = cmdMgr.AddCommandBar(barSpec);

            return(new EnumCommandGroup <TCmdEnum>(bar));
        }
示例#10
0
        /// <param name="id">Id or -1 to automatically assign</param>
        private static EnumCommandGroupSpec CreateEnumCommandGroup <TCmdEnum>(IXCommandManager cmdMgr, CommandGroupSpec parent, string tabName, int id)
            where TCmdEnum : Enum
        {
            var cmdGroupType = typeof(TCmdEnum);

            if (parent != null)
            {
                if (parent.Id == id)
                {
                    throw new ParentGroupCircularDependencyException($"{parent.Title} ({parent.Id})");
                }
            }

            var bar = new EnumCommandGroupSpec(cmdGroupType, id);

            bar.RibbonTabName = tabName;
            bar.Parent        = parent;

            bar.InitFromEnum <TCmdEnum>();

            bar.Commands = Enum.GetValues(cmdGroupType).Cast <TCmdEnum>().Select(
                c =>
            {
                var enumCmdUserId = Convert.ToInt32(c);

                if (enumCmdUserId < 0)
                {
                    enumCmdUserId = 0;    //default id (not used)
                }
                else
                {
                    //NOTE: adding one to the id as 0 id means not used while enums start with 0
                    enumCmdUserId++;
                }

                return(CreateEnumCommand(c, enumCmdUserId));
            }).ToArray();

            return(bar);
        }
示例#11
0
 public static CommandGroupSpec CreateSpecFromEnum <TCmdEnum>(this IXCommandManager cmdMgr, int id = -1, CommandGroupSpec parent = null)
     where TCmdEnum : Enum => CreateEnumCommandGroup <TCmdEnum>(cmdMgr, parent, id);
示例#12
0
        private static EnumCommandGroupSpec CreateEnumCommandGroup <TCmdEnum>(IXCommandManager cmdMgr)
            where TCmdEnum : Enum
        {
            var nextGroupId = 0;

            if (cmdMgr.CommandGroups.Any())
            {
                nextGroupId = cmdMgr.CommandGroups.Max(g => g.Spec.Id) + 1;
            }

            var groups = cmdMgr.CommandGroups.Select(c => c.Spec);

            var cmdGroupType = typeof(TCmdEnum);

            CommandGroupInfoAttribute grpInfoAtt = null;

            EnumCommandGroupSpec parent = null;
            int id = 0;

            if (cmdGroupType.TryGetAttribute <CommandGroupInfoAttribute>(x => grpInfoAtt = x))
            {
                if (grpInfoAtt.UserId != -1)
                {
                    id = grpInfoAtt.UserId;
                }
                else
                {
                    id = nextGroupId;
                }
            }
            else
            {
                id = nextGroupId;
            }

            CommandGroupParentAttribute grpParentAtt = null;

            if (cmdGroupType.TryGetAttribute <CommandGroupParentAttribute>(x => grpParentAtt = x))
            {
                if (grpParentAtt.ParentGroupType != null)
                {
                    var parentGrpSpec = groups.OfType <EnumCommandGroupSpec>()
                                        .FirstOrDefault(g => g.CmdGrpEnumType == grpParentAtt.ParentGroupType);

                    if (parentGrpSpec == null)
                    {
                        throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupType.FullName, cmdGroupType.FullName);
                    }

                    if (grpParentAtt.ParentGroupType == cmdGroupType)
                    {
                        throw new ParentGroupCircularDependencyException(grpParentAtt.ParentGroupType.FullName);
                    }

                    parent = parentGrpSpec;
                }
                else
                {
                    if (grpParentAtt.ParentGroupUserId == id)
                    {
                        throw new ParentGroupCircularDependencyException(grpParentAtt.ParentGroupType.FullName);
                    }

                    var parentGrpSpec = groups.OfType <EnumCommandGroupSpec>()
                                        .FirstOrDefault(g => g.Id == grpParentAtt.ParentGroupUserId);

                    if (parentGrpSpec == null)
                    {
                        throw new ParentGroupNotFoundException(grpParentAtt.ParentGroupUserId.ToString(), cmdGroupType.FullName);
                    }

                    parent = parentGrpSpec;
                }
            }

            var bar = new EnumCommandGroupSpec(cmdGroupType, id);

            bar.Parent = parent;

            bar.InitFromEnum <TCmdEnum>();

            bar.Commands = Enum.GetValues(cmdGroupType).Cast <TCmdEnum>().Select(
                c => CreateCommand(c)).ToArray();

            return(bar);
        }