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); }
/// <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); }
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); }
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); }