PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/Scripts/Gumps/AProperties/PropertyChoice.cs

https://bitbucket.org/Kel/crepuscule
C# | 120 lines | 92 code | 19 blank | 9 comment | 23 complexity | 0f26aec4b3e9f1acdc130b65d031681b MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using Server.Scripts.Commands;
  7. namespace Server
  8. {
  9. public interface IWeakChoice
  10. {
  11. void SetValueByKey(object Object, object KeyPropertyValue);
  12. void SetValueDirectly(object Object, object Value);
  13. IEnumerable<Pair<object, string>> GetChoiceList(object Object);
  14. }
  15. public class WeakChoice<T> : IWeakChoice where T :class
  16. {
  17. // List to chose from, Title to show in the gump and Key to link with Value property
  18. public string ObjectKeyProperty { get; set; }
  19. public string ListProperty { get; set; }
  20. public string ListKeyProperty { get; set; }
  21. private T fValue;
  22. public T Value { get; private set; }
  23. public bool HasValue { get { return Value != default(T); } }
  24. public WeakChoice(string ObjectKeyProperty, string ListProperty, string ListKeyProperty)
  25. {
  26. this.ListProperty = ListProperty;
  27. this.ListKeyProperty = ListKeyProperty;
  28. this.ObjectKeyProperty = ObjectKeyProperty;
  29. }
  30. /// <summary>
  31. /// This one sets directly the value
  32. /// </summary>
  33. public void SetValueDirectly(object Object, object value)
  34. {
  35. Value = value as T;
  36. // Set the linked key
  37. if (ObjectKeyProperty != null && ListKeyProperty != null)
  38. {
  39. var KeyPropertyValue = Properties.GetPropertyValue(Value, ListKeyProperty);
  40. Properties.SetPropertyValue(Object, ObjectKeyProperty, KeyPropertyValue);
  41. }
  42. }
  43. /// <summary>
  44. /// Use this for setting by an internal (indirect) key, as internal name or something
  45. /// </summary>
  46. public void SetValueByKey(object Object, object KeyPropertyValue)
  47. {
  48. if (Object == null || ListProperty == null || ListKeyProperty == null)
  49. {
  50. //WorldContext.BroadcastMessage(AccessLevel.Counselor, 7, "Some parameters are wrong!");
  51. }
  52. else
  53. {
  54. if (KeyPropertyValue == null)
  55. {
  56. Value = default(T);
  57. return;
  58. }
  59. var list = Properties.GetPropertyValue(Object, ListProperty) as IList;
  60. if (list != null)
  61. {
  62. foreach (var item in list)
  63. {
  64. var itemValue = Properties.GetPropertyValue(item, ListKeyProperty);
  65. if (itemValue != null && itemValue.Equals(KeyPropertyValue))
  66. {
  67. Value = item as T;
  68. break;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. public IEnumerable<Pair<object, string>> GetChoiceList(object Object)
  75. {
  76. var result = new List<Pair<object, string>>();
  77. var list = Properties.GetPropertyValue(Object, ListProperty) as IEnumerable;
  78. if (list != null)
  79. {
  80. foreach (var item in list)
  81. {
  82. if (item == null) continue;
  83. var value = new Pair<object, string>();
  84. value.First = item;
  85. value.Second = item.ToString();
  86. result.Add(value);
  87. }
  88. }
  89. return result;
  90. }
  91. public override string ToString()
  92. {
  93. if (!HasValue)
  94. return "(Non-Défini)";
  95. else
  96. {
  97. return Value.ToString();
  98. }
  99. }
  100. }
  101. }