JsonEditorControl.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using InABox.Core;
  10. namespace InABox.DynamicGrid
  11. {
  12. internal class DynamicJsonGrid<T> : DynamicGrid<T> where T : BaseObject, new()
  13. {
  14. private int rowindex = -1;
  15. protected override void Init()
  16. {
  17. }
  18. protected override void DoReconfigure(DynamicGridOptions options)
  19. {
  20. }
  21. public override void DeleteItems(params CoreRow[] rows)
  22. {
  23. var indexes = rows.Select(x => x.Index).OrderBy(x => x).ToArray();
  24. foreach (var index in indexes)
  25. Data.Rows.RemoveAt(index);
  26. }
  27. public override T LoadItem(CoreRow row)
  28. {
  29. rowindex = row.Index;
  30. return row.ToObject<T>();
  31. }
  32. protected override void Reload(
  33. Filters<T> criteria, Columns<T> columns, ref SortOrder<T>? sort,
  34. CancellationToken token, Action<CoreTable?, Exception?> action)
  35. {
  36. }
  37. public override void SaveItem(T item)
  38. {
  39. CoreRow row;
  40. if (rowindex == -1)
  41. {
  42. row = Data.NewRow();
  43. Data.Rows.Add(row);
  44. }
  45. else
  46. {
  47. row = Data.Rows[rowindex];
  48. }
  49. Data.FillRow(row, item);
  50. }
  51. }
  52. internal class JsonEditorControl : DynamicEditorControl<string, JsonEditor>
  53. {
  54. static JsonEditorControl()
  55. {
  56. //DynamicEditorControlFactory.Register<JsonEditorControl, JsonEditor>();
  57. }
  58. private Button _button;
  59. private string _value = "";
  60. public JsonEditorControl()
  61. {
  62. Width = 150;
  63. }
  64. public override int DesiredHeight()
  65. {
  66. return 25;
  67. }
  68. public override int DesiredWidth()
  69. {
  70. return 150;
  71. }
  72. public override void SetColor(Color color)
  73. {
  74. _button.Background = new SolidColorBrush(color);
  75. }
  76. public override void SetFocus()
  77. {
  78. // Not Sure what to do here?
  79. }
  80. public override void Configure()
  81. {
  82. }
  83. protected override FrameworkElement CreateEditor()
  84. {
  85. _button = new Button();
  86. _button.Content = "Edit";
  87. _button.Click += _button_Click;
  88. return _button;
  89. }
  90. private void _button_Click(object sender, RoutedEventArgs e)
  91. {
  92. var type = (EditorDefinition as JsonEditor).Type;
  93. var listtype = typeof(List<>).MakeGenericType(type);
  94. var list = Activator.CreateInstance(listtype) as IList;
  95. if (!string.IsNullOrWhiteSpace(_value))
  96. Serialization.DeserializeInto(_value, list);
  97. var array = new object[list.Count];
  98. list.CopyTo(array, 0);
  99. var data = new CoreTable();
  100. data.LoadColumns(type);
  101. data.LoadRows(array);
  102. /*var gridtype = typeof(DynamicJsonGrid<>).MakeGenericType(type);
  103. var grid = Activator.CreateInstance(gridtype) as IDynamicGrid;
  104. grid.Data = data;
  105. if (grid.DirectEdit(data))
  106. {
  107. var saved = data.Rows.Select(x => x.ToObject(type)).ToArray();
  108. _value = Serialization.Serialize(saved, true);
  109. CheckChanged();
  110. }*/
  111. }
  112. protected override string RetrieveValue()
  113. {
  114. return _value;
  115. }
  116. protected override void UpdateValue(string value)
  117. {
  118. _value = "";
  119. }
  120. }
  121. }