private void RequestForm_Load()

in Office365APIEditor/UI/RequestForm.cs [51:334]


        private void RequestForm_Load(object sender, EventArgs e)
        {
            Icon = Properties.Resources.DefaultIcon;

            button_RefreshToken.Enabled = false;
            button_Run.Enabled = false;
            button_ViewTokenInfo.Enabled = false;

            // Change window title
            Text = Util.GenerateWindowTitle("Editor");

            // Restore window location, size and state.

            var savedBounds = Properties.Settings.Default.RequestFormBounds;
            var savedWindowState = Properties.Settings.Default.RequestFormWindowState;

            if (savedWindowState == FormWindowState.Minimized)
            {
                // We should not restore window location, size and state.
                savedBounds = new Rectangle(0, 0, 0, 0);
                savedWindowState = FormWindowState.Normal;
            }
            else if (savedWindowState == FormWindowState.Maximized)
            {
                // We should not restore window size.
                savedBounds = new Rectangle(new Point(savedBounds.Location.X + 8, savedBounds.Y + 8), new Size(Width, Height));
            }

            if (!savedBounds.IsEmpty)
            {
                // We should not restore window size and location if it is out of screens.

                foreach (Screen screen in Screen.AllScreens)
                {
                    if (screen.WorkingArea.Contains(savedBounds))
                    {
                        Bounds = savedBounds;
                        break;
                    }
                }
            }

            WindowState = savedWindowState;

            // Enable Ctrl+A short cut key for all textbox.
            AddKeyDownEvent(this);
            
            // Load Run History
            try
            {
                string runHistoryFilePath = Util.RunHistoryPath;
                if (File.Exists(runHistoryFilePath))
                {
                    using (FileStream stream = new FileStream(runHistoryFilePath, FileMode.Open))
                    {
                        System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(RunHistory));
                        runHistory = (RunHistory)serializer.Deserialize(stream);
                    }
                }
                else
                {
                    runHistory.RunInfo = new System.Collections.Generic.List<RunInformation>();
                }
            }
            catch(Exception ex)
            {
                runHistory.RunInfo = new System.Collections.Generic.List<RunInformation>();

                MessageBox.Show("The run history file could not be loaded." + Environment.NewLine + Environment.NewLine + ex.Message, "Office365APIEditor");
            }

            listBox_RunHistory.DrawMode = DrawMode.OwnerDrawFixed;
            listBox_RunHistory.ItemHeight = 80;

            for (int i = runHistory.RunInfo.Count -1 ; i >= 0 ; i--)
            {
                listBox_RunHistory.Items.Add(runHistory.RunInfo[i]);
            }

            // JSON style editor setting.

            // Request Body
            scintilla_RequestBody = new Scintilla();
            tabPage_Body.Controls.Add(scintilla_RequestBody);

            scintilla_RequestBody.Dock = DockStyle.Fill;
            InitSyntaxColoring(scintilla_RequestBody);
            scintilla_RequestBody.ReadOnly = false;

            // Folding
            scintilla_RequestBody.Lexer = Lexer.Json;

            // Instruct the lexer to calculate folding
            scintilla_RequestBody.SetProperty("fold", "1");
            scintilla_RequestBody.SetProperty("fold.compact", "1");

            // Configure a margin to display folding symbols
            scintilla_RequestBody.Margins[2].Type = MarginType.Symbol;
            scintilla_RequestBody.Margins[2].Mask = Marker.MaskFolders;
            scintilla_RequestBody.Margins[2].Sensitive = true;
            scintilla_RequestBody.Margins[2].Width = 20;

            // Set colors for all folding markers
            for (int i = 25; i <= 31; i++)
            {
                scintilla_RequestBody.Markers[i].SetForeColor(SystemColors.ControlLightLight);
                scintilla_RequestBody.Markers[i].SetBackColor(SystemColors.ControlDark);
            }

            // Configure folding markers with respective symbols
            scintilla_RequestBody.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            scintilla_RequestBody.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            scintilla_RequestBody.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            scintilla_RequestBody.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            scintilla_RequestBody.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            scintilla_RequestBody.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            scintilla_RequestBody.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            // Enable automatic folding
            scintilla_RequestBody.AutomaticFold = (AutomaticFold.Show | AutomaticFold.Click | AutomaticFold.Change);

            // Tie in Scintilla event
            scintilla_RequestBody.KeyDown += Scintilla_RequestBody_KeyDown;

            // Response Body
            scintilla_ResponseBody = new Scintilla();
            tabPage2.Controls.Add(scintilla_ResponseBody);

            scintilla_ResponseBody.Dock = DockStyle.Fill;
            InitSyntaxColoring(scintilla_ResponseBody);
            scintilla_ResponseBody.ReadOnly = true;

            // Folding
            scintilla_ResponseBody.Lexer = Lexer.Json;

            // Instruct the lexer to calculate folding
            scintilla_ResponseBody.SetProperty("fold", "1");
            scintilla_ResponseBody.SetProperty("fold.compact", "1");

            // Configure a margin to display folding symbols
            scintilla_ResponseBody.Margins[2].Type = MarginType.Symbol;
            scintilla_ResponseBody.Margins[2].Mask = Marker.MaskFolders;
            scintilla_ResponseBody.Margins[2].Sensitive = true;
            scintilla_ResponseBody.Margins[2].Width = 20;

            // Set colors for all folding markers
            for (int i = 25; i <= 31; i++)
            {
                scintilla_ResponseBody.Markers[i].SetForeColor(SystemColors.ControlLightLight);
                scintilla_ResponseBody.Markers[i].SetBackColor(SystemColors.ControlDark);
            }

            // Configure folding markers with respective symbols
            scintilla_ResponseBody.Markers[Marker.Folder].Symbol = MarkerSymbol.BoxPlus;
            scintilla_ResponseBody.Markers[Marker.FolderOpen].Symbol = MarkerSymbol.BoxMinus;
            scintilla_ResponseBody.Markers[Marker.FolderEnd].Symbol = MarkerSymbol.BoxPlusConnected;
            scintilla_ResponseBody.Markers[Marker.FolderMidTail].Symbol = MarkerSymbol.TCorner;
            scintilla_ResponseBody.Markers[Marker.FolderOpenMid].Symbol = MarkerSymbol.BoxMinusConnected;
            scintilla_ResponseBody.Markers[Marker.FolderSub].Symbol = MarkerSymbol.VLine;
            scintilla_ResponseBody.Markers[Marker.FolderTail].Symbol = MarkerSymbol.LCorner;

            // Enable automatic folding
            scintilla_ResponseBody.AutomaticFold = (AutomaticFold.Show | AutomaticFold.Click | AutomaticFold.Change);

            // Enable URL Interaction
            scintilla_ResponseBody.Styles[Style.Json.Uri].Hotspot = true;
            scintilla_ResponseBody.HotspotClick += new EventHandler<HotspotClickEventArgs>(this.scintilla_HotspotClick);

            // Create instance of FindReplace with reference to a ScintillaNET control.
            findReplaceDialog_ResponseBody = new FindReplace(scintilla_ResponseBody); // For WinForms

            // Tie in FindReplace event
            findReplaceDialog_ResponseBody.KeyPressed += FindReplaceDialog_ResponseBody_KeyPressed;

            // Tie in Scintilla event
            scintilla_ResponseBody.KeyDown += Scintilla_ResponseBody_KeyDown;

            // Load sample request

            List<SampleRequestDefinitionRoot> sampleRequestDefinitionLists = new List<SampleRequestDefinitionRoot>();
            string sampleRequestDirectory = Path.Combine(Application.StartupPath, "SampleRequest");

            if (Directory.Exists(sampleRequestDirectory))
            {
                string[] definitionFiles = Directory.GetFiles(sampleRequestDirectory, "*.json", SearchOption.TopDirectoryOnly);

                foreach (var definitionFilePath in definitionFiles)
                {
                    try
                    {
                        string rawJsonSampleRequest = "";

                        using (StreamReader reader = new StreamReader(definitionFilePath))
                        {
                            rawJsonSampleRequest = reader.ReadToEnd();
                        }

                        var sampleRequest = JsonConvert.DeserializeObject<SampleRequestDefinitionRoot>(rawJsonSampleRequest);
                        sampleRequestDefinitionLists.Add(sampleRequest);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }

            sampleRequests = new List<SampleRequest>();

            foreach (var sampleRequest in sampleRequestDefinitionLists)
            {
                var rootNode = new TreeNode(sampleRequest.DisplayName);

                foreach (var firstLevelCategory in sampleRequest.FirstLevelCategory)
                {
                    var firstLevelNode = new TreeNode(firstLevelCategory.DisplayName);

                    foreach (var secondLevelCategory in firstLevelCategory.SecondLevelCategory)
                    {
                        var secondLevelNode = new TreeNode(secondLevelCategory.DisplayName);

                        foreach (var thirdLevelCategory in secondLevelCategory.ThirdLevelCategory)
                        {
                            var thirdLevelNode = new TreeNode(thirdLevelCategory.DisplayName);

                            if (thirdLevelCategory.SampleRequest != null)
                            {
                                foreach (var sample in thirdLevelCategory.SampleRequest)
                                {
                                    // Check whether ID is duplicated or not.

                                    if (sampleRequests.Where(s => s.Id == sample.Id).Count() != 0)
                                    {
                                        MessageBox.Show("A Duplicated sample request ID was found in the definition file." + Environment.NewLine + sample.Id, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                    }
                                    else
                                    {
                                        var sampleNode = new TreeNode(sample.DisplayName) { Tag = sample.Id };
                                        thirdLevelNode.Nodes.Add(sampleNode);

                                        sampleRequests.Add(sample);
                                    }
                                }
                            }

                            if (thirdLevelCategory.FourthLevelCategory != null)
                            {
                                foreach (var fourthLevelCategory in thirdLevelCategory.FourthLevelCategory)
                                {
                                    var fourthLevelNode = new TreeNode(fourthLevelCategory.DisplayName);

                                    foreach (var sample in fourthLevelCategory.SampleRequest)
                                    {
                                        // Check whether ID is duplicated or not.

                                        if (sampleRequests.Where(s => s.Id == sample.Id).Count() != 0)
                                        {
                                            MessageBox.Show("A Duplicated sample request ID was found in the definition file." + Environment.NewLine + sample.Id, "Office365APIEditor", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                        }
                                        else
                                        {
                                            var sampleNode = new TreeNode(sample.DisplayName) { Tag = sample.Id };
                                            fourthLevelNode.Nodes.Add(sampleNode);

                                            sampleRequests.Add(sample);
                                        }
                                    }

                                    thirdLevelNode.Nodes.Add(fourthLevelNode);
                                }
                            }

                            secondLevelNode.Nodes.Add(thirdLevelNode);
                        }

                        firstLevelNode.Nodes.Add(secondLevelNode);
                    }

                    rootNode.Nodes.Add(firstLevelNode);
                }

                treeView_Example.Nodes.Add(rootNode);
            }
        }