/Samples/DragDrop/DragDrop.Model/CookieJar.cs
C# | 138 lines | 90 code | 20 blank | 28 comment | 9 complexity | 3cd4d8f5445df25974ce90e492405eb3 MD5 | raw file
- 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));
- }
- }
- }