/// <summary> /// Updates the lists of Cultivar and Field names in the model. /// This is used when the model has been changed. For example, when a /// new crop has been selecled. /// </summary> /// <param name="model">The new model</param> public void UpdateModel(IModel model) { this.model = model; if (this.model != null) { IGridCell curCell = grid?.GetCurrentCell; for (int i = 0; i < properties.Count; i++) { IGridCell cell = grid.GetCell(1, i); if (curCell != null && cell.RowIndex == curCell.RowIndex && cell.ColumnIndex == curCell.ColumnIndex) { continue; } if (properties[i].Display != null && properties[i].Display.Type == DisplayType.CultivarName) { IPlant crop; if (properties[i].Display.PlantName != null) { crop = model.FindInScope <IPlant>(properties[i].Display.PlantName); } else { crop = GetCrop(properties); } if (crop != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetCultivarNames(crop); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifeCycleName) { Zone zone = model.FindInScope <Zone>(); if (zone != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetLifeCycleNames(zone); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifePhaseName) { LifeCycle lifeCycle; if (properties[i].Display.LifeCycleName != null) { lifeCycle = model.FindAllInScope <LifeCycle>().FirstOrDefault(p => p.Name == properties[i].Display.LifeCycleName) as LifeCycle; } else { lifeCycle = GetLifeCycle(properties); } if (lifeCycle != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetPhaseNames(lifeCycle); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FieldName) { List <string> fieldNames = GetFieldNames(); if (fieldNames != null) { fieldNames.Insert(0, string.Empty); cell.DropDownStrings = fieldNames.ToArray(); } } } } }
/// <summary> /// Format the grid. /// </summary> protected virtual void FormatGrid() { for (int i = 0; i < properties.Count; i++) { IGridCell cell = grid.GetCell(1, i); if (properties[i] is VariableObject) { cell.EditorType = EditorTypeEnum.TextBox; grid.SetRowAsSeparator(i, true); } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.TableName) { cell.EditorType = EditorTypeEnum.DropDown; if (storage == null) { storage = model.FindInScope <IDataStore>(); } cell.DropDownStrings = storage.Reader.TableNames.ToArray(); } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.CultivarName) { cell.EditorType = EditorTypeEnum.DropDown; IPlant crop; if (properties[i].Display.PlantName != null) { crop = model.FindInScope <IPlant>(properties[i].Display.PlantName); } else { crop = GetCrop(properties); } if (crop != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetCultivarNames(crop); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifeCycleName) { cell.EditorType = EditorTypeEnum.DropDown; Zone zone = model.FindInScope <Zone>(); if (zone != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetLifeCycleNames(zone); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifePhaseName) { cell.EditorType = EditorTypeEnum.DropDown; LifeCycle lifeCycle = GetLifeCycle(properties); if (lifeCycle != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetPhaseNames(lifeCycle); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FileName) { cell.EditorType = EditorTypeEnum.Button; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FileNames) { cell.EditorType = EditorTypeEnum.MultiFiles; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.DirectoryName) { cell.EditorType = EditorTypeEnum.DirectoryChooser; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FieldName) { cell.EditorType = EditorTypeEnum.DropDown; List <string> fieldNames = GetFieldNames(); if (fieldNames != null) { fieldNames.Insert(0, string.Empty); cell.DropDownStrings = fieldNames.ToArray(); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.ResidueName && model is SurfaceOrganicMatter) { cell.EditorType = EditorTypeEnum.DropDown; string[] fieldNames = GetResidueNames(); if (fieldNames != null) { cell.DropDownStrings = fieldNames; } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.Model) { cell.EditorType = EditorTypeEnum.DropDown; string[] modelNames = GetModelNames(properties[i].Display.ModelType); if (modelNames != null) { cell.DropDownStrings = modelNames; } } else { object cellValue = properties[i].ValueWithArrayHandling; if (cellValue is DateTime) { cell.EditorType = EditorTypeEnum.DateTime; } else if (cellValue is bool) { cell.EditorType = EditorTypeEnum.Boolean; } else if (cellValue.GetType().IsEnum) { cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = VariableProperty.EnumToStrings(cellValue); Enum cellValueAsEnum = cellValue as Enum; if (cellValueAsEnum != null) { cell.Value = VariableProperty.GetEnumDescription(cellValueAsEnum); } } else if (cellValue.GetType() == typeof(IPlant)) { cell.EditorType = EditorTypeEnum.DropDown; List <string> cropNames = new List <string>(); foreach (Model crop in model.FindAllInScope <IPlant>()) { cropNames.Add(crop.Name); } cell.DropDownStrings = cropNames.ToArray(); } else if (properties[i].DataType == typeof(IPlant)) { cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = model.FindAllInScope <IPlant>().OfType <IModel>().Select(m => m.Name).ToArray(); } else if (!string.IsNullOrWhiteSpace(properties[i].Display?.Values)) { explorerPresenter.ApsimXFile.Links.Resolve(model, allLinks: true, throwOnFail: false); BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; MethodInfo method = model.GetType().GetMethod(properties[i].Display.Values, flags); string[] values = ((IEnumerable <object>)method.Invoke(model, properties[i].Display.ValuesArgs))?.Select(v => v?.ToString())?.ToArray(); cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = values; } else { cell.EditorType = EditorTypeEnum.TextBox; } } cell.IsRowReadonly = !IsPropertyEnabled(i); } IGridColumn descriptionColumn = grid.GetColumn(0); descriptionColumn.Width = -1; descriptionColumn.ReadOnly = true; IGridColumn valueColumn = grid.GetColumn(1); valueColumn.Width = -1; }
/// <summary> /// Format the grid. /// </summary> protected virtual void FormatGrid() { for (int i = 0; i < properties.Count; i++) { IGridCell cell = grid.GetCell(1, i); if (properties[i] is VariableObject) { cell.EditorType = EditorTypeEnum.TextBox; grid.SetRowAsSeparator(i, true); } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.TableName) { cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = storage.Reader.TableNames.ToArray(); } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.CultivarName) { cell.EditorType = EditorTypeEnum.DropDown; IPlant crop; if (properties[i].Display.PlantName != null) { crop = model.FindInScope <IPlant>(properties[i].Display.PlantName); } else { crop = GetCrop(properties); } if (crop != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetCultivarNames(crop); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifeCycleName) { cell.EditorType = EditorTypeEnum.DropDown; Zone zone = model.FindInScope <Zone>(); if (zone != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetLifeCycleNames(zone); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.LifePhaseName) { cell.EditorType = EditorTypeEnum.DropDown; LifeCycle lifeCycle = GetLifeCycle(properties); if (lifeCycle != null) { cell.DropDownStrings = PropertyPresenterHelpers.GetPhaseNames(lifeCycle); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FileName) { cell.EditorType = EditorTypeEnum.Button; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FileNames) { cell.EditorType = EditorTypeEnum.MultiFiles; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.DirectoryName) { cell.EditorType = EditorTypeEnum.DirectoryChooser; } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.FieldName) { cell.EditorType = EditorTypeEnum.DropDown; List <string> fieldNames = GetFieldNames(); if (fieldNames != null) { fieldNames.Insert(0, string.Empty); cell.DropDownStrings = fieldNames.ToArray(); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.ResidueName && model is SurfaceOrganicMatter) { cell.EditorType = EditorTypeEnum.DropDown; string[] fieldNames = GetResidueNames(); if (fieldNames != null) { cell.DropDownStrings = fieldNames; } } else if (properties[i].Display != null && (properties[i].Display.Type == DisplayType.CLEMResource)) { cell.EditorType = EditorTypeEnum.DropDown; List <string> fieldNames = new List <string>(); fieldNames.AddRange(this.GetCLEMResourceNames(this.properties[i].Display.CLEMResourceGroups)); // add any extras elements provided to the list. if (this.properties[i].Display.CLEMExtraEntries != null) { fieldNames.AddRange(this.properties[i].Display.CLEMExtraEntries); } if (fieldNames.Count != 0) { cell.DropDownStrings = fieldNames.ToArray(); } } else if (properties[i].Display != null && (properties[i].Display.Type == DisplayType.CLEMCropFileReader)) { cell.EditorType = EditorTypeEnum.DropDown; List <string> fieldNames = new List <string>(); Simulation clemParent = model.FindAncestor <Simulation>(); // get crop file names fieldNames.AddRange(clemParent.FindAllDescendants <FileCrop>().Select(a => a.Name).ToList()); fieldNames.AddRange(clemParent.FindAllDescendants <FileSQLiteCrop>().Select(a => a.Name).ToList()); if (fieldNames.Count != 0) { cell.DropDownStrings = fieldNames.ToArray(); } } else if (properties[i].Display != null && (properties[i].Display.Type == DisplayType.CLEMPastureFileReader)) { cell.EditorType = EditorTypeEnum.DropDown; List <string> fieldNames = new List <string>(); Simulation clemParent = model.FindAncestor <Simulation>(); // get Pasture file names fieldNames.AddRange(clemParent.FindAllDescendants <FilePasture>().Select(a => a.Name).ToList()); fieldNames.AddRange(clemParent.FindAllDescendants <FileSQLitePasture>().Select(a => a.Name).ToList()); if (fieldNames.Count != 0) { cell.DropDownStrings = fieldNames.ToArray(); } } else if (properties[i].Display != null && properties[i].Display.Type == DisplayType.Model) { cell.EditorType = EditorTypeEnum.DropDown; string[] modelNames = GetModelNames(properties[i].Display.ModelType); if (modelNames != null) { cell.DropDownStrings = modelNames; } } else { object cellValue = properties[i].ValueWithArrayHandling; if (cellValue is DateTime) { cell.EditorType = EditorTypeEnum.DateTime; } else if (cellValue is bool) { cell.EditorType = EditorTypeEnum.Boolean; } else if (cellValue.GetType().IsEnum) { cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = VariableProperty.EnumToStrings(cellValue); Enum cellValueAsEnum = cellValue as Enum; if (cellValueAsEnum != null) { cell.Value = VariableProperty.GetEnumDescription(cellValueAsEnum); } } else if (cellValue.GetType() == typeof(IPlant)) { cell.EditorType = EditorTypeEnum.DropDown; List <string> cropNames = new List <string>(); foreach (Model crop in model.FindAllInScope <IPlant>()) { cropNames.Add(crop.Name); } cell.DropDownStrings = cropNames.ToArray(); } else if (properties[i].DataType == typeof(IPlant)) { cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = model.FindAllInScope <IPlant>().OfType <IModel>().Select(m => m.Name).ToArray(); } else if (!string.IsNullOrWhiteSpace(properties[i].Display?.Values)) { explorerPresenter.ApsimXFile.Links.Resolve(model, allLinks: true, throwOnFail: false); MethodInfo method = model.GetType().GetMethod(properties[i].Display.Values); string[] values = ((IEnumerable <object>)method.Invoke(model, null))?.Select(v => v?.ToString())?.ToArray(); cell.EditorType = EditorTypeEnum.DropDown; cell.DropDownStrings = values; } else { cell.EditorType = EditorTypeEnum.TextBox; } } cell.IsRowReadonly = !IsPropertyEnabled(i); } IGridColumn descriptionColumn = grid.GetColumn(0); descriptionColumn.Width = -1; descriptionColumn.ReadOnly = true; IGridColumn valueColumn = grid.GetColumn(1); valueColumn.Width = -1; }