Vereinsmeisterschaften  22aa7800eae54b428d40e835886cefe1fdefdfdf
This is a software that can be used to manage the internal competition of the swimming club Illertissen called "Vereinsmeisterschaften".
Loading...
Searching...
No Matches
DropAllowedHandlerParkingLot.cs
1using System.Collections;
4using GongSolutions.Wpf.DragDrop;
5using GongSolutions.Wpf.DragDrop.Utilities;
6
8{
12 public class DropAllowedHandlerParkingLot : DefaultDropHandler
13 {
14 #region Singleton
15
16 private static readonly DropAllowedHandlerParkingLot instance = new DropAllowedHandlerParkingLot();
17
18 // Explicit static constructor to tell C# compiler not to mark type as beforefieldinit
19 static DropAllowedHandlerParkingLot()
20 {
21 }
22
23 private DropAllowedHandlerParkingLot()
24 {
25 }
26
30 public static DropAllowedHandlerParkingLot Instance => instance;
31
32 #endregion
33
34 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
35
37 public override void DragOver(IDropInfo dropInfo)
38 {
39 dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
40
41 if (dropAllowed(dropInfo))
42 {
43 dropInfo.DropTargetHintState = DropHintState.Active;
44 dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
45 dropInfo.Effects = DragDropEffects.Move;
46 }
47 else
48 {
49 dropInfo.DropTargetHintState = DropHintState.Error;
50 dropInfo.DropHintText = "";
51 dropInfo.Effects = DragDropEffects.None;
52 }
53 }
54
56 public override void Drop(IDropInfo dropInfo)
57 {
58 if (dropAllowed(dropInfo))
59 {
60 // Check if the default drag and drop handler can accept the data (if dragged data type matches the target list type)
61 if (CanAcceptData(dropInfo))
62 {
63 base.Drop(dropInfo);
64 }
65 else
66 {
67 // Custom drop logic needed (e.g. Race is dropped on List of PersonStart)
68 DropRaceOnPersonStarts(dropInfo);
69 }
70 }
71 }
72
77 private void DropRaceOnPersonStarts(IDropInfo dropInfo)
78 {
79 if (dropInfo?.DragInfo == null)
80 {
81 return;
82 }
83
84 int insertIndex = GetInsertIndex(dropInfo);
85 IList destinationList = dropInfo.TargetCollection.TryGetList();
86 List<object> data = ExtractData(dropInfo.Data).OfType<object>().ToList();
87
88 if(data.FirstOrDefault().GetType() != typeof(Race))
89 {
90 return;
91 }
92
93 bool isSameCollection = false;
94 IList sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
95 if (sourceList != null)
96 {
97 isSameCollection = sourceList.IsSameObservableCollection(destinationList);
98 if (!isSameCollection)
99 {
100 foreach (object o in data)
101 {
102 int index = sourceList.IndexOf(o);
103 if (index != -1)
104 {
105 sourceList.RemoveAt(index);
106
107 // If source is destination too fix the insertion index
108 if (destinationList != null && ReferenceEquals(sourceList, destinationList) && index < insertIndex)
109 {
110 --insertIndex;
111 }
112 }
113 }
114 }
115 }
116
117 if (destinationList != null)
118 {
119 List<object> objects2Insert = new List<object>();
120
121 if(data.FirstOrDefault().GetType() == typeof(Race))
122 {
123 List<PersonStart> persons = new List<PersonStart>();
124 data.Cast<Race>().ToList().ForEach(r => persons.AddRange(r.Starts));
125 data = persons.Cast<object>().ToList();
126 }
127
128 foreach (object o in data)
129 {
130 object obj2Insert = o;
131
132 objects2Insert.Add(obj2Insert);
133
134 if (isSameCollection)
135 {
136 int index = destinationList.IndexOf(o);
137 if (index != -1)
138 {
139 if (insertIndex > index)
140 {
141 insertIndex--;
142 }
143
144 Move(destinationList, index, insertIndex++);
145 }
146 }
147 else
148 {
149 destinationList.Insert(insertIndex++, obj2Insert);
150 }
151
152 if (obj2Insert is IDragItemSource dragItemSource)
153 {
154 dragItemSource.ItemDropped(dropInfo);
155 }
156 }
157
158 SelectDroppedItems(dropInfo, objects2Insert);
159 }
160 }
161
167 private bool dropAllowed(IDropInfo dropInfo)
168 {
169 Type dragItemType = dropInfo.DragInfo.SourceItem.GetType();
170 return dragItemType == typeof(Race) || dragItemType == typeof(PersonStart);
171 }
172
173 }
174}
Class describing a start of a person.
Definition PersonStart.cs:9
Class that represents a single race.
Definition Race.cs:12
static DropAllowedHandlerParkingLot Instance
Singleton instance for the DropAllowedHandlerParkingLot
bool dropAllowed(IDropInfo dropInfo)
Determine if dropping is allowed.
void DropRaceOnPersonStarts(IDropInfo dropInfo)
Custom drop logic inspired by DefaultDropHandler.Drop(IDropInfo)