~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/SearchAndReplace/Project/Gui/SearchNode.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
 
2
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
 
3
 
 
4
using System;
 
5
using System.Collections.Generic;
 
6
using System.ComponentModel;
 
7
using System.Windows.Controls;
 
8
using System.Windows.Media;
 
9
 
 
10
namespace SearchAndReplace
 
11
{
 
12
        abstract class SearchNode : INotifyPropertyChanged
 
13
        {
 
14
                bool isExpanded;
 
15
                
 
16
                public bool IsExpanded {
 
17
                        get { return isExpanded; }
 
18
                        set {
 
19
                                if (isExpanded != value) {
 
20
                                        isExpanded = value;
 
21
                                        OnPropertyChanged("IsExpanded");
 
22
                                }
 
23
                        }
 
24
                }
 
25
                
 
26
                IEnumerable<SearchNode> children;
 
27
                
 
28
                public IEnumerable<SearchNode> Children {
 
29
                        get { return children; }
 
30
                        set {
 
31
                                if (children != value) {
 
32
                                        children = value;
 
33
                                        OnPropertyChanged("Children");
 
34
                                }
 
35
                        }
 
36
                }
 
37
                
 
38
                // Text usually is a TextBlock object, so we don't want too many of them floating in memory
 
39
                // (remember, we have a SearchNode for each search result, even for those in the search history).
 
40
                // We don't want to create a new TextBlock for each property access, either; so we'll cache them
 
41
                // using a WeakReference.
 
42
                
 
43
                WeakReference cachedText;
 
44
                
 
45
                public object Text {
 
46
                        get {
 
47
                                object text;
 
48
                                if (cachedText != null) {
 
49
                                        text = cachedText.Target;
 
50
                                        if (text != null)
 
51
                                                return text;
 
52
                                }
 
53
                                text = CreateText() ?? string.Empty;
 
54
                                cachedText = new WeakReference(text);
 
55
                                return text;
 
56
                        }
 
57
                }
 
58
                
 
59
                protected abstract object CreateText();
 
60
                
 
61
                protected void InvalidateText()
 
62
                {
 
63
                        cachedText = null;
 
64
                        OnPropertyChanged("Text");
 
65
                }
 
66
                
 
67
                public event PropertyChangedEventHandler PropertyChanged;
 
68
                
 
69
                protected virtual void OnPropertyChanged(string propertyName)
 
70
                {
 
71
                        if (PropertyChanged != null) {
 
72
                                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
 
73
                        }
 
74
                }
 
75
                
 
76
                public virtual void ActivateItem()
 
77
                {
 
78
                }
 
79
        }
 
80
}