#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace HeuristicLab.Core { /// /// Static helper class. /// public static class Auxiliary { #region Cloning /// /// Clones the given (deep clone). /// /// Checks before clone if object has not already been cloned. /// The object to clone. /// A dictionary of all already cloned objects. (Needed to avoid cycles.) /// The cloned object. public static object Clone(IStorable obj, IDictionary clonedObjects) { object clone; if (clonedObjects.TryGetValue(obj.Guid, out clone)) return clone; else return obj.Clone(clonedObjects); } #endregion #region Error Messages /// /// Shows an error message box with a given error and an OK-Button. /// /// The error message to display. public static void ShowErrorMessageBox(string message) { MessageBox.Show(message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } /// /// Shows an error message box with a given exception and an OK-Button. /// /// The exception to display. public static void ShowErrorMessageBox(Exception ex) { MessageBox.Show(BuildErrorMessage(ex), "Error - " + ex.GetType().Name, MessageBoxButtons.OK, MessageBoxIcon.Error); } /// /// Builds an error message out of an exception and formats it accordingly. /// /// The exception to format. /// The formated message. private static string BuildErrorMessage(Exception ex) { StringBuilder sb = new StringBuilder(); sb.Append("Sorry, but something went wrong!\n\n" + ex.Message + "\n\n" + ex.StackTrace); while (ex.InnerException != null) { ex = ex.InnerException; sb.Append("\n\n-----\n\n" + ex.Message + "\n\n" + ex.StackTrace); } return sb.ToString(); } #endregion #region Constraint Violation Messages /// /// Shows a warning message box with an OK-Button, indicating that the given constraints were violated and so /// the operation could not be completed. /// /// The constraints that could not be fulfilled. public static void ShowConstraintViolationMessageBox(ICollection violatedConstraints) { string message = BuildConstraintViolationMessage(violatedConstraints); MessageBox.Show("The following constraints are violated. The operation could not be completed.\n\n" + message, "Constraint Violation", MessageBoxButtons.OK, MessageBoxIcon.Warning); } /// /// Shows a question message box with a yes-no option, where to choose whether to ignore /// the given violated constraints and to complete the operation or not. /// /// The constraints that could not be fulfilled. /// The result of the choice ("Yes" = 6, "No" = 7). public static DialogResult ShowIgnoreConstraintViolationMessageBox(ICollection violatedConstraints) { string message = BuildConstraintViolationMessage(violatedConstraints); return MessageBox.Show("The following constraints are violated. Do you want to complete the operation anyhow?\n\n" + message, "Constraint Violation", MessageBoxButtons.YesNo, MessageBoxIcon.Question); } /// /// Builds a message out of a given collection of violated constraints, /// including the constraints type and description. /// /// The constraints that could not be fulfilled. /// The message to display. private static string BuildConstraintViolationMessage(ICollection violatedConstraints) { StringBuilder sb = new StringBuilder(); foreach (IConstraint constraint in violatedConstraints) { sb.AppendLine(constraint.GetType().Name); sb.AppendLine(constraint.Description); sb.AppendLine(); } return sb.ToString(); } #endregion } }