Esempio n. 1
0
        /// <summary>
        /// Implementation of "paste" action using 2 delegates for the concrete action on JToken tree and TreeView.
        /// </summary>
        /// <param name="node"></param>
        /// <param name="pasteJTokenImplementation">Implementation of paste action in the JToken tree.</param>
        /// <param name="pasteTreeNodeImplementation">Implementation of paste action in the treeView.</param>
        private static void ClipboardPaste(this JTokenTreeNode node, Action <JToken> pasteJTokenImplementation, Action <TreeNode> pasteTreeNodeImplementation)
        {
            var sourceJTokenTreeNode = EditorClipboard <JTokenTreeNode> .Get();

            var jTokenSource = sourceJTokenTreeNode.JTokenTag.DeepClone();

            try
            {
                pasteJTokenImplementation(jTokenSource);
            }
            catch (Exception exception)
            {
                // If cut was asked, the clipboard is now empty and source should be inserted again in clipboard
                if (EditorClipboard <JTokenTreeNode> .IsEmpty())
                {
                    EditorClipboard <JTokenTreeNode> .Set(sourceJTokenTreeNode, false);
                }

                throw new JTokenTreeNodePasteException(exception);
            }

            var treeView = node.TreeView;

            treeView.BeginUpdate();

            pasteTreeNodeImplementation(JsonTreeNodeFactory.Create(jTokenSource));

            treeView.EndUpdate();

            // If cut was asked, the clipboard is now empty and source should be removed from treeview
            if (EditorClipboard <JTokenTreeNode> .IsEmpty())
            {
                sourceJTokenTreeNode.EditDelete();
            }
        }
        /// <summary>
        /// Sets the json string to show.
        /// </summary>
        /// <param name="jsonString"></param>
        public void SetJsonSource(string jsonString)
        {
            var jsonEditorItem = new JTokenRoot(jsonString);

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }
        /// <summary>
        /// Sets the stream from which to get the json string to show.
        /// </summary>
        /// <param name="stream"></param>
        public void SetJsonSource(Stream stream)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            JTokenRoot jsonEditorItem;

            try
            {
                jsonEditorItem = new JTokenRoot(stream);
            }
            catch (Exception exception)
            {
                throw new WrongJsonStreamException("Stream could not be parsed from json", exception);
            }

            jsonTreeView.Nodes.Clear();
            jsonTreeView.Nodes.Add(JsonTreeNodeFactory.Create(jsonEditorItem.JTokenValue, 2));
            jsonTreeView.Nodes
            .Cast <TreeNode>()
            .ForEach(n => n.Expand());
        }