LayoutSerializer.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************************
  2. Extended WPF Toolkit
  3. Copyright (C) 2007-2013 Xceed Software Inc.
  4. This program is provided to you under the terms of the Microsoft Public
  5. License (Ms-PL) as published at http://wpftoolkit.codeplex.com/license
  6. For more features, controls, and fast professional support,
  7. pick up the Plus Edition at http://xceed.com/wpf_toolkit
  8. Stay informed: follow @datagrid on Twitter or Like http://facebook.com/datagrids
  9. ***********************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. using System.IO;
  15. using Xceed.Wpf.AvalonDock.Controls;
  16. using System.Windows;
  17. namespace Xceed.Wpf.AvalonDock.Layout.Serialization
  18. {
  19. public abstract class LayoutSerializer
  20. {
  21. DockingManager _manager;
  22. public LayoutSerializer(DockingManager manager)
  23. {
  24. if (manager == null)
  25. throw new ArgumentNullException("manager");
  26. _manager = manager;
  27. _previousAnchorables = _manager.Layout.Descendents().OfType<LayoutAnchorable>().ToArray();
  28. _previousDocuments = _manager.Layout.Descendents().OfType<LayoutDocument>().ToArray();
  29. }
  30. LayoutAnchorable[] _previousAnchorables = null;
  31. LayoutDocument[] _previousDocuments = null;
  32. public DockingManager Manager
  33. {
  34. get { return _manager; }
  35. }
  36. public event EventHandler<LayoutSerializationCallbackEventArgs> LayoutSerializationCallback;
  37. protected virtual void FixupLayout(LayoutRoot layout)
  38. {
  39. //fix container panes
  40. foreach (var lcToAttach in layout.Descendents().OfType<ILayoutPreviousContainer>().Where(lc => lc.PreviousContainerId != null))
  41. {
  42. var paneContainerToAttach = layout.Descendents().OfType<ILayoutPaneSerializable>().FirstOrDefault(lps => lps.Id == lcToAttach.PreviousContainerId);
  43. if (paneContainerToAttach == null)
  44. throw new ArgumentException(string.Format("Unable to find a pane with id ='{0}'", lcToAttach.PreviousContainerId));
  45. lcToAttach.PreviousContainer = paneContainerToAttach as ILayoutContainer;
  46. }
  47. //now fix the content of the layoutcontents
  48. foreach (var lcToFix in layout.Descendents().OfType<LayoutAnchorable>().Where(lc => lc.Content == null).ToArray())
  49. {
  50. LayoutAnchorable previousAchorable = null;
  51. if (lcToFix.ContentId != null)
  52. {
  53. //try find the content in replaced layout
  54. previousAchorable = _previousAnchorables.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
  55. }
  56. if (LayoutSerializationCallback != null)
  57. {
  58. var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousAchorable != null ? previousAchorable.Content : null);
  59. LayoutSerializationCallback(this, args);
  60. if (args.Cancel)
  61. lcToFix.Close();
  62. else if (args.Content != null)
  63. lcToFix.Content = args.Content;
  64. else if (args.Model.Content != null)
  65. lcToFix.Hide(false);
  66. }
  67. else if (previousAchorable == null)
  68. lcToFix.Hide(false);
  69. else
  70. {
  71. lcToFix.Content = previousAchorable.Content;
  72. lcToFix.IconSource = previousAchorable.IconSource;
  73. }
  74. }
  75. foreach (var lcToFix in layout.Descendents().OfType<LayoutDocument>().Where(lc => lc.Content == null).ToArray())
  76. {
  77. LayoutDocument previousDocument = null;
  78. if (lcToFix.ContentId != null)
  79. {
  80. //try find the content in replaced layout
  81. previousDocument = _previousDocuments.FirstOrDefault(a => a.ContentId == lcToFix.ContentId);
  82. }
  83. if (LayoutSerializationCallback != null)
  84. {
  85. var args = new LayoutSerializationCallbackEventArgs(lcToFix, previousDocument != null ? previousDocument.Content : null);
  86. LayoutSerializationCallback(this, args);
  87. if (args.Cancel)
  88. lcToFix.Close();
  89. else if (args.Content != null)
  90. lcToFix.Content = args.Content;
  91. else if (args.Model.Content != null)
  92. lcToFix.Close();
  93. }
  94. else if (previousDocument == null)
  95. lcToFix.Close();
  96. else
  97. lcToFix.Content = previousDocument.Content;
  98. }
  99. layout.CollectGarbage();
  100. }
  101. protected void StartDeserialization()
  102. {
  103. Manager.SuspendDocumentsSourceBinding = true;
  104. Manager.SuspendAnchorablesSourceBinding = true;
  105. }
  106. protected void EndDeserialization()
  107. {
  108. Manager.SuspendDocumentsSourceBinding = false;
  109. Manager.SuspendAnchorablesSourceBinding = false;
  110. }
  111. }
  112. }