From 941739469ebd032a1025e549a1ab8d8174190891 Mon Sep 17 00:00:00 2001 From: Daniel Grunwald Date: Fri, 18 Mar 2011 17:18:31 +0100 Subject: [PATCH] Implement INotifyPropertyChanged in TextDocument. --- .../Document/TextDocument.cs | 25 ++++++++++++++++--- .../Document/TextDocumentWeakEventManager.cs | 2 ++ .../Editing/LineNumberMargin.cs | 7 +++--- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs index 86dc81848d..23ed66e594 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocument.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Globalization; @@ -21,7 +22,7 @@ namespace ICSharpCode.AvalonEdit.Document /// /// However, there is a single method that is thread-safe: (and its overloads). /// - public sealed class TextDocument : ITextSource + public sealed class TextDocument : ITextSource, INotifyPropertyChanged { #region Thread ownership readonly object lockObject = new object(); @@ -207,8 +208,16 @@ namespace ICSharpCode.AvalonEdit.Document /// Is raised when the TextLength property changes. /// /// + [Obsolete("This event will be removed in a future version; use the PropertyChanged event instead")] public event EventHandler TextLengthChanged; + /// + /// Is raised when one of the properties , , , + /// changes. + /// + /// + public event PropertyChangedEventHandler PropertyChanged; + /// /// Is raised before the document changes. /// @@ -231,8 +240,7 @@ namespace ICSharpCode.AvalonEdit.Document /// EndUpdate() /// /// event is raised - /// event is raised - /// event is raised + /// event is raised (for the Text, TextLength, LineCount properties, in that order) /// End of change group (on undo stack) /// event is raised /// @@ -418,21 +426,30 @@ namespace ICSharpCode.AvalonEdit.Document fireTextChanged = false; if (TextChanged != null) TextChanged(this, EventArgs.Empty); + OnPropertyChanged("Text"); int textLength = rope.Length; if (textLength != oldTextLength) { oldTextLength = textLength; if (TextLengthChanged != null) TextLengthChanged(this, EventArgs.Empty); + OnPropertyChanged("TextLength"); } int lineCount = lineTree.LineCount; if (lineCount != oldLineCount) { oldLineCount = lineCount; if (LineCountChanged != null) LineCountChanged(this, EventArgs.Empty); + OnPropertyChanged("LineCount"); } } } + + void OnPropertyChanged(string propertyName) + { + if (PropertyChanged != null) + PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); + } #endregion #region Insert / Remove / Replace @@ -743,6 +760,7 @@ namespace ICSharpCode.AvalonEdit.Document undoStack.ClearAll(); // first clear old undo stack, so that it can't be used to perform unexpected changes on this document // ClearAll() will also throw an exception when it's not safe to replace the undo stack (e.g. update is currently in progress) undoStack = value; + OnPropertyChanged("UndoStack"); } } } @@ -775,6 +793,7 @@ namespace ICSharpCode.AvalonEdit.Document /// /// Is raised when the LineCount property changes. /// + [Obsolete("This event will be removed in a future version; use the PropertyChanged event instead")] public event EventHandler LineCountChanged; #endregion diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs index 9c118cd00c..ba71084cbc 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Document/TextDocumentWeakEventManager.cs @@ -91,6 +91,7 @@ namespace ICSharpCode.AvalonEdit.Document /// Weak event manager for the event. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] + [Obsolete("The TextDocument.LineCountChanged event will be removed in a future version. Use PropertyChangedEventManager instead.")] public sealed class LineCountChanged : WeakEventManagerBase { /// @@ -110,6 +111,7 @@ namespace ICSharpCode.AvalonEdit.Document /// Weak event manager for the event. /// [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")] + [Obsolete("The TextDocument.TextLengthChanged event will be removed in a future version. Use PropertyChangedEventManager instead.")] public sealed class TextLengthChanged : WeakEventManagerBase { /// diff --git a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs index 59ca29864b..6153757973 100644 --- a/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs +++ b/src/Libraries/AvalonEdit/ICSharpCode.AvalonEdit/Editing/LineNumberMargin.cs @@ -2,6 +2,7 @@ // This code is distributed under the GNU LGPL (for details please see \doc\license.txt) using System; +using System.ComponentModel; using System.Globalization; using System.Windows; using System.Windows.Controls; @@ -89,11 +90,11 @@ namespace ICSharpCode.AvalonEdit.Editing protected override void OnDocumentChanged(TextDocument oldDocument, TextDocument newDocument) { if (oldDocument != null) { - TextDocumentWeakEventManager.LineCountChanged.RemoveListener(oldDocument, this); + PropertyChangedEventManager.RemoveListener(oldDocument, this, "LineCount"); } base.OnDocumentChanged(oldDocument, newDocument); if (newDocument != null) { - TextDocumentWeakEventManager.LineCountChanged.AddListener(newDocument, this); + PropertyChangedEventManager.AddListener(oldDocument, this, "LineCount"); } OnDocumentLineCountChanged(); } @@ -101,7 +102,7 @@ namespace ICSharpCode.AvalonEdit.Editing /// protected virtual bool ReceiveWeakEvent(Type managerType, object sender, EventArgs e) { - if (managerType == typeof(TextDocumentWeakEventManager.LineCountChanged)) { + if (managerType == typeof(PropertyChangedEventManager)) { OnDocumentLineCountChanged(); return true; }