Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Core/3.2/Auxiliary.cs @ 1529

Last change on this file since 1529 was 1529, checked in by gkronber, 15 years ago

Moved source files of plugins AdvancedOptimizationFrontEnd ... Grid into version-specific sub-folders. #576

File size: 5.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  /// <summary>
29  /// Static helper class.
30  /// </summary>
31  public static class Auxiliary {
32    #region Cloning
33    /// <summary>
34    /// Clones the given <paramref name="obj"/> (deep clone).
35    /// </summary>
36    /// <remarks>Checks before clone if object has not already been cloned.</remarks>
37    /// <param name="obj">The object to clone.</param>
38    /// <param name="clonedObjects">A dictionary of all already cloned objects. (Needed to avoid cycles.)</param>
39    /// <returns>The cloned object.</returns>
40    public static object Clone(IStorable obj, IDictionary<Guid, object> clonedObjects) {
41      object clone;
42      if (clonedObjects.TryGetValue(obj.Guid, out clone))
43        return clone;
44      else
45        return obj.Clone(clonedObjects);
46    }
47    #endregion
48
49    #region Error Messages
50    /// <summary>
51    /// Shows an error message box with a given error <paramref name="message"/> and an OK-Button.
52    /// </summary>
53    /// <param name="message">The error message to display.</param>
54    public static void ShowErrorMessageBox(string message) {
55      MessageBox.Show(message,
56                      "Error",
57                      MessageBoxButtons.OK,
58                      MessageBoxIcon.Error);
59    }
60    /// <summary>
61    /// Shows an error message box with a given exception <paramref name="ex"/> and an OK-Button.
62    /// </summary>
63    /// <param name="ex">The exception to display.</param>
64    public static void ShowErrorMessageBox(Exception ex) {
65      MessageBox.Show(BuildErrorMessage(ex),
66                      "Error - " + ex.GetType().Name,
67                      MessageBoxButtons.OK,
68                      MessageBoxIcon.Error);
69    }
70    /// <summary>
71    /// Builds an error message out of an exception and formats it accordingly.
72    /// </summary>
73    /// <param name="ex">The exception to format.</param>
74    /// <returns>The formated message.</returns>
75    private static string BuildErrorMessage(Exception ex) {
76      StringBuilder sb = new StringBuilder();
77      sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace);
78
79      while (ex.InnerException != null) {
80        ex = ex.InnerException;
81        sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace);
82      }
83      return sb.ToString();
84    }
85    #endregion
86
87    #region Constraint Violation Messages
88    /// <summary>
89    /// Shows a warning message box with an OK-Button, indicating that the given constraints were violated and so
90    /// the operation could not be completed.
91    /// </summary>
92    /// <param name="violatedConstraints">The constraints that could not be fulfilled.</param>
93    public static void ShowConstraintViolationMessageBox(ICollection<IConstraint> violatedConstraints) {
94      string message = BuildConstraintViolationMessage(violatedConstraints);
95      MessageBox.Show("The following constraints are violated. The operation could not be completed.\n\n" + message,
96                      "Constraint Violation",
97                      MessageBoxButtons.OK,
98                      MessageBoxIcon.Warning);
99    }
100    /// <summary>
101    /// Shows a question message box with a yes-no option, where to choose whether to ignore
102    /// the given violated constraints and to complete the operation or not.
103    /// </summary>
104    /// <param name="violatedConstraints">The constraints that could not be fulfilled.</param>
105    /// <returns>The result of the choice ("Yes" = 6, "No" = 7).</returns>
106    public static DialogResult ShowIgnoreConstraintViolationMessageBox(ICollection<IConstraint> violatedConstraints) {
107      string message = BuildConstraintViolationMessage(violatedConstraints);
108      return MessageBox.Show("The following constraints are violated. Do you want to complete the operation anyhow?\n\n" + message,
109                             "Constraint Violation",
110                             MessageBoxButtons.YesNo,
111                             MessageBoxIcon.Question);
112    }
113    /// <summary>
114    /// Builds a message out of a given collection of violated constraints,
115    /// including the constraints type and description.
116    /// </summary>
117    /// <param name="violatedConstraints">The constraints that could not be fulfilled.</param>
118    /// <returns>The message to display.</returns>
119    private static string BuildConstraintViolationMessage(ICollection<IConstraint> violatedConstraints) {
120      StringBuilder sb = new StringBuilder();
121      foreach (IConstraint constraint in violatedConstraints) {
122        sb.AppendLine(constraint.GetType().Name);
123        sb.AppendLine(constraint.Description);
124        sb.AppendLine();
125      }
126      return sb.ToString();
127    }
128    #endregion
129  }
130}
Note: See TracBrowser for help on using the repository browser.