// Copyright (C) Stichting Deltares 2017. 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;
using System.ComponentModel;
using System.Globalization;
using Core.Common.Gui.Properties;
namespace Core.Common.Gui.Converters
{
///
/// with modified conversion to string and shows an array
/// starting with index 1 instead of 0.
///
public class ExpandableArrayConverter : ArrayConverter
{
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var array = value as Array;
if (destinationType == typeof(string) && array != null)
{
return string.Format(CultureInfo.CurrentCulture,
Resources.ExpandableArrayConverter_ConvertTo_Count_0_,
array.GetLength(0));
}
return base.ConvertTo(context, culture, value, destinationType);
}
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
PropertyDescriptor[] properties = null;
var array = value as Array;
if (array != null)
{
int length = array.GetLength(0);
properties = new PropertyDescriptor[length];
Type type = array.GetType();
Type elementType = type.GetElementType();
for (var index = 0; index < length; ++index)
{
properties[index] = CreateElementPropertyDescriptor(type, elementType, index);
}
}
return new PropertyDescriptorCollection(properties);
}
///
/// Creates a new instance of type .
///
/// Type of the array.
/// Type of the elements in .
/// Index of the element corresponding with this property descriptor.
/// New instance of .
///
protected virtual PropertyDescriptor CreateElementPropertyDescriptor(Type type, Type elementType, int index)
{
return new ArrayPropertyDescriptor(type, elementType, index);
}
#region Nested Type: ArrayPropertyDescriptor
///
/// Array element property descriptor used by .
/// Properties are named based on their index + 1.
///
protected class ArrayPropertyDescriptor : SimplePropertyDescriptor
{
private readonly int index;
///
/// Initializes a new instance of the class.
///
/// Type of the array.
/// Type of the elements in .
/// Index of the element corresponding with this property descriptor.
public ArrayPropertyDescriptor(Type arrayType, Type elementType, int elementIndex)
: base(arrayType, "[" + (elementIndex + 1) + "]", elementType, null)
{
index = elementIndex;
}
public override object GetValue(object component)
{
var array = (Array) component;
return array.GetValue(index);
}
public override void SetValue(object component, object value)
{
var array = (Array) component;
array.SetValue(value, index);
// This class is based on the System.ComponentModel.ArrayConverter.ArrayPropertyDescriptor,
// and there the SetValue also called OnValueChanged. Copying that behavior here as well.
OnValueChanged(array, EventArgs.Empty);
}
}
#endregion
}
}