Exemplo n.º 1
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);
        }
Exemplo n.º 2
0
        private static bool TryGetUserAssignedGroupId(Type cmdGroupType, out string tabName, out int userId)
        {
            CommandGroupInfoAttribute grpInfoAtt = null;

            if (cmdGroupType.TryGetAttribute <CommandGroupInfoAttribute>(x => grpInfoAtt = x))
            {
                if (grpInfoAtt.UserId != -1)
                {
                    userId  = grpInfoAtt.UserId;
                    tabName = grpInfoAtt.TabName;
                    return(true);
                }
            }

            userId  = -1;
            tabName = "";
            return(false);
        }
Exemplo n.º 3
0
        private static EnumCommandGroupSpec CreateCommandBar <TCmdEnum>(int nextGroupId, IEnumerable <CommandGroupSpec> groups)
            where TCmdEnum : Enum
        {
            var cmdGroupType = typeof(TCmdEnum);

            var bar = new EnumCommandGroupSpec(cmdGroupType);

            CommandGroupInfoAttribute grpInfoAtt = null;

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

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

                    if (parentGrpSpec == null)
                    {
                        //TODO: create a specific exception
                        throw new NullReferenceException("Parent group is not created");
                    }

                    if (grpInfoAtt.ParentGroupType == cmdGroupType)
                    {
                        throw new InvalidOperationException("Group cannot be a parent of itself");
                    }

                    bar.Parent = parentGrpSpec;
                }
            }
            else
            {
                bar.Id = nextGroupId;
            }

            if (!cmdGroupType.TryGetAttribute <IconAttribute>(a => bar.Icon = a.Icon))
            {
                bar.Icon = Defaults.Icon;
            }

            if (!cmdGroupType.TryGetAttribute <DisplayNameAttribute>(a => bar.Title = a.DisplayName))
            {
                bar.Title = cmdGroupType.ToString();
            }

            if (!cmdGroupType.TryGetAttribute <DescriptionAttribute>(a => bar.Tooltip = a.Description))
            {
                bar.Tooltip = cmdGroupType.ToString();
            }

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

            return(bar);
        }
Exemplo n.º 4
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);
        }