comparison Chronosv2/source/DragAndDrop/DragDropManager.cs @ 10:443821e55f06

Initial cleaned up add from Codeplex files
author stevenh7776 stevenhollidge@hotmail.com
date Tue, 21 Feb 2012 17:25:44 +0700
parents
children 09d18d6e5f40
comparison
equal deleted inserted replaced
9:904a9faadf8b 10:443821e55f06
1 /*
2 The MIT License
3
4 Copyright (c) 2009-2010 Carlos Guzmán Álvarez
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24
25 using System.Collections.Generic;
26 using System.Windows;
27 using System.Windows.Controls;
28
29 namespace Chronos.Presentation.DragAndDrop
30 {
31 public sealed class DragDropManager
32 {
33 #region · Singleton Instance ·
34
35 public static readonly DragDropManager Instance = new DragDropManager();
36
37 #endregion
38
39 #region · Attached Properties ·
40
41 /// <summary>
42 /// Identifies the IsDesktopCanvas dependency property.
43 /// </summary>
44 public static readonly DependencyProperty IsDropTargetProperty =
45 DependencyProperty.RegisterAttached("IsDropTarget", typeof(bool), typeof(DragDropManager),
46 new FrameworkPropertyMetadata(false,
47 new PropertyChangedCallback(OnIsDropTarget)));
48
49 /// <summary>
50 /// Identifies the IsDragSource dependency property.
51 /// </summary>
52 public static readonly DependencyProperty IsDragSourceProperty =
53 DependencyProperty.RegisterAttached("IsDragSource", typeof(bool), typeof(DragDropManager),
54 new FrameworkPropertyMetadata(false,
55 new PropertyChangedCallback(OnIsDragSource)));
56
57 #endregion
58
59 #region · Dependency Property Get/Set Methods ·
60
61 /// <summary>
62 /// Gets the value of the IsDropTarget attached property
63 /// </summary>
64 /// <param name="d"></param>
65 /// <returns></returns>
66 public static bool GetIsDropTarget(DependencyObject d)
67 {
68 return (bool)d.GetValue(IsDropTargetProperty);
69 }
70
71 /// <summary>
72 /// Sets the value of the IsDropTarget attached property
73 /// </summary>
74 /// <param name="d"></param>
75 /// <param name="value"></param>
76 public static void SetIsDropTarget(DependencyObject d, bool value)
77 {
78 d.SetValue(IsDropTargetProperty, value);
79 }
80
81 /// <summary>
82 /// Gets the value of the IsDragSource attached property
83 /// </summary>
84 /// <param name="d"></param>
85 /// <returns></returns>
86 public static bool GetIsDragSource(DependencyObject d)
87 {
88 return (bool)d.GetValue(IsDragSourceProperty);
89 }
90
91 /// <summary>
92 /// Sets the value of the IsDragSource attached property
93 /// </summary>
94 /// <param name="d"></param>
95 /// <param name="value"></param>
96 public static void SetIsDragSource(DependencyObject d, bool value)
97 {
98 d.SetValue(IsDragSourceProperty, value);
99 }
100
101 #endregion
102
103 #region · Dependency Property Callbacks ·
104
105 private static void OnIsDropTarget(DependencyObject d, DependencyPropertyChangedEventArgs e)
106 {
107 FrameworkElement element = d as FrameworkElement;
108
109 if (element != null)
110 {
111 DragDropManager.Instance.AttachDropTarget(element);
112 }
113 }
114
115 private static void OnIsDragSource(DependencyObject d, DependencyPropertyChangedEventArgs e)
116 {
117 FrameworkElement element = d as FrameworkElement;
118
119 if (element != null)
120 {
121 DragDropManager.Instance.AttachDragSource(element);
122 }
123 }
124
125 #endregion
126
127 #region · Fields ·
128
129 private Dictionary<FrameworkElement, DropHelper> dropTargets;
130 private Dictionary<FrameworkElement, DragHelper> dragSources;
131
132 #endregion
133
134 #region · Constructors ·
135
136 private DragDropManager()
137 {
138 this.dropTargets = new Dictionary<FrameworkElement, DropHelper>();
139 this.dragSources = new Dictionary<FrameworkElement, DragHelper>();
140 }
141
142 #endregion
143
144 #region · Private Methods ·
145
146 private void AttachDropTarget(FrameworkElement element)
147 {
148 if (!this.dropTargets.ContainsKey(element))
149 {
150 element.Unloaded += new RoutedEventHandler(DragDropManager_Unloaded);
151 this.dropTargets.Add(element, new DropHelper(element));
152 }
153 }
154
155 private void AttachDragSource(FrameworkElement element)
156 {
157 if (!this.dragSources.ContainsKey(element))
158 {
159 element.Unloaded += new RoutedEventHandler(DragDropManager_Unloaded);
160
161 if (element is ListBox)
162 {
163 this.dragSources.Add(element, new DragHelper(element, new ListBoxDragDropDataProvider(element as ListBox), null));
164 }
165 else if (element is TreeView)
166 {
167 this.dragSources.Add(element, new DragHelper(element, new TreeViewDragDropDataProvider(element as TreeView), null));
168 }
169 }
170 }
171
172 #endregion
173
174 #region · Event Handlers ·
175
176 private void DragDropManager_Unloaded(object sender, RoutedEventArgs e)
177 {
178 (sender as FrameworkElement).Unloaded -= new RoutedEventHandler(DragDropManager_Unloaded);
179
180 if (this.dragSources.ContainsKey(sender as FrameworkElement))
181 {
182 this.dragSources.Remove(sender as FrameworkElement);
183 }
184 else if (this.dropTargets.ContainsKey(sender as FrameworkElement))
185 {
186 this.dropTargets.Remove(sender as FrameworkElement);
187 }
188 }
189
190 #endregion
191 }
192 }