DocumentEditorControl.cs 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Media;
  8. using InABox.Core;
  9. using Microsoft.Win32;
  10. namespace InABox.DynamicGrid
  11. {
  12. public enum DocumentAction
  13. {
  14. Replace,
  15. UseExisting,
  16. MakeCopy
  17. }
  18. public class DocumentEditorControl : DynamicEditorControl<Guid>
  19. {
  20. public delegate Document FindDocumentEvent(string FileName);
  21. public delegate Document? GetDocumentEvent(Guid id);
  22. public delegate void OnUpdateOtherEditorHandler(string columnname, object value);
  23. public delegate void SaveDocumentEvent(Document document);
  24. private Document _document = new();
  25. private TextBox Editor;
  26. public DocumentEditorControl()
  27. {
  28. OtherColumns = new Dictionary<string, string>();
  29. }
  30. public Dictionary<string, string> OtherColumns { get; }
  31. public string Filter { get; set; }
  32. public event GetDocumentEvent? OnGetDocument;
  33. public event FindDocumentEvent? OnFindDocument;
  34. public event SaveDocumentEvent? OnSaveDocument;
  35. public event OnUpdateOtherEditorHandler? OnUpdateOtherEditor;
  36. protected override FrameworkElement CreateEditor()
  37. {
  38. var Grid = new Grid
  39. {
  40. VerticalAlignment = VerticalAlignment.Stretch,
  41. HorizontalAlignment = HorizontalAlignment.Stretch
  42. };
  43. //Grid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(25, GridUnitType.Pixel) });
  44. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
  45. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  46. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  47. Grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(50, GridUnitType.Pixel) });
  48. Editor = new TextBox
  49. {
  50. VerticalAlignment = VerticalAlignment.Stretch,
  51. VerticalContentAlignment = VerticalAlignment.Center,
  52. HorizontalAlignment = HorizontalAlignment.Stretch,
  53. Margin = new Thickness(0, 0, 0, 0),
  54. IsEnabled = false
  55. };
  56. //Editor.LostFocus += (o, e) => CheckChanged();
  57. Editor.SetValue(Grid.ColumnProperty, 0);
  58. Editor.SetValue(Grid.RowProperty, 0);
  59. Grid.Children.Add(Editor);
  60. var Select = new Button
  61. {
  62. VerticalAlignment = VerticalAlignment.Stretch,
  63. VerticalContentAlignment = VerticalAlignment.Center,
  64. HorizontalAlignment = HorizontalAlignment.Stretch,
  65. Margin = new Thickness(5, 1, 0, 1),
  66. Content = "Select",
  67. Focusable = false
  68. };
  69. Select.SetValue(Grid.ColumnProperty, 1);
  70. Select.SetValue(Grid.RowProperty, 0);
  71. Select.Click += Select_Click;
  72. Grid.Children.Add(Select);
  73. var Clear = new Button
  74. {
  75. VerticalAlignment = VerticalAlignment.Stretch,
  76. VerticalContentAlignment = VerticalAlignment.Center,
  77. HorizontalAlignment = HorizontalAlignment.Stretch,
  78. Margin = new Thickness(5, 1, 0, 1),
  79. Content = "Clear",
  80. Focusable = false
  81. };
  82. Clear.SetValue(Grid.ColumnProperty, 2);
  83. Clear.SetValue(Grid.RowProperty, 0);
  84. Clear.Click += Clear_Click;
  85. Grid.Children.Add(Clear);
  86. var View = new Button
  87. {
  88. VerticalAlignment = VerticalAlignment.Stretch,
  89. VerticalContentAlignment = VerticalAlignment.Center,
  90. HorizontalAlignment = HorizontalAlignment.Stretch,
  91. Margin = new Thickness(5, 1, 0, 1),
  92. Content = "View",
  93. Focusable = false
  94. };
  95. View.SetValue(Grid.ColumnProperty, 3);
  96. View.SetValue(Grid.RowProperty, 0);
  97. View.Click += View_Click;
  98. Grid.Children.Add(View);
  99. return Grid;
  100. }
  101. private void Select_Click(object sender, RoutedEventArgs e)
  102. {
  103. var dlg = new OpenFileDialog();
  104. dlg.Filter = Filter;
  105. if (dlg.ShowDialog() == true)
  106. {
  107. var filename = Path.GetFileName(dlg.FileName).ToLower();
  108. var timestamp = new FileInfo(dlg.FileName).LastWriteTime;
  109. var data = File.ReadAllBytes(dlg.FileName);
  110. var crc = CoreUtils.CalculateCRC(data);
  111. var existing = OnFindDocument?.Invoke(filename);
  112. if (existing != null)
  113. {
  114. if ((existing.TimeStamp == DateTime.MinValue || existing.TimeStamp.ToString("yyyy-MM-ddThh:mm.ss.fff")
  115. .Equals(timestamp.ToString("yyyy-MM-ddThh:mm.ss.fff"))) && existing.CRC.Equals(crc))
  116. {
  117. if (existing.TimeStamp == DateTime.MinValue)
  118. {
  119. existing.TimeStamp = timestamp;
  120. OnSaveDocument?.Invoke(existing);
  121. }
  122. _document = existing;
  123. }
  124. else
  125. {
  126. var confirm = new DocumentConfirm
  127. {
  128. FileName = filename,
  129. LocalSize = data.Length,
  130. RemoteSize = existing.Data.Length,
  131. LocalTimeStamp = timestamp,
  132. RemoteTimeStamp = existing.TimeStamp
  133. };
  134. if (confirm.ShowDialog() == true)
  135. {
  136. if (confirm.Result == DocumentAction.Replace)
  137. {
  138. existing.Data = data;
  139. existing.TimeStamp = timestamp;
  140. existing.CRC = crc;
  141. OnSaveDocument?.Invoke(existing);
  142. _document = existing;
  143. }
  144. else if (confirm.Result == DocumentAction.UseExisting)
  145. {
  146. _document = existing;
  147. }
  148. else if (confirm.Result == DocumentAction.MakeCopy)
  149. {
  150. var basefilename = Path.GetFileNameWithoutExtension(filename);
  151. var ext = Path.GetExtension(filename);
  152. var i = 0;
  153. while (existing != null)
  154. {
  155. i++;
  156. filename = Path.ChangeExtension(string.Format("{0} ({1})", basefilename, i), ext);
  157. existing = OnFindDocument(filename);
  158. }
  159. var document = new Document
  160. {
  161. FileName = filename,
  162. Data = data,
  163. TimeStamp = timestamp,
  164. CRC = crc
  165. };
  166. OnSaveDocument?.Invoke(document);
  167. _document = document;
  168. }
  169. }
  170. }
  171. }
  172. else
  173. {
  174. _document = new Document
  175. {
  176. FileName = filename,
  177. Data = data,
  178. TimeStamp = timestamp,
  179. CRC = crc
  180. };
  181. OnSaveDocument?.Invoke(_document);
  182. }
  183. Editor.Text = _document.FileName;
  184. if (OtherColumns.ContainsKey("FileName"))
  185. OtherValues[OtherColumns["FileName"]] = _document.FileName;
  186. CheckChanged();
  187. }
  188. }
  189. private void View_Click(object sender, RoutedEventArgs e)
  190. {
  191. if (_document.ID == Guid.Empty)
  192. {
  193. MessageBox.Show("Please select a document first!");
  194. return;
  195. }
  196. var ext = Path.GetExtension(_document.FileName);
  197. var filename = Path.ChangeExtension(Path.GetTempFileName(), ext);
  198. File.WriteAllBytes(filename, _document.Data);
  199. var gsProcessInfo = new ProcessStartInfo();
  200. gsProcessInfo.Verb = "open";
  201. gsProcessInfo.WindowStyle = ProcessWindowStyle.Normal;
  202. gsProcessInfo.FileName = filename;
  203. gsProcessInfo.UseShellExecute = true;
  204. Process.Start(gsProcessInfo);
  205. }
  206. private void Clear_Click(object sender, RoutedEventArgs e)
  207. {
  208. _document = new Document();
  209. Editor.Text = "";
  210. CheckChanged();
  211. }
  212. public override int DesiredHeight()
  213. {
  214. return 25;
  215. }
  216. public override int DesiredWidth()
  217. {
  218. return int.MaxValue;
  219. }
  220. protected override Guid RetrieveValue()
  221. {
  222. return _document.ID;
  223. }
  224. protected override void UpdateValue(Guid value)
  225. {
  226. if (value != Guid.Empty)
  227. _document = OnGetDocument?.Invoke(value) ?? new Document();
  228. Editor.Text = _document.FileName;
  229. }
  230. public override void SetFocus()
  231. {
  232. Editor.Focus();
  233. }
  234. public override void SetColor(Color color)
  235. {
  236. Editor.Background = new SolidColorBrush(color);
  237. }
  238. }
  239. }