Пример #1
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);
        }
Пример #2
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);
        }