CellConverter.cs 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright © 2005, Mathew Hall
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * - Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. *
  11. * - Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  19. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  21. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  23. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  24. * OF SUCH DAMAGE.
  25. */
  26. using System;
  27. using System.ComponentModel;
  28. using System.ComponentModel.Design.Serialization;
  29. using System.Drawing;
  30. using System.Globalization;
  31. using System.Reflection;
  32. using XPTable.Models;
  33. namespace XPTable.Models.Design
  34. {
  35. /// <summary>
  36. /// A custom TypeConverter used to help convert Cells from
  37. /// one Type to another
  38. /// </summary>
  39. public class CellConverter : TypeConverter
  40. {
  41. /// <summary>
  42. /// Returns whether this converter can convert the object to the
  43. /// specified type, using the specified context
  44. /// </summary>
  45. /// <param name="context">An ITypeDescriptorContext that provides a
  46. /// format context</param>
  47. /// <param name="destinationType">A Type that represents the type
  48. /// you want to convert to</param>
  49. /// <returns>true if this converter can perform the conversion; o
  50. /// therwise, false</returns>
  51. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  52. {
  53. if (destinationType == typeof(InstanceDescriptor))
  54. {
  55. return true;
  56. }
  57. return base.CanConvertTo(context, destinationType);
  58. }
  59. /// <summary>
  60. /// Converts the given value object to the specified type, using
  61. /// the specified context and culture information
  62. /// </summary>
  63. /// <param name="context">An ITypeDescriptorContext that provides
  64. /// a format context</param>
  65. /// <param name="culture">A CultureInfo object. If a null reference
  66. /// is passed, the current culture is assumed</param>
  67. /// <param name="value">The Object to convert</param>
  68. /// <param name="destinationType">The Type to convert the value
  69. /// parameter to</param>
  70. /// <returns>An Object that represents the converted value</returns>
  71. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  72. {
  73. if (destinationType == typeof(InstanceDescriptor) && value is Cell)
  74. {
  75. ConstructorInfo ci = typeof(Cell).GetConstructor(new Type[] {});
  76. if (ci != null)
  77. {
  78. return new InstanceDescriptor(ci, null, false);
  79. }
  80. }
  81. return base.ConvertTo(context, culture, value, destinationType);
  82. }
  83. }
  84. }