using GongSolutions.Wpf.DragDrop;
|
using GongSolutions.Wpf.DragDrop.Utilities;
|
using OpenTap;
|
using System.Windows;
|
|
namespace OpenTapEditor.Util
|
{
|
public class StepTreeDragHandler: DefaultDropHandler
|
{
|
public bool CanConvert(IDropInfo dropInfo)
|
{
|
if (!(dropInfo.TargetItem is TestStep target))
|
{
|
return false;
|
}
|
|
if (!(dropInfo.Data is TestStep source))
|
{
|
return false;
|
}
|
|
if (source == target)
|
{
|
return false;
|
}
|
var tType = target.GetType();
|
var sType = source.GetType();
|
if (!dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
|
{
|
return true;
|
}
|
|
if (!TestStepList.AllowChild(tType, sType))
|
{
|
return false;
|
}
|
|
//if(source.)
|
|
//if (target.sequence != source.sequence)
|
//{
|
// return false;
|
//}
|
return true;
|
}
|
|
public override void DragOver(IDropInfo dropInfo)
|
{
|
if (CanConvert(dropInfo))
|
{
|
dropInfo.Effects = DragDropEffects.Move;
|
|
if (dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter))
|
{
|
dropInfo.DropTargetAdorner = null;
|
}
|
else
|
{
|
dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
|
}
|
|
//if (dropInfo.DropTargetAdorner == DropTargetAdorners.Highlight) {
|
|
//}
|
//dropInfo.DropTargetAdorner = DropTargetAdorners.Insert;
|
dropInfo.DropTargetHintState = DropHintState.Active;
|
dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
|
}
|
else
|
{
|
dropInfo.Effects = DragDropEffects.None;
|
dropInfo.DropTargetHintAdorner = DropTargetAdorners.Hint;
|
dropInfo.DropTargetHintState = DropHintState.Error;
|
}
|
}
|
|
public override void Drop(IDropInfo dropInfo)
|
{
|
if (dropInfo?.DragInfo == null)
|
{
|
return;
|
}
|
|
if (!CanConvert(dropInfo))
|
{
|
return;
|
}
|
|
ITestStep target = dropInfo.TargetItem as ITestStep;
|
|
var insertIndex = GetInsertIndex(dropInfo);
|
var destinationList = dropInfo.TargetCollection.TryGetList();
|
var data = ExtractData(dropInfo.Data).OfType<object>().ToList();
|
bool isSameCollection = false;
|
|
var copyData = ShouldCopyData(dropInfo);
|
if (!copyData)
|
{
|
var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
|
if (sourceList != null)
|
{
|
isSameCollection = sourceList.IsSameObservableCollection(destinationList);
|
if (!isSameCollection)
|
{
|
foreach (var o in data)
|
{
|
var index = sourceList.IndexOf(o);
|
if (index != -1)
|
{
|
sourceList.RemoveAt(index);
|
|
// If source is destination too fix the insertion index
|
if (destinationList != null && ReferenceEquals(sourceList, destinationList) && index < insertIndex)
|
{
|
--insertIndex;
|
}
|
}
|
}
|
}
|
}
|
}
|
|
if (destinationList != null)
|
{
|
var objects2Insert = new List<object>();
|
|
// check for cloning
|
var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);
|
|
foreach (var o in data)
|
{
|
var obj2Insert = o;
|
if (cloneData)
|
{
|
if (o is ICloneableDragItem cloneableItem)
|
{
|
obj2Insert = cloneableItem.CloneItem(dropInfo);
|
}
|
else if (o is ICloneable cloneable)
|
{
|
obj2Insert = cloneable.Clone();
|
}
|
}
|
|
objects2Insert.Add(obj2Insert);
|
|
if (!cloneData && isSameCollection)
|
{
|
var index = destinationList.IndexOf(o);
|
if (index != -1)
|
{
|
if (insertIndex > index)
|
{
|
insertIndex--;
|
}
|
|
Move(destinationList, index, insertIndex++);
|
}
|
}
|
else
|
{
|
destinationList.Insert(insertIndex++, obj2Insert);
|
}
|
|
if (obj2Insert is IDragItemSource dragItemSource)
|
{
|
dragItemSource.ItemDropped(dropInfo);
|
}
|
}
|
|
SelectDroppedItems(dropInfo, objects2Insert);
|
}
|
}
|
}
|
}
|