DocumentEditorControl.cs 11 KB

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