Changeset 9040 for branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs
- Timestamp:
- 12/12/12 14:53:54 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/RuntimeOptimizer/HeuristicLab.Optimization.Views/3.3/TimeLimitRunView.cs
r8977 r9040 23 23 using System.ComponentModel; 24 24 using System.Linq; 25 using System.Text.RegularExpressions; 25 26 using System.Windows.Forms; 26 27 using HeuristicLab.Collections; … … 48 49 InitializeComponent(); 49 50 snapshotButton.Text = String.Empty; 50 snapshotButton.Image = VSImageLibrary. Breakpoint;51 snapshotButton.Image = VSImageLibrary.Camera; 51 52 } 52 53 … … 208 209 errorProvider.SetError(timeLimitTextBox, String.Empty); 209 210 210 var tokens = snapshotsTextBox.Text.Trim().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);211 212 211 var snapshotTimes = new ObservableList<TimeSpan>(); 213 foreach (var t in tokens) { 212 var matches = Regex.Matches(snapshotsTextBox.Text, @"(\d+[ \t]*\w+)"); 213 foreach (Match m in matches) { 214 214 TimeSpan value; 215 if (!TryGetTimeSpanFromFormat( t, out value)) {216 e.Cancel = true;217 errorProvider.SetError(snapshotsTextBox, "Error parsing " + t+ ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days");215 if (!TryGetTimeSpanFromFormat(m.Value, out value)) { 216 e.Cancel = !snapshotsTextBox.ReadOnly && snapshotsTextBox.Enabled; // don't cancel an operation that cannot be edited 217 errorProvider.SetError(snapshotsTextBox, "Error parsing " + m.Value + ", please provide a valid time span, e.g. 60, 20s, 45 seconds, 3h, 1 hour, 4 d, 2 days"); 218 218 return; 219 219 } else { … … 317 317 private bool TryGetTimeSpanFromFormat(string text, out TimeSpan result) { 318 318 double value; 319 text = text.Trim(); 320 var length = text.Length; 321 while (!double.TryParse(text.Substring(0, length), out value)) { 322 length--; 323 if (length <= 0) { 324 value = double.NaN; 325 break; 326 } 327 } 328 if (double.IsNaN(value)) { 329 result = TimeSpan.Zero; 319 result = TimeSpan.Zero; 320 321 var numberMatch = Regex.Match(text, @"\d+"); 322 if (!numberMatch.Success) return false; 323 324 if (!double.TryParse(numberMatch.Value, out value)) 330 325 return false; 331 } else { 332 var remaining = String.Empty; 333 if (length < text.Length) remaining = text.Substring(length, text.Length - length); 334 switch (remaining.Trim()) { 335 case "d": 336 case "day": 337 case "days": result = TimeSpan.FromDays(value); break; 338 case "h": 339 case "hour": 340 case "hours": result = TimeSpan.FromHours(value); break; 341 case "min": 342 case "minute": 343 case "minutes": result = TimeSpan.FromMinutes(value); break; 344 default: result = TimeSpan.FromSeconds(value); break; 345 } 346 return true; 347 } 326 327 var unitMatch = Regex.Match(text, @"[a-zA-Z]+$"); 328 if (!unitMatch.Success) return false; 329 330 switch (unitMatch.Value) { 331 case "d": 332 case "day": 333 case "days": result = TimeSpan.FromDays(value); break; 334 case "h": 335 case "hour": 336 case "hours": result = TimeSpan.FromHours(value); break; 337 case "min": 338 case "minute": 339 case "minutes": result = TimeSpan.FromMinutes(value); break; 340 case "s": 341 case "second": 342 case "seconds": result = TimeSpan.FromSeconds(value); break; 343 default: return false; 344 } 345 return true; 348 346 } 349 347 }
Note: See TracChangeset
for help on using the changeset viewer.