/// <summary> /// Open a dialog for downloading a new weather file /// </summary> public void DownloadWeather() { Model model = this.ApsimXFile.FindByPath(this.CurrentNodePath)?.Value as Model; if (model != null) { Utility.WeatherDownloadDialog dlg = new Utility.WeatherDownloadDialog(); IModel currentNode = ApsimXFile.FindByPath(CurrentNodePath)?.Value as IModel; dlg.ShowFor(model, (view as ExplorerView), currentNode, this); } }
/// <summary> /// Called after undoing/redoing a command. /// Selects the model which was affected by the command. /// </summary> /// <param name="affectedModel">The model which was affected by the command.</param> /// <remarks> /// When the user undoes/redoes something we want to select the affected /// model. Therefore this callback is used for both undo and redo operations. /// </remarks> public void OnUndoRedo(IModel model) { Refresh(); if (model != null) { if (ApsimXFile.FindAllDescendants().Contains(model)) { SelectNode(model); } else if (model.Parent != null && ApsimXFile.FindAllDescendants().Contains(model.Parent)) { SelectNode(model.Parent); } } }
/// <summary>User has attempted to move the current node up.</summary> /// <param name="sender">Sender object</param> /// <param name="e">Event arguments</param> private void OnMoveUp(object sender, EventArgs e) { try { Model model = ApsimXFile.FindByPath(view.Tree.SelectedNode)?.Value as Model; if (model != null && model.Parent != null) { IModel firstModel = model.Parent.Children[0]; if (model != firstModel) { CommandHistory.Add(new MoveModelUpDownCommand(model, true)); } } } catch (Exception err) { MainPresenter.ShowError(err); } }
/// <summary>Save all changes.</summary> /// <returns>True if file was saved.</returns> public bool Save() { // Need to hide the right hand panel because some views may not have saved // their contents until they get a 'Detach' call. try { HideRightHandPanel(); } catch (Exception err) { MainPresenter.ShowError(err); } if (string.IsNullOrEmpty(ApsimXFile.FileName)) { SaveAs(); } if (!string.IsNullOrEmpty(ApsimXFile.FileName)) { ApsimXFile.Write(ApsimXFile.FileName); MainPresenter.ShowMessage(string.Format("Successfully saved to {0}", StringUtilities.PangoString(ApsimXFile.FileName)), Simulation.MessageType.Information); return(true); } try { ShowRightHandPanel(); } catch (Exception err) { MainPresenter.ShowError(err); } return(false); }
/// <summary>Pastes the contents of the clipboard.</summary> public void Add(string xml, string parentPath) { try { XmlDocument document = new XmlDocument(); try { document.LoadXml(xml); } catch (XmlException) { MainPresenter.ShowMessage("Invalid XML. Are you sure you're trying to paste an APSIM model?", Simulation.ErrorLevel.Error); } object newModel = XmlUtilities.Deserialise(document.DocumentElement, ApsimXFile.GetType().Assembly); // See if the presenter is happy with this model being added. Model parentModel = Apsim.Get(this.ApsimXFile, parentPath) as Model; AllowDropArgs allowDropArgs = new AllowDropArgs(); allowDropArgs.NodePath = parentPath; allowDropArgs.DragObject = new DragObject() { NodePath = null, ModelType = newModel.GetType(), Xml = GetClipboardText() }; this.OnAllowDrop(null, allowDropArgs); // If it is happy then issue an AddModelCommand. if (allowDropArgs.Allow) { // If the model xml is a soil object then try and convert from old // APSIM format to new. if (document.DocumentElement.Name == "Soil" && XmlUtilities.Attribute(document.DocumentElement, "Name") != "") { XmlDocument newDoc = new XmlDocument(); newDoc.AppendChild(newDoc.CreateElement("D")); APSIMImporter importer = new APSIMImporter(); importer.ImportSoil(document.DocumentElement, newDoc.DocumentElement, newDoc.DocumentElement); XmlNode soilNode = XmlUtilities.FindByType(newDoc.DocumentElement, "Soil"); if (soilNode != null && XmlUtilities.FindByType(soilNode, "Sample") == null && XmlUtilities.FindByType(soilNode, "InitialWater") == null) { // Add in an initial water and initial conditions models. XmlNode initialWater = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("InitialWater")); XmlUtilities.SetValue(initialWater, "Name", "Initial water"); XmlUtilities.SetValue(initialWater, "PercentMethod", "FilledFromTop"); XmlUtilities.SetValue(initialWater, "FractionFull", "1"); XmlUtilities.SetValue(initialWater, "DepthWetSoil", "NaN"); XmlNode initialConditions = soilNode.AppendChild(soilNode.OwnerDocument.CreateElement("Sample")); XmlUtilities.SetValue(initialConditions, "Name", "Initial conditions"); XmlUtilities.SetValue(initialConditions, "Thickness/double", "1800"); XmlUtilities.SetValue(initialConditions, "NO3/double", "10"); XmlUtilities.SetValue(initialConditions, "NH4/double", "1"); XmlUtilities.SetValue(initialConditions, "NO3Units", "kgha"); XmlUtilities.SetValue(initialConditions, "NH4Units", "kgha"); XmlUtilities.SetValue(initialConditions, "SWUnits", "Volumetric"); } document.LoadXml(newDoc.DocumentElement.InnerXml); } IModel child = XmlUtilities.Deserialise(document.DocumentElement, ApsimXFile.GetType().Assembly) as IModel; AddModelCommand command = new AddModelCommand(parentModel, document.DocumentElement, GetNodeDescription(child), view); this.CommandHistory.Add(command, true); } } catch (Exception exception) { this.MainPresenter.ShowMessage(exception.Message, Simulation.ErrorLevel.Error); } }
/// <summary>Do the actual write to the file</summary> /// <param name="fileName">Path to which the file will be saved.</param> public void WriteSimulation(string fileName) { ApsimXFile.ExplorerWidth = TreeWidth; ApsimXFile.Write(fileName); CommandHistory.Save(); }