private void TbName_Validating(object sender, EventArgs e) { try { bool cancel = false; if (string.IsNullOrWhiteSpace(tbName.Text) && SupplementNames.Length > 0) { cancel = true; MessageDialog md = new MessageDialog(MainWidget.Toplevel as Window, DialogFlags.Modal, MessageType.Warning, ButtonsType.Ok, "You must provide a name for the supplement"); md.Title = "Invalid entry"; md.Run(); md.Cleanup(); } if (!cancel) { if (SuppAttrChanged != null) { TStringArgs args = new TStringArgs(); args.Name = tbName.Text; if (SuppNameChanged != null) { SuppNameChanged.Invoke(sender, args); } } } } catch (Exception err) { ShowError(err); } }
/// <summary> /// Show an error message to caller. /// </summary> public void ShowMsg(string message) { MessageDialog md = new MessageDialog(window1, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, message); md.Run(); md.Cleanup(); }
/// <summary> /// Show an error message to caller. /// </summary> public void ShowMsg(string message) { MessageDialog md = new MessageDialog(Editor.Toplevel as Window, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, message); md.Run(); md.Cleanup(); }
private void ShowMessage(MessageType type, string msg, string title) { MessageDialog md = new MessageDialog(dialog1, DialogFlags.Modal, type, ButtonsType.Ok, msg); md.Title = title; md.Run(); md.Cleanup(); }
/// <summary>Ask the user a question</summary> /// <param name="message">The message to show the user.</param> public QuestionResponseEnum AskQuestion(string message) { MessageDialog md = new MessageDialog(MainWidget.Toplevel as Window, DialogFlags.Modal, MessageType.Question, ButtonsType.YesNo, message); md.Title = "Save changes"; int result = md.Run(); md.Cleanup(); switch ((ResponseType)result) { case ResponseType.Yes: return(QuestionResponseEnum.Yes); case ResponseType.No: return(QuestionResponseEnum.No); default: return(QuestionResponseEnum.Cancel); } }
private void TbAmount_Validating(object sender, EventArgs e) { try { double value; bool cancel = false; if (string.IsNullOrWhiteSpace(tbAmount.Text)) // Treat blank as a value of 0. { value = 0.0; } else if (!Double.TryParse(tbAmount.Text, out value) || value < 0.0) { cancel = true; MessageDialog md = new MessageDialog(MainWidget.Toplevel as Window, DialogFlags.Modal, MessageType.Warning, ButtonsType.Ok, "Value should be a non-negative number"); md.Title = "Invalid entry"; md.Run(); md.Cleanup(); } if (!cancel) { if (SuppAttrChanged != null) { TSuppAttrArgs args = new TSuppAttrArgs(); args.Attr = -1; args.AttrVal = value; if (SuppAttrChanged != null) { SuppAttrChanged.Invoke(sender, args); } } } } catch (Exception err) { ShowError(err); } }
public string GetPatchPoint() { string newWeatherPath = null; string dest = PathUtilities.GetAbsolutePath(entryFilePath.Text, this.explorerPresenter.ApsimXFile.FileName); DateTime startDate = calendarStart.Date; if (startDate.Year < 1889) { ShowMessage(MessageType.Warning, "SILO data is not available before 1889", "Invalid start date"); return(null); } DateTime endDate = calendarEnd.Date; if (endDate.CompareTo(DateTime.Today) >= 0) { ShowMessage(MessageType.Warning, "SILO data end date can be no later than yesterday", "Invalid end date"); return(null); } if (endDate.CompareTo(startDate) < 0) { ShowMessage(MessageType.Warning, "The end date must be after the start date!", "Invalid dates"); return(null); } if (String.IsNullOrWhiteSpace(entryEmail.Text)) { ShowMessage(MessageType.Warning, "The SILO data API requires you to provide your e-mail address", "E-mail address required"); return(null); } // Patch point get a bit complicated. We need a BOM station number, but can't really expect the user // to know that in advance. So what we can attempt to do is use the provided lat and long in the geocoding service // to get us a placename, then use the SILO name search API to find a list of stations for us. // If we get multiple stations, we let the user choose the one they wish to use. string url = googleGeocodingApi + "latlng=" + entryLatitude.Text + ',' + entryLongitude.Text; try { MemoryStream stream = WebUtilities.ExtractDataFromURL(url); stream.Position = 0; JsonTextReader reader = new JsonTextReader(new StreamReader(stream)); // Parsing the JSON gets a little tricky with a forward-reading parser. We're trying to track // down a "short_name" address component of "locality" type (I guess). string locName = ""; while (reader.Read()) { if (reader.TokenType == JsonToken.PropertyName && reader.Value.Equals("address_components")) { reader.Read(); if (reader.TokenType == JsonToken.StartArray) { JArray arr = JArray.Load(reader); foreach (JToken token in arr) { JToken typesToken = token.Last; JArray typesArray = typesToken.First as JArray; for (int i = 0; i < typesArray.Count; i++) { if (typesArray[i].ToString() == "locality") { locName = token["short_name"].ToString(); break; } } if (!string.IsNullOrEmpty(locName)) { break; } } } } if (!string.IsNullOrEmpty(locName)) { break; } } if (string.IsNullOrEmpty(locName)) { ShowMessage(MessageType.Error, "Unable to find a name key for the specified location", "Error determining location"); return(null); } int stationNumber = -1; if (locName.Contains(" ")) // the SILO API doesn't handle spaces well { Regex regex = new Regex(" ."); locName = regex.Replace(locName, "_"); } string stationUrl = String.Format("https://www.longpaddock.qld.gov.au/cgi-bin/silo/PatchedPointDataset.php?format=name&nameFrag={0}", locName); MemoryStream stationStream = WebUtilities.ExtractDataFromURL(stationUrl); stationStream.Position = 0; StreamReader streamReader = new StreamReader(stationStream); string stationInfo = streamReader.ReadToEnd(); string[] stationLines = stationInfo.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None); if (stationLines.Length == 0) { ShowMessage(MessageType.Error, "Unable to find a BOM station for this location", "Cannot find station"); return(null); } if (stationLines.Length == 1) { string[] lineInfo = stationLines[0].Split('|'); stationNumber = Int32.Parse(lineInfo[0]); } else { MessageDialog md = new MessageDialog(owningView.MainWidget.Toplevel as Window, DialogFlags.Modal, MessageType.Question, ButtonsType.OkCancel, "Which station do you wish to use?"); md.Title = "Select BOM Station"; Gtk.TreeView tree = new Gtk.TreeView(); ListStore list = new ListStore(typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string), typeof(string)); tree.AppendColumn("Number", new CellRendererText(), "text", 0); tree.AppendColumn("Name", new CellRendererText(), "text", 1); tree.AppendColumn("Latitude", new CellRendererText(), "text", 2); tree.AppendColumn("Longitude", new CellRendererText(), "text", 3); tree.AppendColumn("State", new CellRendererText(), "text", 4); tree.AppendColumn("Elevation", new CellRendererText(), "text", 5); tree.AppendColumn("Notes", new CellRendererText(), "text", 6); foreach (string stationLine in stationLines) { string[] lineInfo = stationLine.Split('|'); list.AppendValues(lineInfo); } tree.Model = list; tree.RowActivated += OnPatchPointSoilSelected; #if NETFRAMEWORK Box box = md.VBox; #else Box box = md.ContentArea; #endif box.PackStart(tree, true, true, 5); box.ShowAll(); ResponseType result = (ResponseType)md.Run(); if (result == ResponseType.Ok) { TreeIter iter; tree.Selection.GetSelected(out iter); string stationString = (string)list.GetValue(iter, 0); stationNumber = Int32.Parse(stationString); } md.Cleanup(); } if (stationNumber >= 0) // Phew! We finally have a station number. Now fetch the data. { string pointUrl = String.Format("https://www.longpaddock.qld.gov.au/cgi-bin/silo/PatchedPointDataset.php?start={0:yyyyMMdd}&finish={1:yyyyMMdd}&station={2}&format=apsim&username={3}", startDate, endDate, stationNumber, System.Net.WebUtility.UrlEncode(entryEmail.Text)); MemoryStream pointStream = WebUtilities.ExtractDataFromURL(pointUrl); pointStream.Seek(0, SeekOrigin.Begin); string headerLine = new StreamReader(pointStream).ReadLine(); pointStream.Seek(0, SeekOrigin.Begin); if (headerLine.StartsWith("[weather.met.weather]")) { using (FileStream fs = new FileStream(dest, FileMode.Create)) { pointStream.CopyTo(fs); fs.Flush(); } if (File.Exists(dest)) { newWeatherPath = dest; } } else { ShowMessage(MessageType.Error, new StreamReader(pointStream).ReadToEnd(), "Not valid APSIM weather data"); } } } catch (Exception err) { ShowMessage(MessageType.Error, err.Message, "Error"); } return(newWeatherPath); }
private void RealEditValidator(object sender, EventArgs e) { try { Entry tb = sender as Entry; if (tb != null) { FoodSupplement.SuppAttribute tagEnum; if (entryLookup.TryGetValue(tb, out tagEnum)) { double maxVal = 0.0; double scale = 1.0; switch (tagEnum) { case FoodSupplement.SuppAttribute.spaDMP: case FoodSupplement.SuppAttribute.spaDMD: case FoodSupplement.SuppAttribute.spaEE: case FoodSupplement.SuppAttribute.spaDG: maxVal = 100.0; scale = 0.01; break; case FoodSupplement.SuppAttribute.spaMEDM: maxVal = 20.0; break; case FoodSupplement.SuppAttribute.spaCP: maxVal = 300.0; scale = 0.01; break; case FoodSupplement.SuppAttribute.spaPH: case FoodSupplement.SuppAttribute.spaSU: case FoodSupplement.SuppAttribute.spaADIP: maxVal = 100.0; scale = 0.01; break; default: maxVal = 100.0; break; } double value; bool cancel = false; if (string.IsNullOrWhiteSpace(tb.Text)) // Treat blank as a value of 0. { value = 0.0; } else if (!Double.TryParse(tb.Text, out value) || value < 0.0 || value > maxVal) { // e.Cancel = true; cancel = true; MessageDialog md = new MessageDialog(MainWidget.Toplevel as Window, DialogFlags.Modal, MessageType.Warning, ButtonsType.Ok, String.Format("Value should be a number in the range 0 to {0:F2}", maxVal)); md.Title = "Invalid entry"; md.Run(); md.Cleanup(); } if (!cancel) { if (SuppAttrChanged != null) { TSuppAttrArgs args = new TSuppAttrArgs(); args.Attr = (int)tagEnum; args.AttrVal = value * scale; if (SuppAttrChanged != null) { SuppAttrChanged.Invoke(sender, args); } } } } } } catch (Exception err) { ShowError(err); } }