/// <summary>The user has clicked the add button.</summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private void OnAddButtonClicked(object sender, EventArgs e) { try { Apsim.ModelDescription selectedModelType = GetModelDescription(tree.SelectedNode); if (selectedModelType != null) { this.explorerPresenter.MainPresenter.ShowWaitCursor(true); IModel child = (IModel)Activator.CreateInstance(selectedModelType.ModelType, true); child.Name = selectedModelType.ModelName; if (child is ModelCollectionFromResource resource) { resource.ResourceName = selectedModelType.ModelName; } var command = new AddModelCommand(Apsim.FullPath(this.model), child, explorerPresenter); explorerPresenter.CommandHistory.Add(command, true); } } finally { this.explorerPresenter.MainPresenter.ShowWaitCursor(false); } }
private static void AddTreeNodeIfDoesntExist(Apsim.ModelDescription modelThatCanBeAdded, TreeViewNode parent) { var namespaceWords = modelThatCanBeAdded.ModelType.Namespace.Split(".".ToCharArray()).ToList(); // Remove the first namespace word ('Models') namespaceWords.Remove(namespaceWords.First()); foreach (var namespaceWord in namespaceWords.Where(word => word != "Models")) { var node = parent.Children.Find(n => n.Name == namespaceWord); if (node == null) { node = new TreeViewNode() { Name = namespaceWord, ResourceNameForImage = ExplorerPresenter.GetIconResourceName(typeof(Folder), null, null) }; parent.Children.Add(node); } parent = node; } // Add the last model var description = new TreeViewNode() { Name = modelThatCanBeAdded.ModelName, ResourceNameForImage = ExplorerPresenter.GetIconResourceName(modelThatCanBeAdded.ModelType, modelThatCanBeAdded.ModelName, modelThatCanBeAdded.ResourceString) }; parent.Children.Add(description); }
private static void AddTreeNodeIfDoesntExist(Apsim.ModelDescription modelThatCanBeAdded, TreeViewNode parent) { List <string> namespaceWords; // Remove the first namespace word ('Models') bool resourceIsInSubDirectory = false; if (modelThatCanBeAdded.ResourceString != null) { // Need to determine if the resource name is in a sub directory of Models.Resources e.g. Models.Resources.GrazPlan.Cattle.Angus.json var subDirectory = modelThatCanBeAdded.ResourceString.Replace("Models.Resources.", "").Replace(".json", ""); resourceIsInSubDirectory = subDirectory.Contains('.'); } if (resourceIsInSubDirectory) { var path = modelThatCanBeAdded.ResourceString.Replace("Models.Resources.", ""); namespaceWords = path.Split(".".ToCharArray()).ToList(); namespaceWords.Remove(namespaceWords.Last()); // remove the "json" word at the end. namespaceWords.Remove(namespaceWords.Last()); // remove the model name at the end. } else { namespaceWords = modelThatCanBeAdded.ModelType.Namespace.Split(".".ToCharArray()).ToList(); namespaceWords.Remove(namespaceWords.First()); modelThatCanBeAdded.ResourceString = modelThatCanBeAdded.ModelName; } foreach (var namespaceWord in namespaceWords.Where(word => word != "Models")) { var node = parent.Children.Find(n => n.Name == namespaceWord); if (node == null) { node = new TreeViewNode() { Name = namespaceWord, ResourceNameForImage = ExplorerPresenter.GetIconResourceName(typeof(Folder), null, null) }; parent.Children.Add(node); } parent = node; } // Add the last model var description = new TreeViewNode() { Name = modelThatCanBeAdded.ModelName, ResourceNameForImage = ExplorerPresenter.GetIconResourceName(modelThatCanBeAdded.ModelType, modelThatCanBeAdded.ModelName, modelThatCanBeAdded.ResourceString) }; parent.Children.Add(description); }
/// <summary>The user has clicked the add button.</summary> /// <param name="sender">Event sender</param> /// <param name="e">Event arguments</param> private void OnAddButtonClicked(object sender, EventArgs e) { try { Apsim.ModelDescription selectedModelType = GetModelDescription(tree.SelectedNode); if (selectedModelType != null) { this.explorerPresenter.MainPresenter.ShowWaitCursor(true); IModel child; if (!(selectedModelType.ModelType == typeof(ModelCollectionFromResource)) && selectedModelType.ResourceString != null && selectedModelType.ResourceString.Contains('.')) { List <Exception> exceptions; var contents = ReflectionUtilities.GetResourceAsString(explorerPresenter.ApsimXFile.GetType().Assembly, selectedModelType.ResourceString); child = FileFormat.ReadFromString <Model>(contents, out exceptions); if (child.Children.Count == 1) { child = child.Children[0]; } } else { child = (IModel)Activator.CreateInstance(selectedModelType.ModelType, true); child.Name = selectedModelType.ModelName; if (child is ModelCollectionFromResource resource) { resource.ResourceName = selectedModelType.ResourceString; } } var command = new AddModelCommand(this.model, child); explorerPresenter.CommandHistory.Add(command, true); explorerPresenter.Refresh(); } } finally { this.explorerPresenter.MainPresenter.ShowWaitCursor(false); } }