2
// TransferDataSource.cs
5
// Lluis Sanchez <lluis@xamarin.com>
7
// Copyright (c) 2012 Xamarin Inc
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28
using System.Collections.Generic;
31
using System.Runtime.Serialization.Formatters.Binary;
39
/// A collection of data to be transferred through drag and drop or the clipboard
41
public sealed class TransferDataSource
43
Dictionary<TransferDataType,object> data = new Dictionary<TransferDataType,object> ();
46
/// Gets or sets the data request callback.
49
/// The data request callback.
52
/// This callback can be used in combination with the AddType method to
53
/// generate the data on demand. In some scenarios, the drop/paste
54
/// side of a drag&drop or clipboard operation can decide if a drop/paste
55
/// is allowed or not by checking the available data type in this
56
/// data source. Once the operation is accepted, the DataRequestCallback
57
/// callback will be invoked to get the data for the type.
59
public DataRequestDelegate DataRequestCallback { get; set; }
62
/// Adds a value to the data source
64
/// <param name='value'>
67
public void AddValue<T> (T value) where T : class
70
throw new ArgumentNullException ("value");
71
data [TransferDataType.FromType (typeof (T))] = value;
75
/// Registers that the data store contains data of the provided type
77
/// <param name='type'>
78
/// The transfer data type
81
/// This method can be used in combination with DataRequestCallback to
82
/// generate the data on demand. In some scenarios, the drop/paste
83
/// side of a drag&drop or clipboard operation can decide if a drop/paste
84
/// is allowed or not by checking the available data type in this
85
/// data source. Once the operation is accepted, the DataRequestCallback
86
/// callback will be invoked to get the data for the type.
88
public void AddType (TransferDataType type)
94
/// Registers that the data store contains data of the provided type
96
/// <param name='type'>
100
/// This method can be used in combination with DataRequestCallback to
101
/// generate the data on demand. In some scenarios, the drop/paste
102
/// side of a drag&drop or clipboard operation can decide if a drop/paste
103
/// is allowed or not by checking the available data type in this
104
/// data source. Once the operation is accepted, the DataRequestCallback
105
/// callback will be invoked to get the data for the type.
107
public void AddType (Type type)
109
data [TransferDataType.FromType (type)] = null;
113
/// Gets the types included in this data source
115
public TransferDataType[] DataTypes {
117
return data.Keys.ToArray ();
122
/// Gets the value for a specific type
125
/// The value, or null if there is not value for this type
127
/// <param name='type'>
130
public object GetValue (TransferDataType type)
133
if (data.TryGetValue (type, out val)) {
136
if (DataRequestCallback != null)
137
return DataRequestCallback (type);
142
public static byte[] SerializeValue (object val)
144
using (MemoryStream ms = new MemoryStream ()) {
145
BinaryFormatter bf = new BinaryFormatter ();
146
bf.Serialize (ms, val);
147
return ms.ToArray ();
151
public static object DeserializeValue (byte[] data)
153
using (MemoryStream ms = new MemoryStream (data)) {
154
BinaryFormatter bf = new BinaryFormatter ();
155
return bf.Deserialize (ms);
160
public delegate object DataRequestDelegate (TransferDataType type);