/// <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 } } }
/// <summary> /// Loads all layouts located in the supplied folder into the class data member. /// This method recusively scans subfolders to locate the layout files. /// </summary> /// <param name="folderName">The folder to load layouts from.</param> private void RecursivelyLoadLayoutsFromFolder(string folderName) { if (string.IsNullOrEmpty(folderName)) { return; } try { // Load the layouts from disk. foreach (string fileName in Directory.GetFiles(folderName, "*" + FileExtension, SearchOption.AllDirectories)) { // Load each file SavedLayout layout = ReadLayoutFromFile(fileName); if (null != layout) { AddLayout(layout); } else { System.Diagnostics.Trace.TraceWarning("Unable to load layout file '{0}'", fileName); } } } catch (DirectoryNotFoundException) { // Ignore errors if the folder doesn't exist System.Diagnostics.Trace.TraceInformation("Layout folder '{0}' does not exist", folderName); } }
/// <summary> /// Saves the supplied layout to the specified file. /// </summary> /// <param name="layout">The saved layout object to write to the file.</param> /// <param name="fileName">The name of the file to write.</param> /// <returns>true if the file was written, false if an error occured.</returns> private static bool SaveLayoutToFile(SavedLayout layout, string fileName) { try { XmlSerializer serializer = new XmlSerializer(typeof(SavedLayout)); using (TextWriter writer = new StreamWriter(fileName)) { serializer.Serialize(writer, layout); } return(true); } catch (System.IO.IOException) { return(false); } }
/// <summary> /// Contructs the filename, including sub-folders, for the saved layout. /// By default, layouts are saved in separate folders for each playing /// surface type and each category. /// </summary> /// <param name="layout">The layout to generate a file name for.</param> /// <returns>The file name for the layout file.</returns> private string GetFileNameForLayout(SavedLayout layout) { if (string.IsNullOrEmpty(layout.FieldTypeTag)) { string msg = Properties.Resources.ResourceManager.GetString("ExceptionMessage_LayoutFieldTagInvalid"); throw new ArgumentException(msg, "layout"); } if (string.IsNullOrEmpty(layout.Name)) { string msg = Properties.Resources.ResourceManager.GetString("ExceptionMessage_LayoutNameInvalid"); throw new ArgumentException(msg, "layout"); } string fn = Path.Combine(LayoutPath, layout.FieldTypeTag); if (!string.IsNullOrEmpty(layout.Category)) { fn = Path.Combine(fn, layout.Category); } fn = Path.Combine(fn, layout.Name + FileExtension); return(fn); }
/// <summary> /// Reads a saved layout from a file. This method catches most exceptions and /// masks any errors that occur and returns a null layout if an error occurs. /// </summary> /// <param name="fileName">The file to read the layout from.</param> /// <returns>A saved layout or null if the file could not be loaded properly.</returns> private static SavedLayout ReadLayoutFromFile(string fileName) { try { XmlSerializer serializer = new XmlSerializer(typeof(SavedLayout)); using (FileStream fs = new FileStream(fileName, FileMode.Open)) { SavedLayout layout = (SavedLayout)serializer.Deserialize(fs); if ((string.IsNullOrEmpty(layout.FieldTypeTag)) || (string.IsNullOrEmpty(layout.Name))) { return(null); } return(layout); } } catch (System.InvalidOperationException) { return(null); } catch (System.Xml.XmlException) { return(null); } catch (System.IO.IOException) { return(null); } }
private void AddLayout(SavedLayout layout) { // TODO: See if the layout already "exists" in our set of layouts savedLayouts.Add(layout); }