using GongSolutions.Wpf.DragDrop;
|
using HMI.UI;
|
using NationalInstruments.TestStand.Interop.API;
|
using Plugin.SeqEditor.Entity;
|
using System.Windows;
|
|
namespace Plugin.SeqEditor.ViewModel
|
{
|
public class StepDropHandler : DefaultDropHandler
|
{
|
private SeqEditorViewModel model;
|
public StepDropHandler(SeqEditorViewModel model)
|
{
|
this.model = model;
|
}
|
|
public bool CanConvert(IDropInfo dropInfo)
|
{
|
if (!(dropInfo.TargetItem is TreeItemData targetItem))
|
{
|
return false;
|
}
|
|
if (!(targetItem.Data is StepUI target))
|
{
|
return false;
|
}
|
|
if (!(dropInfo.Data is TreeItemData sourceItem))
|
{
|
return false;
|
}
|
|
if (!(sourceItem.Data is StepUI source))
|
{
|
return false;
|
}
|
|
if (targetItem.ParentItem != sourceItem.ParentItem)
|
{
|
return false;
|
}
|
|
if (source == target)
|
{
|
return false;
|
}
|
|
if (source.sequence != target.sequence)
|
{
|
return false;
|
}
|
|
return true;
|
}
|
|
public override void DragOver(IDropInfo dropInfo)
|
{
|
if (CanConvert(dropInfo))
|
{
|
dropInfo.Effects = DragDropEffects.Move;
|
dropInfo.DropTargetHintState = DropHintState.Active;
|
}
|
else
|
{
|
dropInfo.DropTargetHintState = DropHintState.None;
|
}
|
}
|
|
public override void Drop(IDropInfo dropInfo)
|
{
|
if (!CanConvert(dropInfo))
|
{
|
return;
|
}
|
var sourceItem = (TreeItemData)dropInfo.Data;
|
var targetItem = (TreeItemData)dropInfo.TargetItem;
|
|
var source = sourceItem.Data as StepUI;
|
var target = targetItem.Data as StepUI;
|
int sourceIndex = source.IndexInSequence;
|
int targetIndex = target.IndexInSequence;
|
|
TreeItemDataCollection collection = model.TreeCollection;
|
|
List<TreeItemData> datasource;
|
|
if (sourceItem.ParentItem == null)
|
{
|
datasource = collection.RootItems;
|
}
|
else
|
{
|
datasource = sourceItem.ParentItem.Items;
|
}
|
|
//ObservableCollection<TreeItemData> datasource =
|
// (ObservableCollection<TreeItemData>)dropInfo.TargetCollection.TryGetList();
|
|
int uiSourceIndex = collection.IndexOf(sourceItem);
|
int uiTargetIndex = collection.IndexOf(targetItem);
|
|
if (uiSourceIndex == uiTargetIndex + 1)
|
{
|
return;
|
}
|
|
StepGroups group = target.StepGroup;
|
if (sourceIndex == targetIndex) {
|
return;
|
}
|
|
if (sourceIndex < targetIndex)
|
{
|
source.sequence.DeleteStep(sourceIndex, source.StepGroup);
|
target.sequence.InsertStep(source.step, targetIndex, group);
|
|
collection.Move(uiSourceIndex, uiTargetIndex);
|
|
//datasource.RemoveAt(uiSourceIndex);
|
//datasource.Insert(uiTargetIndex, sourceItem);
|
}
|
else if (sourceIndex > targetIndex)
|
{
|
source.sequence.DeleteStep(sourceIndex, source.StepGroup);
|
target.sequence.InsertStep(source.step, targetIndex + 1, group);
|
|
collection.Move(uiSourceIndex, uiTargetIndex);
|
}
|
model.CurrentSeqUI.IsChanged = true;
|
//var task = Task.Run(() =>
|
//{
|
// collection.ResetItems();
|
// model.TreeItemData = sourceItem;
|
//});
|
//task.Wait();
|
}
|
}
|
}
|