sharpfellows.toolkit /Samples/DragDrop/DragDrop.Model/CookieJar.cs

Language C# Lines 139
MD5 Hash 3cd4d8f5445df25974ce90e492405eb3 Estimated Cost $2,156 (why?)
Repository https://bitbucket.org/suhas_28/sharpfellows.toolkit View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using SharpFellows.Toolkit.Behaviours;

namespace DragDrop.Model
{
    /// <summary>
    /// Container for cookies 
    /// </summary>
    public class CookieJar : INotifyPropertyChanged
    {
        #region Constants
        private const int INITIAL_NUMBER_OF_COOKIES = 5;
        private const int JAR_CAPACITY = 10;
        #endregion

        #region Fields

        private readonly Stack<Cookie> _cookies = new Stack<Cookie>(JAR_CAPACITY);
        private IDragSource _source;
        private IDropTarget _target;

        #endregion

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Initializes a new instance of the <see cref="CookieJar"/> class.
        /// </summary>
        /// <param name="numberOfCookies">The number of cookies to be placed in the jar</param>
        public CookieJar(int numberOfCookies)
        {
            for (int i = 0; i < numberOfCookies; i++)
                _cookies.Push(new Cookie { Jar = this });            
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CookieJar"/> class.
        /// </summary>
        public CookieJar() : this(INITIAL_NUMBER_OF_COOKIES)
        {
        }

        /// <summary>
        /// Gets the (drag) source of cookies.
        /// </summary>
        /// <value>The source of cookies.</value>
        public IDragSource SourceOfCookies
        {
            get
            {
                if (_source == null)
                    _source = new DragSource<CookieJar>(GetDragEffects, GetData);

                return _source;
            }
        }

        /// <summary>
        /// Gets the drop target for cookies.
        /// </summary>
        /// <value>The cookie sink.</value>
        public IDropTarget CookieSink
        {
            get
            {
                if (_target == null)
                    _target = new DropTarget<Cookie>(GetDropEffects, Drop);

                return _target;
            }
        }

        /// <summary>
        /// Gets the number of cookies in the jar.
        /// </summary>
        /// <value>The number of cookies.</value>
        public int NumberOfCookies
        {
            get
            {
                return _cookies.Count;
            }
        }

        private DragDropEffects GetDragEffects(CookieJar jar)
        {
            // Only allow the drag and drop to start if we have any cookies
            return _cookies.Any() ? DragDropEffects.Move : DragDropEffects.None;
        }

        private object GetData(CookieJar jar)
        {
            return _cookies.Peek();
        }

        private void Drop(Cookie cookie)
        {
            AddCookie(cookie);
            RaisePropertyChangedEvent("NumberOfCookies");
        }

        private void AddCookie(Cookie cookie)
        {
            cookie.Jar.PopOne();
            _cookies.Push(cookie);
            cookie.Jar = this;
        }

        private void PopOne()
        {
            _cookies.Pop();
            RaisePropertyChangedEvent("NumberOfCookies");
        }

        private DragDropEffects GetDropEffects(Cookie cookie)
        {
            // Do not allow our own cookies
            if (cookie.Jar == this)
                return DragDropEffects.None;
            // The cookie can be dropped if we are not exceeding the capacity of the jar
            return _cookies.Count >= JAR_CAPACITY ? DragDropEffects.None : DragDropEffects.Move;
        }

        public void RaisePropertyChangedEvent(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
Back to Top