Ejemplo n.º 1
0
        public static IEnumerable <DesignerActionItem> GetAllAttributedActionItems(this DesignerActionList actionList)
        {
            var fullAIList = new List <DesignerActionItem>();

            foreach (var mbr in actionList.GetType().GetMethods(allInstBind))
            {
                foreach (IActionGetItem attr in mbr.GetCustomAttributes(typeof(DesignerActionMethodAttribute), true))
                {
                    if (mbr.ReturnType == typeof(void) && mbr.GetParameters().Length == 0)
                    {
                        fullAIList.Add(attr.GetItem(actionList, mbr));
                    }
                    else
                    {
                        throw new FormatException("DesignerActionMethodAttribute must be applied to a method returning void and having no parameters.");
                    }
                }
            }
            foreach (var mbr in actionList.GetType().GetProperties(allInstBind))
            {
                if (mbr.GetIndexParameters().Length > 0)
                {
                    throw new FormatException("DesignerActionPropertyAttribute must be applied to non-indexed properties.");
                }
                foreach (IActionGetItem attr in mbr.GetCustomAttributes(typeof(DesignerActionPropertyAttribute), true))
                {
                    fullAIList.Add(attr.GetItem(actionList, mbr));
                }
            }
            fullAIList.Sort(CompareItems);
            return(fullAIList);

            int CompareItems(DesignerActionItem a, DesignerActionItem b)
            {
                var c = string.Compare(a?.Category ?? string.Empty, b?.Category ?? string.Empty, true);

                if (c != 0)
                {
                    return(c);
                }
                c = (int)(a?.Properties["Order"] ?? 0) - (int)(b?.Properties["Order"] ?? 0);
                if (c != 0)
                {
                    return(c);
                }
                return(string.Compare(a?.DisplayName, b?.DisplayName, true));
            }
        }
Ejemplo n.º 2
0
        public static DesignerActionItemCollection GetFilteredActionItems(this DesignerActionList actionList, IEnumerable <DesignerActionItem> fullAIList)
        {
            var col = new DesignerActionItemCollection();

            foreach (var ai in fullAIList)
            {
                if (CheckCondition(ai))
                {
                    col.Add(ai);
                }
            }

            // Add header items for displayed items
            string cat = null;

            for (var i = 0; i < col.Count; i++)
            {
                var curCat = col[i].Category;
                if (string.Compare(curCat, cat, true) != 0)
                {
                    col.Insert(i++, new DesignerActionHeaderItem(curCat));
                    cat = curCat;
                }
            }

            return(col);

            bool CheckCondition(DesignerActionItem ai)
            {
                if (ai.Properties["Condition"] != null)
                {
                    var p = actionList.GetType().GetProperty((string)ai.Properties["Condition"], allInstBind, null, typeof(bool), Type.EmptyTypes, null);
                    if (p != null)
                    {
                        return((bool)p.GetValue(actionList, null));
                    }
                }
                return(true);
            }
        }