Example #1
0
        private ToolLayout __save_documents_docking_layout(LayoutDocumentPaneGroup root)
        {
            LayoutGroup r = new LayoutGroup();

            r.SubLayout   = new List <ToolLayout>();
            r.Orientation = root.Orientation == Orientation.Horizontal ? TOOL_LAYOUT_ORIENTATION_T.HORIZONTAL : TOOL_LAYOUT_ORIENTATION_T.VERTICAL;
            r.Height      = root.DockHeight.Value;
            r.Width       = root.DockWidth.Value;
            foreach (var sub in root.Children)
            {
                if (sub is LayoutDocumentPaneGroup)
                {
                    r.SubLayout.Add(__save_documents_docking_layout(sub as LayoutDocumentPaneGroup));
                }
                else
                {
                    r.SubLayout.Add(__save_documents_docking_layout(sub as LayoutDocumentPane));
                }
            }
            return(r);
        }
        public ToolLayout ImportDockingToolboxFromJSON(ref Utf8JsonReader reader, ReadOnlySpan <byte> dataSpan)
        {
            ToolLayout layout;

            ClearToolbox();
            if (reader.Read() == false || reader.TokenType != JsonTokenType.StartObject)
            {
                throw new ArgumentException("The given JSON is not a valid <ToolLayout> object.");
            }

            int depth = reader.CurrentDepth;

            try
            {
                if (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
                {
                    string property = reader.GetString();
                    int    index0   = property.IndexOf('*');
                    int    index1   = property.LastIndexOf('*');
                    if (index0 == -1 || index1 == -1)
                    {
                        throw new ArgumentException($"The property({property}) name of root <ToolLayout> object in JSON is invalid.");
                    }
                    layout = new LayoutGroup();
                    var layoutgroup = layout as LayoutGroup;
                    try
                    {
                        layoutgroup.Height = double.Parse(property.Substring(index0 + 1, index1 - index0 - 1));
                        layoutgroup.Width  = double.Parse(property.Substring(index1 + 1, property.Length - index1 - 1));
                    }
                    catch
                    {
                        throw new ArgumentException($"The property({property}) name of the root <ToolLayout> object in JSON is invalid.");
                    }
                    if (property.StartsWith("HGroup"))
                    {
                        layoutgroup.Orientation = TOOL_LAYOUT_ORIENTATION_T.HORIZONTAL;
                    }
                    else if (property.StartsWith("VGroup"))
                    {
                        layoutgroup.Orientation = TOOL_LAYOUT_ORIENTATION_T.VERTICAL;
                    }
                    else
                    {
                        throw new ArgumentException($"The property({property}) name of the root <ToolLayout> object in JSON is invalid.");
                    }

                    layoutgroup.SubLayout = __import_docking_toolbox_from_json_group(ref reader, dataSpan);
                }
                else
                {
                    throw new ArgumentException($"There should be one valid property defined in root <ToolLayout> object in JSON.");
                }
                if (reader.Read() == false || reader.TokenType != JsonTokenType.EndObject)
                {
                    throw new ArgumentException($"Only one valid property can be defined in root <ToolLayout> object in JSON or the root <ToolLayout> object in JSON is corrupted.");
                }
            }
            catch
            {
                ClearToolbox();
                throw;
            }

            return(layout);
        }
        private List <ToolLayout> __import_docking_toolbox_from_json_group(ref Utf8JsonReader reader, ReadOnlySpan <byte> dataSpan)
        {
            List <ToolLayout> layout = new List <ToolLayout>();

            if (reader.Read() == false || reader.TokenType != JsonTokenType.StartArray)
            {
                throw new ArgumentException("<Group> property value of <ToolLayout> object in JSON is not a valid array.");
            }
            LayoutGroup     group;
            LayoutPanel <T> panel;

            while (reader.Read() && (reader.TokenType != JsonTokenType.EndArray))
            {
                switch (reader.TokenType)
                {
                case JsonTokenType.StartObject:
                    if (reader.Read() && reader.TokenType == JsonTokenType.PropertyName)
                    {
                        string property = reader.GetString();
                        int    index0   = property.IndexOf('*');
                        int    index1   = property.LastIndexOf('*');
                        if (index0 == -1 || index1 == -1)
                        {
                            throw new ArgumentException($"The property({property}) name of <ToolLayout> object in JSON is invalid.");
                        }
                        double height, width;
                        try
                        {
                            height = double.Parse(property.Substring(index0 + 1, index1 - index0 - 1));
                            width  = double.Parse(property.Substring(index1 + 1, property.Length - index1 - 1));
                        }
                        catch
                        {
                            throw new ArgumentException($"The property({property}) name of <ToolLayout> object in JSON is invalid.");
                        }

                        if (property.StartsWith("HGroup"))
                        {
                            group = new LayoutGroup()
                            {
                                Orientation = TOOL_LAYOUT_ORIENTATION_T.HORIZONTAL, Height = height, Width = width, SubLayout = __import_docking_toolbox_from_json_group(ref reader, dataSpan)
                            };
                            layout.Add(group);
                        }
                        else if (property.StartsWith("VGroup"))
                        {
                            group = new LayoutGroup()
                            {
                                Orientation = TOOL_LAYOUT_ORIENTATION_T.VERTICAL, Height = height, Width = width, SubLayout = __import_docking_toolbox_from_json_group(ref reader, dataSpan)
                            };
                            layout.Add(group);
                        }
                        else if (property.StartsWith("Panel"))
                        {
                            panel = new LayoutPanel <T>()
                            {
                                Height = height, Width = width
                            };
                            panel.Documents = __import_docking_toolbox_from_json_panel(ref reader, dataSpan);
                            layout.Add(panel);
                        }
                        else
                        {
                            throw new ArgumentException($"The property({property}) name of <ToolLayout> object in JSON is invalid.");
                        }
                    }
                    else
                    {
                        throw new ArgumentException($"There should be one valid property defined in <ToolLayout> object in JSON.");
                    }
                    if (reader.Read() == false || reader.TokenType != JsonTokenType.EndObject)
                    {
                        throw new ArgumentException($"Only one valid property can be defined in <ToolLayout> object in JSON or the <ToolLayout> object in JSON is corrupted.");
                    }
                    break;
                }
            }
            return(layout);
        }