/AvalonEdit/ICSharpCode.AvalonEdit/Document/WeakLineTracker.cs

http://github.com/icsharpcode/ILSpy · C# · 109 lines · 71 code · 10 blank · 28 comment · 16 complexity · f15fb85ce3439080a3f0d9e31052eda5 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. namespace ICSharpCode.AvalonEdit.Document
  20. {
  21. /// <summary>
  22. /// Allows registering a line tracker on a TextDocument using a weak reference from the document to the line tracker.
  23. /// </summary>
  24. public sealed class WeakLineTracker : ILineTracker
  25. {
  26. TextDocument textDocument;
  27. WeakReference targetObject;
  28. private WeakLineTracker(TextDocument textDocument, ILineTracker targetTracker)
  29. {
  30. this.textDocument = textDocument;
  31. this.targetObject = new WeakReference(targetTracker);
  32. }
  33. /// <summary>
  34. /// Registers the <paramref name="targetTracker"/> as line tracker for the <paramref name="textDocument"/>.
  35. /// A weak reference to the target tracker will be used, and the WeakLineTracker will deregister itself
  36. /// when the target tracker is garbage collected.
  37. /// </summary>
  38. public static WeakLineTracker Register(TextDocument textDocument, ILineTracker targetTracker)
  39. {
  40. if (textDocument == null)
  41. throw new ArgumentNullException("textDocument");
  42. if (targetTracker == null)
  43. throw new ArgumentNullException("targetTracker");
  44. WeakLineTracker wlt = new WeakLineTracker(textDocument, targetTracker);
  45. textDocument.LineTrackers.Add(wlt);
  46. return wlt;
  47. }
  48. /// <summary>
  49. /// Deregisters the weak line tracker.
  50. /// </summary>
  51. public void Deregister()
  52. {
  53. if (textDocument != null) {
  54. textDocument.LineTrackers.Remove(this);
  55. textDocument = null;
  56. }
  57. }
  58. void ILineTracker.BeforeRemoveLine(DocumentLine line)
  59. {
  60. ILineTracker targetTracker = targetObject.Target as ILineTracker;
  61. if (targetTracker != null)
  62. targetTracker.BeforeRemoveLine(line);
  63. else
  64. Deregister();
  65. }
  66. void ILineTracker.SetLineLength(DocumentLine line, int newTotalLength)
  67. {
  68. ILineTracker targetTracker = targetObject.Target as ILineTracker;
  69. if (targetTracker != null)
  70. targetTracker.SetLineLength(line, newTotalLength);
  71. else
  72. Deregister();
  73. }
  74. void ILineTracker.LineInserted(DocumentLine insertionPos, DocumentLine newLine)
  75. {
  76. ILineTracker targetTracker = targetObject.Target as ILineTracker;
  77. if (targetTracker != null)
  78. targetTracker.LineInserted(insertionPos, newLine);
  79. else
  80. Deregister();
  81. }
  82. void ILineTracker.RebuildDocument()
  83. {
  84. ILineTracker targetTracker = targetObject.Target as ILineTracker;
  85. if (targetTracker != null)
  86. targetTracker.RebuildDocument();
  87. else
  88. Deregister();
  89. }
  90. void ILineTracker.ChangeComplete(DocumentChangeEventArgs e)
  91. {
  92. ILineTracker targetTracker = targetObject.Target as ILineTracker;
  93. if (targetTracker != null)
  94. targetTracker.ChangeComplete(e);
  95. else
  96. Deregister();
  97. }
  98. }
  99. }