Skip to content

Commit

Permalink
- New: Some copy/move actions added using Drag&Drop.
Browse files Browse the repository at this point in the history
  • Loading branch information
zetoken committed Apr 20, 2014
1 parent 49c4fba commit b374260
Showing 1 changed file with 110 additions and 49 deletions.
159 changes: 110 additions & 49 deletions Json Editor/Forms/JTokenTreeView.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Reflection;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Linq;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ZTn.Json.Editor.Extensions;
using ZTn.Json.Editor.Generic;

namespace ZTn.Json.Editor.Forms
{
Expand All @@ -14,9 +12,9 @@ public partial class JTokenTreeView : TreeView
#region >> Fields

JTokenTreeNode lastDragDropTarget;
DateTime lastDragDropDateTime;
Color lastDragDropBackColor;
DragDropEffects lastValidDragDropEffect;
DateTime lastDragOverDateTime;
Color lastDragDropTargetBackColor;
readonly TimeSpan dragDropExpandDelay = new TimeSpan(5000000);

#endregion

Expand Down Expand Up @@ -69,7 +67,6 @@ private void ItemDragHandler(object sender, ItemDragEventArgs e)
return;
}

lastValidDragDropEffect = DragDropEffects.Copy;
DoDragDrop(e.Item, DragDropEffects.Move | DragDropEffects.Copy);
}

Expand All @@ -82,7 +79,7 @@ private void DragDropHandler(object sender, DragEventArgs e)
{
if (lastDragDropTarget != null)
{
lastDragDropTarget.BackColor = lastDragDropBackColor;
lastDragDropTarget.BackColor = lastDragDropTargetBackColor;
lastDragDropTarget = null;
}

Expand Down Expand Up @@ -131,6 +128,17 @@ private void DoDragDropCopy(JPropertyTreeNode sourceNode, JObjectTreeNode target
targetNode.ClipboardPasteInto();
}

/// <summary>
/// Copies a JValue into a JArray as first child.
/// </summary>
/// <param name="sourceNode"></param>
/// <param name="targetNode"></param>
private void DoDragDropCopy(JValueTreeNode sourceNode, JArrayTreeNode targetNode)
{
sourceNode.ClipboardCopy();
targetNode.ClipboardPasteInto();
}

/// <summary>
/// Copies a JObject into a JArray as first child.
/// </summary>
Expand Down Expand Up @@ -163,6 +171,28 @@ private void DoDragDropMove(JTokenTreeNode sourceNode, JTokenTreeNode targetNode
MessageBox.Show(@"Drag & Drop: Unmanaged Move");
}

/// <summary>
/// Copies a JProperty into a JObject as first child.
/// </summary>
/// <param name="sourceNode"></param>
/// <param name="targetNode"></param>
private void DoDragDropMove(JPropertyTreeNode sourceNode, JObjectTreeNode targetNode)
{
sourceNode.ClipboardCut();
targetNode.ClipboardPasteInto();
}

/// <summary>
/// Copies a JObject into a JArray as first child.
/// </summary>
/// <param name="sourceNode"></param>
/// <param name="targetNode"></param>
private void DoDragDropMove(JObjectTreeNode sourceNode, JArrayTreeNode targetNode)
{
sourceNode.ClipboardCut();
targetNode.ClipboardPasteInto();
}

#endregion

/// <summary>
Expand All @@ -183,76 +213,79 @@ private void DragOverHandler(object sender, DragEventArgs e)
{
var targetNode = GetDragDropTargetNode(e);

var keyState = (KeyStates)e.KeyState;
if ((keyState & KeyStates.Control) == KeyStates.Control)
{
e.Effect = DragDropEffects.Copy;
}
else if ((keyState & KeyStates.Shift) == KeyStates.Shift)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.Copy;
}

if (targetNode == null)
{
if (e.Effect != DragDropEffects.None)
{
lastValidDragDropEffect = e.Effect;
e.Effect = DragDropEffects.None;
}
e.Effect = DragDropEffects.None;

if (lastDragDropTarget != null)
{
lastDragDropTarget.BackColor = lastDragDropBackColor;
lastDragDropTarget.BackColor = lastDragDropTargetBackColor;
}

lastDragDropTarget = null;

return;
}

if (targetNode == lastDragDropTarget)
var keyState = (KeyStates)e.KeyState;
if (keyState.HasFlag(KeyStates.Control | KeyStates.Shift))
{
if (DateTime.Now - lastDragDropDateTime >= new TimeSpan(5000000))
{
targetNode.Expand();
}

return;
e.Effect = DragDropEffects.None;
}
else if (keyState.HasFlag(KeyStates.Control))
{
e.Effect = DragDropEffects.Copy;
}
else if (keyState.HasFlag(KeyStates.Shift))
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.Move;
}

var sourceNode = GetDragDropSourceNode(e);

if (IsDragDropValid(sourceNode, targetNode))
if (targetNode == lastDragDropTarget)
{
if (e.Effect == DragDropEffects.None)
if (!targetNode.IsExpanded && DateTime.Now - lastDragOverDateTime >= dragDropExpandDelay)
{
e.Effect = lastValidDragDropEffect;
targetNode.Expand();
}

lastDragDropBackColor = targetNode.BackColor;
targetNode.BackColor = Color.BlueViolet;
if (IsDragDropValid(sourceNode, targetNode, e.Effect))
{
lastDragDropTargetBackColor = targetNode.BackColor;
targetNode.BackColor = Color.BlueViolet;
}
else
{
targetNode.BackColor = lastDragDropTargetBackColor;
e.Effect = DragDropEffects.None;
}
}
else
{
if (e.Effect != DragDropEffects.None)
lastDragDropTarget = targetNode;
lastDragOverDateTime = DateTime.Now;

if (IsDragDropValid(sourceNode, targetNode, e.Effect))
{
lastDragDropTargetBackColor = targetNode.BackColor;
targetNode.BackColor = Color.BlueViolet;
}
else
{
lastValidDragDropEffect = e.Effect;
targetNode.BackColor = lastDragDropTargetBackColor;
e.Effect = DragDropEffects.None;
}
}

if (lastDragDropTarget != null)
{
lastDragDropTarget.BackColor = lastDragDropBackColor;
lastDragDropTarget.BackColor = lastDragDropTargetBackColor;
}

lastDragDropTarget = targetNode;
lastDragDropDateTime = DateTime.Now;
}

private static JTokenTreeNode GetDragDropSourceNode(DragEventArgs e)
Expand All @@ -268,7 +301,7 @@ private JTokenTreeNode GetDragDropTargetNode(DragEventArgs e)
return targetNode;
}

private bool IsDragDropValid(JTokenTreeNode sourceNode, JTokenTreeNode targetNode)
private bool IsDragDropValid(JTokenTreeNode sourceNode, JTokenTreeNode targetNode, DragDropEffects effect)
{
if (sourceNode == null || targetNode == null)
{
Expand All @@ -279,13 +312,41 @@ private bool IsDragDropValid(JTokenTreeNode sourceNode, JTokenTreeNode targetNod
{
return targetNode.JTokenTag is JObject;
}

if (sourceNode.JTokenTag is JObject)
{
return targetNode.JTokenTag is JProperty || targetNode.JTokenTag is JArray;
switch (effect)
{
case DragDropEffects.Copy:
return targetNode.JTokenTag is JArray;
case DragDropEffects.Move:
return !(targetNode.JTokenTag.Parent is JProperty)
&& targetNode.JTokenTag is JArray;
}
}

if (sourceNode.JTokenTag is JArray)
{
return targetNode.JTokenTag is JArray;
switch (effect)
{
case DragDropEffects.Copy:
return targetNode.JTokenTag is JArray;
case DragDropEffects.Move:
return !(targetNode.JTokenTag.Parent is JProperty)
&& targetNode.JTokenTag is JArray;
}
}

if (sourceNode.JTokenTag is JValue)
{
switch (effect)
{
case DragDropEffects.Copy:
return targetNode.JTokenTag is JArray;
case DragDropEffects.Move:
return !(targetNode.JTokenTag.Parent is JProperty)
&& targetNode.JTokenTag is JArray;
}
}

return false;
Expand Down

0 comments on commit b374260

Please sign in to comment.