Index: Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj =================================================================== diff -u -r44055100aa3c3f382227becdaeae7d97c75d386c -r2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42 --- Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj (.../Core.Common.Controls.csproj) (revision 44055100aa3c3f382227becdaeae7d97c75d386c) +++ Core/Common/src/Core.Common.Controls/Core.Common.Controls.csproj (.../Core.Common.Controls.csproj) (revision 2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42) @@ -79,6 +79,7 @@ Properties\GlobalAssembly.cs + Form Index: Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewComboBoxItemWrapper.cs =================================================================== diff -u --- Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewComboBoxItemWrapper.cs (revision 0) +++ Core/Common/src/Core.Common.Controls/DataGrid/DataGridViewComboBoxItemWrapper.cs (revision 2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42) @@ -0,0 +1,104 @@ +// Copyright (C) Stichting Deltares 2016. All rights reserved. +// +// This file is part of Ringtoets. +// +// Ringtoets is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program 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 Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +// +// All names, logos, and references to "Deltares" are registered trademarks of +// Stichting Deltares and remain full property of Stichting Deltares at all times. +// All rights reserved. + +using System.Collections.Generic; +using System.Windows.Forms; + +namespace Core.Common.Controls.DataGrid +{ + /// + /// Wrapper for presenting items in a . + /// + /// The type of the object to present in the . + public class DataGridViewComboBoxItemWrapper + { + private readonly T wrappedObject; + + /// + /// Creates a new instance of the . + /// + /// The wrapped object. + public DataGridViewComboBoxItemWrapper(T wrappedObject) + { + this.wrappedObject = wrappedObject; + } + + /// + /// Gets the display name for the combobox item. + /// + public string DisplayName + { + get + { + return wrappedObject.ToString(); + } + } + + /// + /// Gets a reference to the current wrapper instance. + /// + public DataGridViewComboBoxItemWrapper This + { + get + { + return this; + } + } + + /// + /// Gets the wrapped object. + /// + public T WrappedObject + { + get + { + return wrappedObject; + } + } + + private bool Equals(DataGridViewComboBoxItemWrapper other) + { + return EqualityComparer.Default.Equals(wrappedObject, other.wrappedObject); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) + { + return false; + } + if (ReferenceEquals(this, obj)) + { + return true; + } + if (obj.GetType() != this.GetType()) + { + return false; + } + return Equals((DataGridViewComboBoxItemWrapper)obj); + } + + public override int GetHashCode() + { + return EqualityComparer.Default.GetHashCode(wrappedObject); + } + } +} Index: Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj =================================================================== diff -u -r1cfb16d56f5e2e0067f1e9d9b8af401883697687 -r2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42 --- Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj (.../Core.Common.Controls.Test.csproj) (revision 1cfb16d56f5e2e0067f1e9d9b8af401883697687) +++ Core/Common/test/Core.Common.Controls.Test/Core.Common.Controls.Test.csproj (.../Core.Common.Controls.Test.csproj) (revision 2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42) @@ -53,6 +53,7 @@ + Index: Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewComboBoxItemWrapperTest.cs =================================================================== diff -u --- Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewComboBoxItemWrapperTest.cs (revision 0) +++ Core/Common/test/Core.Common.Controls.Test/DataGrid/DataGridViewComboBoxItemWrapperTest.cs (revision 2bece99f4c12df81fb2a6854a24c0e9f4d4d3c42) @@ -0,0 +1,121 @@ +using Core.Common.Controls.DataGrid; +using NUnit.Framework; + +namespace Core.Common.Controls.Test.DataGrid +{ + [TestFixture] + public class DataGridViewComboBoxItemWrapperTest + { + [Test] + public void Constructor_ExpectedValues() + { + // Setup + var testClass = new TestClass(); + + // Call + var dataGridViewComboBoxItemWrapper = new DataGridViewComboBoxItemWrapper(testClass); + + // Assert + Assert.AreEqual("Test class", dataGridViewComboBoxItemWrapper.DisplayName); + Assert.AreEqual(testClass, dataGridViewComboBoxItemWrapper.WrappedObject); + Assert.AreEqual(dataGridViewComboBoxItemWrapper, dataGridViewComboBoxItemWrapper.This); + } + + [Test] + public void Equals_EqualsWithItself_ReturnTrue() + { + // Setup + var dataGridViewComboBoxItemWrapper = new DataGridViewComboBoxItemWrapper(new TestClass()); + + // Call + var isEqual = dataGridViewComboBoxItemWrapper.Equals(dataGridViewComboBoxItemWrapper); + + // Assert + Assert.IsTrue(isEqual); + } + + [Test] + public void Equals_EqualsWithNull_ReturnFalse() + { + // Setup + var dataGridViewComboBoxItemWrapper = new DataGridViewComboBoxItemWrapper(new TestClass()); + + // Call + var isEqual = dataGridViewComboBoxItemWrapper.Equals(null); + + // Assert + Assert.IsFalse(isEqual); + } + + [Test] + public void Equals_EqualsWithOtherTypeOfObject_ReturnFalse() + { + // Setup + var objectOfDifferentType = new object(); + var dataGridViewComboBoxItemWrapper = new DataGridViewComboBoxItemWrapper(new TestClass()); + + // Call + var isEqual = dataGridViewComboBoxItemWrapper.Equals(objectOfDifferentType); + + // Assert + Assert.IsFalse(isEqual); + } + + [Test] + public void Equals_EqualsWithOtherEqualWrappedObject_ReturnTrue() + { + // Setup + var testClass = new TestClass(); + var dataGridViewComboBoxItemWrapper1 = new DataGridViewComboBoxItemWrapper(testClass); + var dataGridViewComboBoxItemWrapper2 = new DataGridViewComboBoxItemWrapper(testClass); + + // Call + var isEqual1 = dataGridViewComboBoxItemWrapper1.Equals(dataGridViewComboBoxItemWrapper2); + var isEqual2 = dataGridViewComboBoxItemWrapper2.Equals(dataGridViewComboBoxItemWrapper1); + + // Assert + Assert.IsTrue(isEqual1); + Assert.IsTrue(isEqual2); + } + + [Test] + public void Equals_TwoUnequalWrappedObjectInstances_ReturnFalse() + { + // Setup + var dataGridViewComboBoxItemWrapper1 = new DataGridViewComboBoxItemWrapper(new TestClass()); + var dataGridViewComboBoxItemWrapper2 = new DataGridViewComboBoxItemWrapper(new TestClass()); + + // Call + var isEqual1 = dataGridViewComboBoxItemWrapper1.Equals(dataGridViewComboBoxItemWrapper2); + var isEqual2 = dataGridViewComboBoxItemWrapper2.Equals(dataGridViewComboBoxItemWrapper1); + + // Assert + Assert.IsFalse(isEqual1); + Assert.IsFalse(isEqual2); + } + + [Test] + public void GetHashCode_TwoEqualWrappedObjectInstances_ReturnSameHash() + { + // Setup + var testClass = new TestClass(); + var dataGridViewComboBoxItemWrapper1 = new DataGridViewComboBoxItemWrapper(testClass); + var dataGridViewComboBoxItemWrapper2 = new DataGridViewComboBoxItemWrapper(testClass); + + // Call + int hash1 = dataGridViewComboBoxItemWrapper1.GetHashCode(); + int hash2 = dataGridViewComboBoxItemWrapper2.GetHashCode(); + + // Assert + Assert.AreEqual(hash1, hash2); + } + + private class TestClass + { + public override string ToString() + { + return "Test class"; + } + } + } +}