Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2695_dataset-ids/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Document/WeakLineTracker.cs

Last change on this file was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 3.7 KB
Line 
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
19using System;
20
21namespace ICSharpCode.AvalonEdit.Document
22{
23  /// <summary>
24  /// Allows registering a line tracker on a TextDocument using a weak reference from the document to the line tracker.
25  /// </summary>
26  public sealed class WeakLineTracker : ILineTracker
27  {
28    TextDocument textDocument;
29    WeakReference targetObject;
30   
31    private WeakLineTracker(TextDocument textDocument, ILineTracker targetTracker)
32    {
33      this.textDocument = textDocument;
34      this.targetObject = new WeakReference(targetTracker);
35    }
36   
37    /// <summary>
38    /// Registers the <paramref name="targetTracker"/> as line tracker for the <paramref name="textDocument"/>.
39    /// A weak reference to the target tracker will be used, and the WeakLineTracker will deregister itself
40    /// when the target tracker is garbage collected.
41    /// </summary>
42    public static WeakLineTracker Register(TextDocument textDocument, ILineTracker targetTracker)
43    {
44      if (textDocument == null)
45        throw new ArgumentNullException("textDocument");
46      if (targetTracker == null)
47        throw new ArgumentNullException("targetTracker");
48      WeakLineTracker wlt = new WeakLineTracker(textDocument, targetTracker);
49      textDocument.LineTrackers.Add(wlt);
50      return wlt;
51    }
52   
53    /// <summary>
54    /// Deregisters the weak line tracker.
55    /// </summary>
56    public void Deregister()
57    {
58      if (textDocument != null) {
59        textDocument.LineTrackers.Remove(this);
60        textDocument = null;
61      }
62    }
63   
64    void ILineTracker.BeforeRemoveLine(DocumentLine line)
65    {
66      ILineTracker targetTracker = targetObject.Target as ILineTracker;
67      if (targetTracker != null)
68        targetTracker.BeforeRemoveLine(line);
69      else
70        Deregister();
71    }
72   
73    void ILineTracker.SetLineLength(DocumentLine line, int newTotalLength)
74    {
75      ILineTracker targetTracker = targetObject.Target as ILineTracker;
76      if (targetTracker != null)
77        targetTracker.SetLineLength(line, newTotalLength);
78      else
79        Deregister();
80    }
81   
82    void ILineTracker.LineInserted(DocumentLine insertionPos, DocumentLine newLine)
83    {
84      ILineTracker targetTracker = targetObject.Target as ILineTracker;
85      if (targetTracker != null)
86        targetTracker.LineInserted(insertionPos, newLine);
87      else
88        Deregister();
89    }
90   
91    void ILineTracker.RebuildDocument()
92    {
93      ILineTracker targetTracker = targetObject.Target as ILineTracker;
94      if (targetTracker != null)
95        targetTracker.RebuildDocument();
96      else
97        Deregister();
98    }
99   
100    void ILineTracker.ChangeComplete(DocumentChangeEventArgs e)
101    {
102      ILineTracker targetTracker = targetObject.Target as ILineTracker;
103      if (targetTracker != null)
104        targetTracker.ChangeComplete(e);
105      else
106        Deregister();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.