internal static SavedLayout AskUserForSavedLayoutDetails(ICollection <FieldObject> fieldObjects, string fieldTypeTag, string[] existingLayoutCategories)
        {
            using (SavedLayoutInformation dialog = new SavedLayoutInformation()) {
                foreach (var fo in fieldObjects)
                {
                    SavedLayoutEntryItem e = new SavedLayoutEntryItem()
                    {
                        Tag  = fo.Tag,
                        Name = fo.Name
                    };
                    dialog.entriesListBox.Items.Add(e, true);
                }
                dialog.categoryComboBox.DataSource    = existingLayoutCategories;
                dialog.categoryComboBox.SelectedIndex = -1; // make sure nothing is specified at first

                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    FieldLayout layout = FieldControl.ConvertFieldObjectsToLayout(fieldObjects);
                    for (int index = 0; index < dialog.entriesListBox.Items.Count; index++)
                    {
                        if (!dialog.entriesListBox.GetItemChecked(index))
                        {
                            layout.RemoveEntry(((SavedLayoutEntryItem)(dialog.entriesListBox.Items[index])).Tag);
                        }
                    }
                    return(new SavedLayout(dialog.nameTextBox.Text, dialog.categoryComboBox.Text, dialog.descriptionTextBox.Text, layout, fieldTypeTag));
                }
                return(null);
            }
        }
        /// <summary>
        /// Prompts the user to save the current layout, along with asking for
        /// details under which to save the layout. If the user saves the layout, it
        /// is added to the list of saved layouts.
        /// </summary>
        /// <param name="layoutToSave">The layout to be saved which describes the
        /// current positions of the field objects.</param>
        /// <param name="fieldTypeTag">Identifies the field type of the layout.</param>
        public void SaveCurrentLayout(ICollection <FieldObject> layoutToSave, string fieldTypeTag)
        {
            if (string.IsNullOrEmpty(layoutPath))
            {
                // TODO: Display an error message to the user
                return;
            }
            SavedLayout sl =
                SavedLayoutInformation.AskUserForSavedLayoutDetails(layoutToSave,
                                                                    fieldTypeTag,
                                                                    GetCurrentSavedLayoutCategories(fieldTypeTag));

            if (null != sl)
            {
                // Save the layout to disk
                string fileName = GetFileNameForLayout(sl);
                {
                    string folderName = Path.GetDirectoryName(fileName);
                    if (!Directory.Exists(folderName))
                    {
                        Directory.CreateDirectory(folderName);
                    }
                }
                bool saveResult = SaveLayoutToFile(sl, fileName);
                if (saveResult)
                {
                    // Add the layout to our internal list
                    AddLayout(sl);
                }
                else
                {
                    // TODO: Display an error message to the user
                }
            }
        }