//-------------------------------------------------------------
//
// Copyright © Microsoft Corporation. All Rights Reserved.
//
//-------------------------------------------------------------
// @owner=alexgor, deliant
//=================================================================
// File: ElementPositionConverter.cs
//
// Namespace: DataVisualization.Charting.Design
//
// Classes: ElementPositionConverter
//
// Purpose: Converter of the element position.
//
// Reviewed: AG - August 7, 2002
//
//===================================================================
#region Used Namespaces
using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
#if Microsoft_CONTROL
using System.Windows.Forms.DataVisualization.Charting;
#else
using System.Web.UI.DataVisualization.Charting;
#endif
#endregion
#if Microsoft_CONTROL
namespace System.Windows.Forms.DataVisualization.Charting
#else
namespace System.Web.UI.DataVisualization.Charting
#endif
{
///
/// Element position converter.
///
internal class ElementPositionConverter : ExpandableObjectConverter
{
#region Converter methods
///
/// Overrides the CanConvertFrom method of TypeConverter.
///
/// Descriptor context.
/// Convertion source type.
/// Indicates if convertion is possible.
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if(sourceType == typeof(string))
{
return true;
}
return base.CanConvertFrom(context, sourceType);
}
///
/// Overrides the CanConvertTo method of TypeConverter.
///
/// Descriptor context.
/// Destination type.
/// Indicates if convertion is possible.
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
{
return true;
}
return base.CanConvertTo(context, destinationType);
}
///
/// Overrides the ConvertTo method of TypeConverter.
///
/// Descriptor context.
/// Culture information.
/// Value to convert.
/// Convertion destination type.
/// Converted object.
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (destinationType == typeof(string))
{
return ((ElementPosition)value).ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}
///
/// Overrides the ConvertFrom method of TypeConverter.
/// Converts from string with comma separated values.
///
/// Descriptor context.
/// Culture information.
/// Value to convert from.
/// Indicates if convertion is possible.
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string posValue = value as string;
if(posValue != null)
{
if (String.Compare(posValue, Constants.AutoValue, StringComparison.OrdinalIgnoreCase) == 0)
{
return new ElementPosition();
}
else
{
string[] array = posValue.Split(',');
if(array.Length == 4)
{
return new ElementPosition(
float.Parse(array[0], System.Globalization.CultureInfo.CurrentCulture),
float.Parse(array[1], System.Globalization.CultureInfo.CurrentCulture),
float.Parse(array[2], System.Globalization.CultureInfo.CurrentCulture),
float.Parse(array[3], System.Globalization.CultureInfo.CurrentCulture));
}
else
{
throw(new ArgumentException( SR.ExceptionElementPositionConverter ));
}
}
}
return base.ConvertFrom(context, culture, value);
}
#endregion
}
}