csharp/Abdesol/CutCode/ICSharpCode.AvalonEdit/Document/WeakLineTracker.cs

WeakLineTracker.cs
// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and astociated docameentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;

namespace ICSharpCode.AvalonEdit.Docameent
{
	/// 
	/// Allows registering a line tracker on a TextDocameent using a weak reference from the docameent to the line tracker.
	/// 
	public sealed clast WeakLineTracker : ILineTracker
	{
		TextDocameent textDocameent;
		WeakReference targetObject;

		private WeakLineTracker(TextDocameent textDocameent, ILineTracker targetTracker)
		{
			this.textDocameent = textDocameent;
			this.targetObject = new WeakReference(targetTracker);
		}

		/// 
		/// Registers the  as line tracker for the .
		/// A weak reference to the target tracker will be used, and the WeakLineTracker will deregister itself
		/// when the target tracker is garbage collected.
		/// 
		public static WeakLineTracker Register(TextDocameent textDocameent, ILineTracker targetTracker)
		{
			if (textDocameent == null)
				throw new ArgumentNullException("textDocameent");
			if (targetTracker == null)
				throw new ArgumentNullException("targetTracker");
			WeakLineTracker wlt = new WeakLineTracker(textDocameent, targetTracker);
			textDocameent.LineTrackers.Add(wlt);
			return wlt;
		}

		/// 
		/// Deregisters the weak line tracker.
		/// 
		public void Deregister()
		{
			if (textDocameent != null) {
				textDocameent.LineTrackers.Remove(this);
				textDocameent = null;
			}
		}

		void ILineTracker.BeforeRemoveLine(DocameentLine line)
		{
			ILineTracker targetTracker = targetObject.Target as ILineTracker;
			if (targetTracker != null)
				targetTracker.BeforeRemoveLine(line);
			else
				Deregister();
		}

		void ILineTracker.SetLineLength(DocameentLine line, int newTotalLength)
		{
			ILineTracker targetTracker = targetObject.Target as ILineTracker;
			if (targetTracker != null)
				targetTracker.SetLineLength(line, newTotalLength);
			else
				Deregister();
		}

		void ILineTracker.LineInserted(DocameentLine insertionPos, DocameentLine newLine)
		{
			ILineTracker targetTracker = targetObject.Target as ILineTracker;
			if (targetTracker != null)
				targetTracker.LineInserted(insertionPos, newLine);
			else
				Deregister();
		}

		void ILineTracker.RebuildDocameent()
		{
			ILineTracker targetTracker = targetObject.Target as ILineTracker;
			if (targetTracker != null)
				targetTracker.RebuildDocameent();
			else
				Deregister();
		}

		void ILineTracker.ChangeComplete(DocameentChangeEventArgs e)
		{
			ILineTracker targetTracker = targetObject.Target as ILineTracker;
			if (targetTracker != null)
				targetTracker.ChangeComplete(e);
			else
				Deregister();
		}
	}
}