DynamicDocumentGrid.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Media;
  11. using InABox.Clients;
  12. using InABox.Core;
  13. using InABox.WPF;
  14. using Microsoft.Win32;
  15. using Microsoft.Xaml.Behaviors.Core;
  16. using Image = System.Windows.Controls.Image;
  17. using InABox.Wpf;
  18. namespace InABox.DynamicGrid
  19. {
  20. public class DocumentConverter : AbstractConverter<object, object>
  21. {
  22. public override object Convert(object value)
  23. {
  24. return value;
  25. }
  26. }
  27. public class TimeStampToBrushConverter : AbstractConverter<DateTime, System.Windows.Media.Brush?>
  28. {
  29. public System.Windows.Media.Brush? Empty { get; init; }
  30. public System.Windows.Media.Brush? Set { get; init; }
  31. public override System.Windows.Media.Brush? Convert(DateTime value)
  32. {
  33. return value.IsEmpty()
  34. ? Empty
  35. : Set;
  36. }
  37. }
  38. public class DynamicDocumentGrid<TDocument, TEntity, TEntityLink> : DynamicManyToManyGrid<TDocument, TEntity>
  39. where TEntity : Entity, IPersistent, IRemotable, new()
  40. where TDocument : Entity, IEntityDocument<TEntityLink>, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany<TEntity, Document>, new()
  41. where TEntityLink : EntityLink<TEntity>, new()
  42. {
  43. // private DynamicActionColumn supercedecolumn;
  44. //
  45. // public bool ShowSupercededColumn
  46. // {
  47. // get
  48. // {
  49. // return supercedecolumn.Position != DynamicActionColumnPosition.Hidden;
  50. // }
  51. // set
  52. // {
  53. // supercedecolumn.Position = value ? DynamicActionColumnPosition.End : DynamicActionColumnPosition.Hidden;
  54. // }
  55. // }
  56. public bool ShowSupercededColumn { get; set; }
  57. public bool SimpleTemplate { get; set; }
  58. private DynamicTemplateColumn _template;
  59. public DynamicDocumentGrid()
  60. {
  61. MultiSelect = false;
  62. HiddenColumns.Add(x => x.DocumentLink.ID);
  63. HiddenColumns.Add(x => x.Superceded);
  64. HiddenColumns.Add(x => x.DocumentLink.FileName);
  65. HiddenColumns.Add(x => x.Thumbnail);
  66. HiddenColumns.Add(x => x.Notes);
  67. //ActionColumns.Add(new DynamicImageColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
  68. //ActionColumns.Add(new DynamicImageColumn(DiskImage, SaveDocument) { Position = DynamicActionColumnPosition.Start });
  69. _template = new DynamicTemplateColumn(DocumentTemplate)
  70. {
  71. Position = DynamicActionColumnPosition.Start,
  72. Width = 0,
  73. HeaderText = "Attached Documents"
  74. };
  75. ActionColumns.Add(_template);
  76. //supercedecolumn = new DynamicImageColumn(SupercededImage, SupercedeDocument);
  77. //ActionColumns.Add(supercedecolumn);
  78. }
  79. protected override void DoDoubleClick(object sender)
  80. {
  81. var doc = SelectedRows.FirstOrDefault()?.ToObject<TDocument>();
  82. if (doc != null)
  83. {
  84. var editor = new DocumentEditor(new IEntityDocument[] { doc });
  85. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  86. editor.SaveAllowed = false;
  87. editor.ShowDialog();
  88. }
  89. }
  90. private FrameworkElement DocumentTemplate()
  91. {
  92. return SimpleTemplate
  93. ? CreateSimpleTemplate()
  94. : CreateDetailedTemplate();
  95. }
  96. private FrameworkElement CreateDetailedTemplate()
  97. {
  98. Grid grid = new Grid()
  99. {
  100. Height = 100,
  101. ContextMenu = CreateContextMenu(),
  102. RowDefinitions =
  103. {
  104. new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) },
  105. new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) },
  106. },
  107. ColumnDefinitions =
  108. {
  109. new ColumnDefinition() { Width = new GridLength(100) },
  110. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) },
  111. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) },
  112. }
  113. };
  114. // grid.SetBinding(
  115. // Grid.BackgroundProperty,
  116. // new Binding("Superceded")
  117. // {
  118. // Converter = new TimeStampToBrushConverter()
  119. // {
  120. // Empty = new SolidColorBrush(Colors.LightYellow),
  121. // Set = new SolidColorBrush(Colors.Silver)
  122. // }
  123. // }
  124. // );
  125. Image thumbnail = new Image()
  126. {
  127. Stretch = Stretch.Uniform,
  128. Margin = new Thickness(5, 2, 5, 2),
  129. };
  130. var ttImage = new Image();
  131. ttImage.SetBinding(Image.SourceProperty,
  132. new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  133. thumbnail.ToolTip = new ToolTip()
  134. {
  135. Content = ttImage
  136. };
  137. thumbnail.SetBinding(Image.SourceProperty,
  138. new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  139. thumbnail.SetValue(Grid.RowProperty, 0);
  140. thumbnail.SetValue(Grid.RowSpanProperty, 2);
  141. thumbnail.SetValue(Grid.ColumnProperty, 0);
  142. grid.Children.Add(thumbnail);
  143. var dock = new DockPanel();
  144. dock.SetValue(Grid.RowProperty, 0);
  145. dock.SetValue(Grid.ColumnProperty, 1);
  146. grid.Children.Add(dock);
  147. var superceded = new Label()
  148. {
  149. FontWeight = FontWeights.Bold,
  150. Content = "*** SUPERCEDED ***",
  151. Margin = new Thickness(0, 0, 5, 0)
  152. };
  153. superceded.SetBinding(Label.VisibilityProperty,
  154. new Binding("Superceded") { Converter = new DateTimeToVisibilityConverter() });
  155. superceded.SetValue(DockPanel.DockProperty, Dock.Left);
  156. dock.Children.Add(superceded);
  157. var filename = new Label()
  158. {
  159. FontWeight = FontWeights.Bold
  160. };
  161. filename.SetBinding(Label.ContentProperty, new Binding("DocumentLink_FileName"));
  162. filename.SetValue(DockPanel.DockProperty, Dock.Left);
  163. dock.Children.Add(filename);
  164. var buttons = new StackPanel()
  165. {
  166. Orientation = Orientation.Horizontal
  167. };
  168. buttons.SetValue(Grid.RowProperty, 0);
  169. buttons.SetValue(Grid.ColumnProperty, 2);
  170. grid.Children.Add(buttons);
  171. var view = new Button()
  172. {
  173. Content = new Image() { Source = Wpf.Resources.multi_image.AsBitmapImage() },
  174. BorderBrush = new SolidColorBrush(Colors.Transparent),
  175. Background = new SolidColorBrush(Colors.Transparent),
  176. Height = 32,
  177. Width = 32,
  178. ToolTip = "View Documents",
  179. Command = new ActionCommand(ViewDocuments)
  180. };
  181. buttons.Children.Add(view);
  182. var copy = new Button()
  183. {
  184. Content = new Image() { Source = Wpf.Resources.copy.AsBitmapImage() },
  185. BorderBrush = new SolidColorBrush(Colors.Transparent),
  186. Background = new SolidColorBrush(Colors.Transparent),
  187. Height = 32,
  188. Width = 32,
  189. ToolTip = "Copy to Clipboard",
  190. Command = new ActionCommand(CopyDocuments)
  191. };
  192. buttons.Children.Add(copy);
  193. var save = new Button()
  194. {
  195. Content = new Image() { Source = Wpf.Resources.download.AsBitmapImage() },
  196. BorderBrush = new SolidColorBrush(Colors.Transparent),
  197. Background = new SolidColorBrush(Colors.Transparent),
  198. Height = 32,
  199. Width = 32,
  200. ToolTip = "Save Documents",
  201. Command = new ActionCommand(SaveDocuments)
  202. };
  203. buttons.Children.Add(save);
  204. var print = new Button()
  205. {
  206. Content = new Image() { Source = Wpf.Resources.print.AsBitmapImage(), Margin = new Thickness(2) },
  207. BorderBrush = new SolidColorBrush(Colors.Transparent),
  208. Background = new SolidColorBrush(Colors.Transparent),
  209. Height = 32,
  210. Width = 32,
  211. ToolTip = "Print Documents",
  212. Command = new ActionCommand(PrintDocuments)
  213. };
  214. buttons.Children.Add(print);
  215. var notes = new Label()
  216. {
  217. };
  218. notes.SetBinding(Label.ContentProperty, new Binding("Notes"));
  219. notes.SetValue(Grid.RowProperty, 1);
  220. notes.SetValue(Grid.ColumnProperty, 1);
  221. notes.SetValue(Grid.ColumnSpanProperty, 2);
  222. grid.Children.Add(notes);
  223. return grid;
  224. }
  225. private ContextMenu CreateContextMenu()
  226. {
  227. var menu = new ContextMenu();
  228. menu.Items.Add(new MenuItem()
  229. {
  230. Header = "View Documents",
  231. Command = new ActionCommand(ViewDocuments)
  232. });
  233. menu.Items.Add(new MenuItem()
  234. {
  235. Header = "Copy To Clipboard",
  236. Command = new ActionCommand(CopyDocuments)
  237. });
  238. menu.Items.Add(new MenuItem()
  239. {
  240. Header = "Save Documents",
  241. Command = new ActionCommand(SaveDocuments)
  242. });
  243. return menu;
  244. }
  245. private FrameworkElement CreateSimpleTemplate()
  246. {
  247. Grid grid = new Grid()
  248. {
  249. Height = 150,
  250. ContextMenu = CreateContextMenu(),
  251. RowDefinitions =
  252. {
  253. new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) },
  254. new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) },
  255. },
  256. ColumnDefinitions =
  257. {
  258. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) },
  259. }
  260. };
  261. Image thumbnail = new Image()
  262. {
  263. Stretch = Stretch.Uniform,
  264. Margin = new Thickness(5),
  265. };
  266. thumbnail.SetBinding(Image.SourceProperty, new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  267. thumbnail.SetValue(Grid.RowProperty,0);
  268. grid.Children.Add(thumbnail);
  269. var filename = new Label()
  270. {
  271. HorizontalContentAlignment = HorizontalAlignment.Center,
  272. FontSize = 10
  273. };
  274. filename.SetBinding(Label.ContentProperty, new Binding("DocumentLink_FileName"));
  275. filename.SetValue(Grid.RowProperty,1);
  276. grid.Children.Add(filename);
  277. return grid;
  278. }
  279. private void GetDocuments(Action<Dictionary<string,byte[]>> action)
  280. {
  281. var ids = SelectedRows.Select(r => r.Get<IEntityDocument, Guid>(c => c.DocumentLink.ID)).ToArray();
  282. var files = Client.Query(
  283. new Filter<Document>(x => x.ID).InList(ids),
  284. new Columns<Document>(x => x.FileName).Add(x => x.Data)
  285. ).ToDictionary<Document, String, byte[]>(x => x.FileName, x => x.Data);
  286. action?.Invoke(files);
  287. }
  288. private static string SanitiseFileName(string filename)
  289. {
  290. var basefilename = Path.GetFileNameWithoutExtension(filename);
  291. var extension = Path.GetExtension(filename);
  292. return Path.ChangeExtension(string.Join("_", basefilename.Split(Path.GetInvalidFileNameChars())), extension);
  293. }
  294. private void ViewDocuments()
  295. {
  296. GetDocuments((files) =>
  297. {
  298. foreach (var file in files)
  299. {
  300. Task.Run(() =>
  301. {
  302. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  303. try
  304. {
  305. File.WriteAllBytes(tempfile, file.Value);
  306. }
  307. catch
  308. {
  309. // Outlook likes to keep files open apparently, which breaks this code.
  310. }
  311. var info = new System.Diagnostics.ProcessStartInfo(tempfile);
  312. info.UseShellExecute = true;
  313. info.Verb = "Open";
  314. Process.Start(info);
  315. });
  316. }
  317. });
  318. }
  319. private void CopyDocuments()
  320. {
  321. if (SelectedRows?.Any() != true)
  322. return;
  323. GetDocuments((files) =>
  324. {
  325. System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
  326. foreach(var file in files)
  327. {
  328. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  329. File.WriteAllBytes(tempfile, file.Value);
  330. FileCollection.Add(tempfile);
  331. }
  332. Clipboard.SetFileDropList(FileCollection);
  333. });
  334. }
  335. private void SaveDocuments()
  336. {
  337. if (SelectedRows?.Any() != true)
  338. return;
  339. using(var fbd = new System.Windows.Forms.FolderBrowserDialog())
  340. {
  341. var result = fbd.ShowDialog();
  342. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
  343. {
  344. var path = fbd.SelectedPath;
  345. GetDocuments(files =>
  346. {
  347. foreach (var file in files)
  348. File.WriteAllBytes(Path.Combine(path, SanitiseFileName(file.Key)), file.Value);
  349. });
  350. }
  351. }
  352. }
  353. private void PrintDocuments()
  354. {
  355. if (SelectedRows?.Any() != true)
  356. return;
  357. GetDocuments(files =>
  358. {
  359. Task.Run(() =>
  360. {
  361. foreach (var file in files)
  362. {
  363. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  364. File.WriteAllBytes(tempfile, file.Value);
  365. var info = new System.Diagnostics.ProcessStartInfo(tempfile);
  366. info.CreateNoWindow = true;
  367. info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  368. info.UseShellExecute = true;
  369. info.Verb = "print";
  370. Process.Start(info);
  371. }
  372. });
  373. });
  374. }
  375. protected override DynamicGridColumns LoadColumns()
  376. {
  377. return new DynamicGridColumns();
  378. }
  379. protected override void DoReconfigure(FluentList<DynamicGridOption> options)
  380. {
  381. base.DoReconfigure(options);
  382. options.Remove(DynamicGridOption.SelectColumns);
  383. options.Add(DynamicGridOption.DragTarget);
  384. }
  385. public override int Order()
  386. {
  387. return int.MaxValue;
  388. }
  389. protected override void HandleDragOver(object sender, DragEventArgs e)
  390. {
  391. if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
  392. {
  393. e.Effects = DragDropEffects.Copy;
  394. }
  395. else
  396. {
  397. e.Effects = DragDropEffects.None;
  398. }
  399. e.Handled = true;
  400. }
  401. protected override void HandleDragDrop(object sender, DragEventArgs e)
  402. {
  403. var result = DocumentUtils.HandleFileDrop(e);
  404. if(result is not null)
  405. {
  406. var docs = new List<Document>();
  407. foreach (var (filename, stream) in result)
  408. {
  409. var doc = new Document();
  410. doc.FileName = Path.GetFileName(filename).ToLower();
  411. if (stream is null)
  412. {
  413. doc.Data = File.ReadAllBytes(filename);
  414. doc.TimeStamp = new FileInfo(filename).LastWriteTime;
  415. }
  416. else
  417. {
  418. using var memoryStream = new MemoryStream();
  419. stream.CopyTo(memoryStream);
  420. doc.Data = memoryStream.ToArray();
  421. doc.TimeStamp = DateTime.Now;
  422. }
  423. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  424. docs.Add(doc);
  425. }
  426. AddDocuments(docs);
  427. }
  428. }
  429. protected override void OnDragEnd(Type entity, CoreTable table, DragEventArgs e)
  430. {
  431. if (entity == typeof(Document))
  432. {
  433. var refresh = false;
  434. var docIDS = table.Rows.Select(x => x.Get<Document, Guid>(x => x.ID)).ToArray();
  435. var columns = new Columns<Document>(x => x.ID);
  436. foreach (var column in VisibleColumns)
  437. {
  438. if (column.ColumnName.StartsWith("DocumentLink."))
  439. {
  440. columns.Add(string.Join('.', column.ColumnName.Split('.').Skip(1)));
  441. }
  442. }
  443. var docs = new Client<Document>()
  444. .Query(
  445. new Filter<Document>(x => x.ID).InList(docIDS),
  446. columns);
  447. foreach (var doc in docs.ToObjects<Document>())
  448. {
  449. var entityDocument = new TDocument();
  450. entityDocument.EntityLink.ID = Item.ID;
  451. entityDocument.DocumentLink.ID = doc.ID;
  452. entityDocument.DocumentLink.Synchronise(doc);
  453. SaveItem(entityDocument);
  454. refresh = true;
  455. }
  456. if (refresh)
  457. {
  458. DoChanged();
  459. Refresh(false, true);
  460. }
  461. }
  462. else
  463. {
  464. base.OnDragEnd(entity, table, e);
  465. }
  466. }
  467. private void AddDocuments(IList<Document> documents)
  468. {
  469. if (documents.Any())
  470. {
  471. new Client<Document>().Save(documents, "Initial Upload");
  472. foreach (var doc in documents)
  473. {
  474. var newitem = CreateItem();
  475. var prop = GetOtherLink(newitem);
  476. prop.ID = doc.ID;
  477. prop.Synchronise(doc);
  478. SaveItem(newitem);
  479. }
  480. DoChanged();
  481. Refresh(false, true);
  482. }
  483. }
  484. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  485. {
  486. var dlg = new OpenFileDialog();
  487. dlg.Multiselect = true;
  488. if (dlg.ShowDialog() == true)
  489. {
  490. using (new WaitCursor())
  491. {
  492. var docs = new List<Document>();
  493. foreach (var filename in dlg.FileNames)
  494. {
  495. // Create a Document
  496. var doc = new Document();
  497. doc.FileName = Path.GetFileName(filename).ToLower();
  498. doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
  499. doc.Data = File.ReadAllBytes(filename);
  500. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  501. docs.Add(doc);
  502. }
  503. AddDocuments(docs);
  504. }
  505. }
  506. }
  507. }
  508. }