Statistics
| Branch: | Revision:

root / trunk / Pithos.Client.WPF / SelectiveSynch / DirectoryRecord.cs @ 422c9598

History | View | Annotate | Download (3.9 kB)

1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Collections.Specialized;
5
using System.IO;
6
using System.Linq;
7
using System.Text;
8
using Caliburn.Micro;
9

    
10
namespace Pithos.Client.WPF.SelectiveSynch
11
{
12
    public class DirectoryRecord : PropertyChangedBase,IEnumerable<DirectoryRecord>
13
    {
14
        public DirectoryInfo Info { get; set; }
15

    
16

    
17
        DirectoryRecord _parent;
18

    
19
        public bool Added { get; set; }
20
        public bool Removed { get; set; }
21

    
22
        private bool? _isChecked;
23
        #region IsChecked
24

    
25
        /// <summary>
26
        /// Gets/sets the state of the associated UI toggle (ex. CheckBox).
27
        /// The return value is calculated based on the check state of all
28
        /// child FooViewModels.  Setting this property to true or false
29
        /// will set all children to the same check state, and setting it 
30
        /// to any value will cause the parent to verify its check state.
31
        /// </summary>
32
        public bool? IsChecked
33
        {
34
            get { return _isChecked; }
35
            set { this.SetIsChecked(value, true, true); }
36
        }
37

    
38
        void SetIsChecked(bool? value, bool updateChildren, bool updateParent)
39
        {
40
            if (value == _isChecked)
41
                return;
42

    
43
            _isChecked = value;
44

    
45
            //If the value is null both Added and Removed should be False
46
            Added = _isChecked??false;
47
            Removed = !(_isChecked??true);
48

    
49
            if (updateChildren && _isChecked.HasValue)
50
                this.Directories.ForEach(c => c.SetIsChecked(_isChecked, true, false));
51

    
52
            if (updateParent && _parent != null)
53
                _parent.VerifyCheckState();
54

    
55
            this.RaisePropertyChangedEventImmediately("IsChecked");
56
        }
57

    
58
        void VerifyCheckState()
59
        {
60
            bool? state = null;
61
            for (int i = 0; i < this.Directories.Count; ++i)
62
            {
63
                bool? current = this.Directories[i].IsChecked;
64
                if (i == 0)
65
                {
66
                    state = current;
67
                }
68
                else if (state != current)
69
                {
70
                    state = null;
71
                    break;
72
                }
73
            }
74
            this.SetIsChecked(state, false, true);
75
        }
76

    
77
        #endregion // IsChecked
78

    
79

    
80
        public bool IsInitiallySelected { get; private set; }
81

    
82
        readonly Lazy<List<DirectoryRecord>> _directories = new Lazy<List<DirectoryRecord>>();
83

    
84
        public List<DirectoryRecord> Directories
85
        {
86
            get
87
            {
88
                return _directories.Value;
89
            }
90
        }
91

    
92
        public DirectoryRecord(string ignorePath)
93
        {
94
            _directories = new Lazy<List<DirectoryRecord>>(() => 
95
                (from directory in Info.EnumerateDirectories("*", SearchOption.TopDirectoryOnly)
96
                where !directory.FullName.StartsWith(ignorePath)
97
                select new DirectoryRecord(ignorePath) { Info = directory }).ToList());
98
        }
99

    
100

    
101

    
102
/*
103
        public IEnumerable<DirectoryInfo> GetCheckedDirectories()
104
        {
105
            var q = from record in this
106
                    where record.IsChecked==true
107
                    select record.Info;
108
            return q;
109
        }
110
*/
111

    
112
/*
113
        public void SetSelections(StringCollection selections)
114
        {
115
            IsChecked=selections.Contains(Info.FullName);                
116
            foreach (var children in Directories)
117
            {
118
                children.SetSelections(selections);
119
            }
120
        }
121
*/
122

    
123
        public IEnumerator<DirectoryRecord> GetEnumerator()
124
        {
125
            yield return this;
126
            foreach (var children in Directories)
127
                foreach (var info in children)
128
                {
129
                    yield return info;
130
                }
131
        }
132

    
133
        IEnumerator IEnumerable.GetEnumerator()
134
        {
135
            return GetEnumerator();
136
        }
137
    }
138
}