BooleanFormat.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using FastReport.Utils;
  5. namespace FastReport.Format
  6. {
  7. /// <summary>
  8. /// Defines how boolean values are formatted and displayed.
  9. /// </summary>
  10. public class BooleanFormat : FormatBase
  11. {
  12. #region Fields
  13. private string falseText;
  14. private string trueText;
  15. #endregion
  16. #region Properties
  17. /// <summary>
  18. /// Gets or sets a string that will be displayed if value is <b>false</b>.
  19. /// </summary>
  20. public string FalseText
  21. {
  22. get { return falseText; }
  23. set { falseText = value; }
  24. }
  25. /// <summary>
  26. /// Gets or sets a string that will be displayed if value is <b>true</b>.
  27. /// </summary>
  28. public string TrueText
  29. {
  30. get { return trueText; }
  31. set { trueText = value; }
  32. }
  33. #endregion
  34. #region Public Methods
  35. /// <inheritdoc/>
  36. public override FormatBase Clone()
  37. {
  38. BooleanFormat result = new BooleanFormat();
  39. result.FalseText = FalseText;
  40. result.TrueText = TrueText;
  41. return result;
  42. }
  43. /// <inheritdoc/>
  44. public override bool Equals(object obj)
  45. {
  46. BooleanFormat f = obj as BooleanFormat;
  47. return f != null && FalseText == f.FalseText && TrueText == f.TrueText;
  48. }
  49. /// <inheritdoc/>
  50. public override int GetHashCode()
  51. {
  52. return base.GetHashCode();
  53. }
  54. /// <inheritdoc/>
  55. public override string FormatValue(object value)
  56. {
  57. if (value is Variant)
  58. value = ((Variant)value).Value;
  59. if (value is bool && (bool)value == false)
  60. return FalseText;
  61. if (value is bool && (bool)value == true)
  62. return TrueText;
  63. return value.ToString();
  64. }
  65. internal override string GetSampleValue()
  66. {
  67. return FormatValue(false);
  68. }
  69. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  70. {
  71. base.Serialize(writer, prefix, format);
  72. BooleanFormat c = format as BooleanFormat;
  73. if (c == null || TrueText != c.TrueText)
  74. writer.WriteStr(prefix + "TrueText", TrueText);
  75. if (c == null || FalseText != c.FalseText)
  76. writer.WriteStr(prefix + "FalseText", FalseText);
  77. }
  78. #endregion
  79. /// <summary>
  80. /// Initializes a new instance of the <b>BooleanFormat</b> class with default settings.
  81. /// </summary>
  82. public BooleanFormat()
  83. {
  84. FalseText = "False";
  85. TrueText = "True";
  86. }
  87. }
  88. }