Mercurial > silverbladetech
comparison Chronosv2/source/DragAndDrop/DropHelper.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 |
comparison
equal
deleted
inserted
replaced
9:904a9faadf8b | 10:443821e55f06 |
---|---|
1 using System; | |
2 using System.Collections; | |
3 using System.Diagnostics; | |
4 using System.IO; | |
5 using System.Text; | |
6 using System.Windows; | |
7 using System.Windows.Controls; | |
8 using Chronos.Presentation.Core.VirtualDesktops; | |
9 using Chronos.Presentation.Core.Widgets; | |
10 using Chronos.Presentation.ViewModel; | |
11 using Chronos.Presentation.Windows.Controls; | |
12 using nRoute.Services; | |
13 using nRoute.SiteMaps; | |
14 | |
15 namespace Chronos.Presentation.DragAndDrop | |
16 { | |
17 public sealed class DropHelper | |
18 { | |
19 #region · Consts · | |
20 | |
21 const string UrlDataFormat = "text/x-moz-url"; | |
22 | |
23 #endregion | |
24 | |
25 #region · Fields · | |
26 | |
27 private UIElement dropTarget = null; | |
28 private string[] datatypes = { typeof(UIElement).ToString(), "Text" }; | |
29 private DragDropEffects allowedEffects; | |
30 | |
31 #endregion | |
32 | |
33 #region · Properties · | |
34 | |
35 public string[] AllowedDataTypes | |
36 { | |
37 get { return this.datatypes; } | |
38 set | |
39 { | |
40 this.datatypes = value; | |
41 | |
42 for (int x = 0; x < this.datatypes.Length; x++) | |
43 { | |
44 this.datatypes[x] = this.datatypes[x].ToLower(); | |
45 } | |
46 } | |
47 } | |
48 | |
49 public DragDropEffects AllowedEffects | |
50 { | |
51 get { return this.allowedEffects; } | |
52 set { this.allowedEffects = value; } | |
53 } | |
54 | |
55 #endregion | |
56 | |
57 #region · Constructors · | |
58 | |
59 public DropHelper(UIElement wrapper) | |
60 { | |
61 this.dropTarget = wrapper; | |
62 | |
63 this.dropTarget.AllowDrop = true; | |
64 this.dropTarget.DragOver += new DragEventHandler(DropTarget_DragOver); | |
65 this.dropTarget.Drop += new DragEventHandler(DropTarget_Drop); | |
66 } | |
67 | |
68 #endregion | |
69 | |
70 #region · DropTarget Event Handlers · | |
71 | |
72 private void DropTarget_Drop(object sender, DragEventArgs e) | |
73 { | |
74 IDataObject data = e.Data; | |
75 DragDataWrapper dw = null; | |
76 bool isDataOperation = false; | |
77 | |
78 Debug.Assert(data != null); | |
79 | |
80 if (data.GetDataPresent(typeof(DragDataWrapper).ToString())) | |
81 { | |
82 dw = data.GetData(typeof(DragDataWrapper).ToString()) as DragDataWrapper; | |
83 | |
84 Debug.Assert(dw.Shim != null); | |
85 | |
86 if ((dw.Shim.SupportedActions & DragDropProviderActions.Data) != 0) | |
87 { | |
88 isDataOperation = true; | |
89 } | |
90 } | |
91 | |
92 // Try a BRUTE FORCE APPROACH on UIElement just to show how it could be done | |
93 // BUT NOT ENDORSING IT!!! | |
94 if (!isDataOperation) | |
95 { | |
96 if (this.dropTarget is Canvas) | |
97 { | |
98 if (e.Data.GetDataPresent(DataFormats.FileDrop)) | |
99 { | |
100 this.DropFileLink(e); | |
101 } | |
102 else if (e.Data.GetDataPresent(UrlDataFormat)) | |
103 { | |
104 this.DropUrl(e); | |
105 } | |
106 } | |
107 } | |
108 else | |
109 { | |
110 Debug.Assert(dw != null); | |
111 Debug.Assert(dw.Shim != null); | |
112 | |
113 object rawdata = dw.Data; | |
114 | |
115 if (this.dropTarget is ItemsControl) | |
116 { | |
117 this.DropItemsControlHandler(e, rawdata); | |
118 } | |
119 else if (dropTarget is Canvas) | |
120 { | |
121 if (rawdata is IWidget) | |
122 { | |
123 this.DropCanvasWidget(e, rawdata as IWidget); | |
124 } | |
125 else if (rawdata is SiteMapNode) | |
126 { | |
127 this.DropCanvasInternalLink(e, rawdata as NavigationNode); | |
128 } | |
129 } | |
130 } | |
131 | |
132 e.Handled = true; | |
133 } | |
134 | |
135 private void DropTarget_DragOver(object sender, DragEventArgs e) | |
136 { | |
137 string[] types = e.Data.GetFormats(); | |
138 bool match = false; | |
139 | |
140 if (datatypes == null || types == null) | |
141 { | |
142 //TODO: ??? Should we set for DragDropEffects.None? | |
143 return; | |
144 } | |
145 | |
146 foreach (string s in types) | |
147 { | |
148 foreach (string type in datatypes) | |
149 { | |
150 match = (s.ToLower() == type); | |
151 | |
152 if (match) | |
153 { | |
154 break; | |
155 } | |
156 } | |
157 | |
158 if (match) | |
159 { | |
160 break; | |
161 } | |
162 } | |
163 | |
164 if (match) | |
165 { | |
166 e.Effects = AllowedEffects; | |
167 e.Handled = true; | |
168 } | |
169 } | |
170 | |
171 #endregion | |
172 | |
173 #region · Drop Handlers Methods · | |
174 | |
175 private void DropCanvasWidget(DragEventArgs e, IWidget dropObject) | |
176 { | |
177 ServiceLocator.GetService<IVirtualDesktopManager>() | |
178 .Show | |
179 ( | |
180 dropObject.CreateView() as WidgetElement, | |
181 e.GetPosition(this.dropTarget) | |
182 ); | |
183 } | |
184 | |
185 private void DropItemsControlHandler(DragEventArgs e, object rawdata) | |
186 { | |
187 ItemsControl ic = this.dropTarget as ItemsControl; | |
188 IList list = ic.ItemsSource as IList; | |
189 | |
190 if (list == null) | |
191 { | |
192 list = ic.Items as System.Collections.IList; | |
193 } | |
194 | |
195 if (list != null) | |
196 { | |
197 if (!list.Contains(rawdata)) | |
198 { | |
199 // Here we do not check for Move | Copy ... because this is a DATA operation .. No parent relationshop at all ... | |
200 list.Add(rawdata); | |
201 } | |
202 else | |
203 { | |
204 // nothing was done ... | |
205 e.Effects = DragDropEffects.None; | |
206 } | |
207 } | |
208 else | |
209 { | |
210 e.Effects = DragDropEffects.None; | |
211 } | |
212 } | |
213 | |
214 private void DropFileLink(DragEventArgs e) | |
215 { | |
216 // External link drop | |
217 Point position = e.GetPosition(this.dropTarget); | |
218 string[] filenames = e.Data.GetData(DataFormats.FileDrop, true) as string[]; | |
219 | |
220 e.Effects = DragDropEffects.Link; | |
221 | |
222 foreach (string filename in filenames) | |
223 { | |
224 ServiceLocator.GetService<IVirtualDesktopManager>() | |
225 .CreateShortcut<ExternalShortcutViewModel> | |
226 ( | |
227 Path.GetFileNameWithoutExtension(filename), filename, position | |
228 ); | |
229 } | |
230 } | |
231 | |
232 private void DropUrl(DragEventArgs e) | |
233 { | |
234 // External link drop | |
235 Point position = e.GetPosition(this.dropTarget); | |
236 String data = null; | |
237 | |
238 using (MemoryStream stream = e.Data.GetData(UrlDataFormat, true) as MemoryStream) | |
239 { | |
240 using (StreamReader reader = new StreamReader(stream, Encoding.Unicode)) | |
241 { | |
242 data = reader.ReadToEnd().Replace("\0", ""); | |
243 } | |
244 } | |
245 | |
246 if (!String.IsNullOrEmpty(data)) | |
247 { | |
248 string[] elements = data.Split('\n'); | |
249 | |
250 e.Effects = DragDropEffects.Link; | |
251 | |
252 if (!String.IsNullOrWhiteSpace(elements[0]) && | |
253 !String.IsNullOrWhiteSpace(elements[1])) | |
254 { | |
255 ServiceLocator.GetService<IVirtualDesktopManager>() | |
256 .CreateShortcut<ExternalShortcutViewModel> | |
257 ( | |
258 elements[1], | |
259 elements[0], | |
260 position | |
261 ); | |
262 } | |
263 } | |
264 } | |
265 | |
266 private void DropCanvasInternalLink(DragEventArgs e, NavigationNode siteMapNode) | |
267 { | |
268 Point position = e.GetPosition(this.dropTarget); | |
269 | |
270 ServiceLocator.GetService<IVirtualDesktopManager>() | |
271 .CreateShortcut<InternalShortcutViewModel> | |
272 ( | |
273 siteMapNode.Title, | |
274 siteMapNode.Url, | |
275 position | |
276 ); | |
277 } | |
278 | |
279 #endregion | |
280 | |
281 #region · Private Methods · | |
282 | |
283 private bool Unparent(DragDataWrapper dw, UIElement uie) | |
284 { | |
285 bool success = false; | |
286 | |
287 if (dw != null) | |
288 { | |
289 if (dw.AllowChildrenRemove) | |
290 { | |
291 dw.Shim.UnParent(); | |
292 } | |
293 } | |
294 | |
295 if (!success) // BRUTE FORCE | |
296 { | |
297 if (uie is FrameworkElement) | |
298 { | |
299 FrameworkElement fe = uie as FrameworkElement; | |
300 | |
301 if (fe.Parent != null) | |
302 { | |
303 if (fe.Parent is Panel) | |
304 { | |
305 try | |
306 { | |
307 ((Panel)(fe.Parent)).Children.Remove(uie); | |
308 success = true; | |
309 } | |
310 catch (Exception) | |
311 { | |
312 #if DEBUG | |
313 System.Diagnostics.Debug.Assert(false); | |
314 #endif | |
315 } | |
316 } | |
317 } | |
318 else if (fe.Parent is ContentControl) | |
319 { | |
320 ContentControl cc = fe.Parent as ContentControl; | |
321 | |
322 cc.Content = null; | |
323 success = true; | |
324 } | |
325 } | |
326 } | |
327 | |
328 return success; | |
329 } | |
330 | |
331 #endregion | |
332 } | |
333 } |