/// <summary> /// A helper function for creating a node description object for the category hierarchy. /// </summary> /// <param name="properties">List of properties </param> /// <returns>The description</returns> private TreeViewNode GetNodeDescription(CategoryTree categoryTree) { TreeViewNode root = new TreeViewNode(); root.Name = model.Name; // find namespace and image name needed to find image file in the Resources of UserInterface project string nameSpace = model.GetType().FullName.Split('.')[1]; string imageName = model.GetType().Name; root.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages." + nameSpace + "." + imageName + ".png"; foreach (CategoryItem cat in categoryTree.CategoryItems) { TreeViewNode description = new TreeViewNode(); description.Name = cat.Name; description.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.Folder.png"; description.Children = new List <TreeViewNode>(); foreach (string subcat in cat.SubcategoryNames) { TreeViewNode child = new TreeViewNode(); child.Name = subcat; child.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.Folder.png"; description.Children.Add(child); } root.Children.Add(description); } return(root); }
/// <summary> /// Returns the Category Tree created from the Category Attributes on the properties in the model. /// </summary> /// <returns></returns> private CategoryTree GetPropertyCategories() { CategoryTree categories = new CategoryTree(); Model firstChild = null; this.childrenWithSameType = this.GetChildModelsWithSameType(this.model); if (childrenWithSameType != null) { firstChild = this.childrenWithSameType.First() as Model; } if (firstChild != null) { foreach (PropertyInfo property in firstChild.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)) { // Properties must have a [Description], not be called Name, and be read/write. bool hasDescription = property.IsDefined(typeof(DescriptionAttribute), false); bool includeProperty = hasDescription && property.Name != "Name" && property.CanRead && property.CanWrite; // Only allow lists that are double[], int[] or string[] if (includeProperty && property.PropertyType.GetInterface("IList") != null) { includeProperty = property.PropertyType == typeof(double[]) || property.PropertyType == typeof(int[]) || property.PropertyType == typeof(string[]); } if (includeProperty) { // Those properties with a [Catagory] attribute bool hasCategory = property.IsDefined(typeof(CategoryAttribute), false); if (hasCategory) { //get the attribute data CategoryAttribute catAtt = (CategoryAttribute)property.GetCustomAttribute(typeof(CategoryAttribute)); //add the category name to the list of category items categories.AddCategoryToTree(catAtt.Category); //add the subcategory name to the list of subcategories for the category CategoryItem catItem = categories.FindCategoryInTree(catAtt.Category); catItem.AddSubcategoryName(catAtt.Subcategory); } else { //If there is not [Category] attribute at all on the property in the model. //Add it to the "Unspecified" Category, and "Unspecified" Subcategory categories.AddCategoryToTree("Unspecified"); CategoryItem catItem = categories.FindCategoryInTree("Unspecified"); catItem.AddSubcategoryName("Unspecified"); } } } } return(categories); }
/// <summary> /// A helper function for creating a node description object for the category hierarchy. /// </summary> /// <param name="categoryTree"></param> /// <returns>The description</returns> private TreeViewNode GetNodeDescription(CategoryTree categoryTree) { TreeViewNode root = new TreeViewNode(); root.Name = model.Name; // find namespace and image name needed to find image file in the Resources of UserInterface project string nameSpace = model.GetType().FullName.Split('.')[1]; string imageName = model.GetType().Name; root.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages." + nameSpace + "." + imageName + ".png"; foreach (CategoryItem cat in categoryTree.CategoryItems) { TreeViewNode description = new TreeViewNode(); description.Name = cat.Name; description.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.CLEM.ActivityFolder.png"; description.Children = new List <TreeViewNode>(); foreach (string subcat in cat.SubcategoryNames) { TreeViewNode child = new TreeViewNode(); child.Name = subcat; if (subcat.ToLower().Contains("pasture")) { child.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.CLEM.PastureTreeItem.png"; } else if (subcat.ToLower().Contains("unspecif")) { child.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.CLEM.UnspecifiedTreeItem.png"; } else if (model.GetType().Name.ToLower().Contains("ruminant")) { child.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.CLEM.RuminantGroup.png"; } else { child.ResourceNameForImage = "ApsimNG.Resources.TreeViewImages.CLEM.UnspecifiedTreeItem.png"; } description.Children.Add(child); } root.Children.Add(description); } return(root); }
/// <summary> /// Refresh the treeview. /// </summary> public void RefreshTreeView() { CategoryTree categoryTree = this.GetPropertyCategories(); this.treeview.Refresh(this.GetNodeDescription(categoryTree)); }
/// <summary> /// Returns the Category Tree created from the Category Attributes on the properties in the model. /// </summary> /// <returns></returns> private CategoryTree GetPropertyCategories() { CategoryTree categories = new CategoryTree(); IModel modelToUse = ModelForProperties(); if (modelToUse != null) { foreach (PropertyInfo property in modelToUse.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)) { // Properties must have a [Description], not be called Name, and be read/write. bool hasDescription = property.IsDefined(typeof(DescriptionAttribute), false); bool includeProperty = hasDescription && property.Name != "Name" && property.CanRead && property.CanWrite; // Only allow lists that are double[], int[] or string[] if (includeProperty && property.PropertyType.GetInterface("IList") != null) { includeProperty = property.PropertyType == typeof(double[]) || property.PropertyType == typeof(int[]) || property.PropertyType == typeof(string[]); } if (includeProperty) { // Those properties with a [Catagory] attribute bool hasCategory = property.IsDefined(typeof(CategoryAttribute), false); if (hasCategory) { //get the attribute data CategoryAttribute catAtt = (CategoryAttribute)property.GetCustomAttribute(typeof(CategoryAttribute)); // add the category name to the list of category items // allow : separated list for multiple categories int catIndex = 0; foreach (var catLabel in catAtt.Category.Split(':')) { if (catLabel != "*") { categories.AddCategoryToTree(catLabel); //add the subcategory name to the list of subcategories for the category CategoryItem catItem = categories.FindCategoryInTree(catLabel); var subLabels = catAtt.Subcategory.Split(':'); if (subLabels.Length >= catIndex + 1) { if (subLabels[catIndex] != "*") { catItem.AddSubcategoryName(subLabels[catIndex]); } } } catIndex++; } //categories.AddCategoryToTree(catAtt.Category); ////add the subcategory name to the list of subcategories for the category //CategoryItem catItem = categories.FindCategoryInTree(catAtt.Category); //catItem.AddSubcategoryName(catAtt.Subcategory); } else { //If there is not [Category] attribute at all on the property in the model. //Add it to the "Unspecified" Category, and "Unspecified" Subcategory categories.AddCategoryToTree("Unspecified"); CategoryItem catItem = categories.FindCategoryInTree("Unspecified"); catItem.AddSubcategoryName("Unspecified"); } } } } return(categories); }