Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.Core/Auxiliary.cs @ 15033

Last change on this file since 15033 was 2, checked in by swagner, 17 years ago

Added HeuristicLab 3.0 sources from former SVN repository at revision 52

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Windows.Forms;
26
27namespace HeuristicLab.Core {
28  public static class Auxiliary {
29    #region Cloning
30    public static object Clone(IStorable obj, IDictionary<Guid, object> clonedObjects) {
31      object clone;
32      if (clonedObjects.TryGetValue(obj.Guid, out clone))
33        return clone;
34      else
35        return obj.Clone(clonedObjects);
36    }
37    #endregion
38
39    #region Error Messages
40    public static void ShowErrorMessageBox(string message) {
41      MessageBox.Show(message,
42                      "Error",
43                      MessageBoxButtons.OK,
44                      MessageBoxIcon.Error);
45    }
46    public static void ShowErrorMessageBox(Exception ex) {
47      MessageBox.Show(BuildErrorMessage(ex),
48                      "Error - " + ex.GetType().Name,
49                      MessageBoxButtons.OK,
50                      MessageBoxIcon.Error);
51    }
52    private static string BuildErrorMessage(Exception ex) {
53      StringBuilder sb = new StringBuilder();
54      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
55
56      while (ex.InnerException != null) {
57        ex = ex.InnerException;
58        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
59      }
60      return sb.ToString();
61    }
62    #endregion
63
64    #region Constraint Violation Messages
65    public static void ShowConstraintViolationMessageBox(ICollection<IConstraint> violatedConstraints) {
66      string message = BuildConstraintViolationMessage(violatedConstraints);
67      MessageBox.Show("The following constraints are violated. The operation could not be completed.\n\n" + message,
68                      "Constraint Violation",
69                      MessageBoxButtons.OK,
70                      MessageBoxIcon.Warning);
71    }
72    public static DialogResult ShowIgnoreConstraintViolationMessageBox(ICollection<IConstraint> violatedConstraints) {
73      string message = BuildConstraintViolationMessage(violatedConstraints);
74      return MessageBox.Show("The following constraints are violated. Do you want to complete the operation anyhow?\n\n" + message,
75                             "Constraint Violation",
76                             MessageBoxButtons.YesNo,
77                             MessageBoxIcon.Question);
78    }
79    private static string BuildConstraintViolationMessage(ICollection<IConstraint> violatedConstraints) {
80      StringBuilder sb = new StringBuilder();
81      foreach (IConstraint constraint in violatedConstraints) {
82        sb.AppendLine(constraint.GetType().Name);
83        sb.AppendLine(constraint.Description);
84        sb.AppendLine();
85      }
86      return sb.ToString();
87    }
88    #endregion
89  }
90}
Note: See TracBrowser for help on using the repository browser.