//here is written the configuration file, default colors are written to this file. public static void WriteCAMainWindowData(string fileNameIn, CAMainWindowVM caMainWindowVMIn) { XmlWriter aXmlWriter = null; try { aXmlWriter = new XmlTextWriter(fileNameIn, Encoding.UTF8); aXmlWriter.WriteStartDocument(); aXmlWriter.WriteStartElement("CAMainWindowData"); WriteColor(aXmlWriter, "SelectedDefaultGridColor", caMainWindowVMIn.SelectedDefaultGridColor); WriteColor(aXmlWriter, "SelectedDefaultSelectionFrameColor", caMainWindowVMIn.SelectedDefaultSelectionFrameColor); WriteColor(aXmlWriter, "SelectedDefaultMarkingColor", caMainWindowVMIn.SelectedDefaultMarkingColor); WriteColor(aXmlWriter, "SelectedDefaultMouseOverColor", caMainWindowVMIn.SelectedDefaultMouseOverColor); WriteColor(aXmlWriter, "SelectedDefaultBackgroundColor", caMainWindowVMIn.SelectedDefaultBackgroundColor); WriteColor(aXmlWriter, "SelectedDefaultStartInterpColor", caMainWindowVMIn.SelectedDefaultStartInterpColor); WriteColor(aXmlWriter, "SelectedDefaultEndInterpColor", caMainWindowVMIn.SelectedDefaultEndInterpColor); foreach (StateAndColor aStateColor in caMainWindowVMIn.DefaultStateColorsCollection) { WriteColor(aXmlWriter, "DefaultStateColor", aStateColor.StateColor, "id", aStateColor.State.ToString(CultureInfo.InvariantCulture)); } aXmlWriter.WriteEndElement(); aXmlWriter.WriteEndDocument(); } catch (Exception ex) { if (ex is InvalidOperationException || ex is EncoderFallbackException || ex is ArgumentException || ex is UnauthorizedAccessException || ex is DirectoryNotFoundException || ex is IOException || ex is System.Security.SecurityException) { DialogMediator.ShowMessageBox(null, "Exception during writing CA Explorer configuration", "Problem with writing of CA Explorer configuration file!: \n" + "Your changes will not be saved!" + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } else { throw; } } finally { aXmlWriter.Close(); } }
private void mwCA_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) { ViewModelBase aViewModelBase = DataContext as ViewModelBase; if (aViewModelBase != null) { //adding this ViewModel = DataContext and this Window to ViewAndViewModelMappings collection. //this is needed because during showing of other Window it is needed to know which is the parent window. DialogMediator.ViewAndViewModelMappings.Add(aViewModelBase, this); DialogMediator.MainWindowView = this; } CAMainWindowVM aCAMainWindowVM = DataContext as CAMainWindowVM; if (aCAMainWindowVM != null) { //during opening loading of the configuration file. aCAMainWindowVM.LoadData(); } }
private void mwCA_Closing(object sender, System.ComponentModel.CancelEventArgs e) { CAMainWindowVM aCAMainWindowVM = this.DataContext as CAMainWindowVM; if (aCAMainWindowVM != null) { try { //during closing configuration file is saved. aCAMainWindowVM.SaveApplicationSettings(); } catch (Exception) { throw; } finally { aCAMainWindowVM.Dispose(); } } }
//all available CAs are written here. public static void WriteAllAvailableCAs(CAMainWindowVM caMainWindowVMIn) { string aCAsWithProblem = null; string aCAExceptions = null; foreach (CAGrid2DVM aCAGrid2DVMItem in caMainWindowVMIn.Grid2DViewModelList) { if (aCAGrid2DVMItem != null) { string aFilename = null; try { aFilename = @".\SavedCA\" + aCAGrid2DVMItem.CAName + ".xml"; CAGrid2DRW.WriteCA(aFilename, aCAGrid2DVMItem); } catch (Exception ex) { if (ex is InvalidOperationException || ex is EncoderFallbackException || ex is ArgumentException || ex is UnauthorizedAccessException || ex is DirectoryNotFoundException || ex is IOException || ex is System.Security.SecurityException || ex is PathTooLongException) { aCAsWithProblem += aFilename + "\n"; aCAExceptions += ex.Message + "\n"; } else { throw; } } } } if (aCAsWithProblem != null) { DialogMediator.ShowMessageBox(null, "Exception during writing CA", "Problem with writing following CA files : \n" + aCAsWithProblem + "\n" + "Following exceptions occured : \n" + aCAExceptions, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); } }
//reading of one specific CA. public static bool ReadCA(string fileNameIn, CAMainWindowVM caMainWindowVMIn, ref CAGrid2DVM caGrid2DVMIn) { int aColumns = 0; int aRows = 0; var aIsMarkedList = new List <bool>(); var aCurrentCellStateList = new List <int>(); var aXList = new List <int>(); var aYList = new List <int>(); var aStateToColorCollection = new List <StateAndColor>(); CAGridInitializationMethodTypes?aCAGridCellInitializationMethod = null; string aCARuleFamilyString = null; string aCARule = null; bool aIsCA = false; XmlReader aXmlReader = null; try { aXmlReader = XmlReader.Create(fileNameIn); //reading all data connected to CA. while (aXmlReader.Read()) { if (aXmlReader.NodeType == XmlNodeType.Element) { switch (aXmlReader.Name) { case "ContentType": { string aContent = aXmlReader.ReadString(); if (aContent == "CellularAutomata") { aIsCA = true; } else if (aContent != "CellularAutomata") { return(false); } } break; case "CAGridCellInitializationMethod": { aCAGridCellInitializationMethod = (CAGridInitializationMethodTypes)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); } break; case "CARuleFamily": { aCARuleFamilyString = aXmlReader.ReadString(); } break; case "CARule": { aCARule = aXmlReader.ReadString(); } break; case "Name": { caGrid2DVMIn.CAName = aXmlReader.ReadString(); } break; case "Columns": { aColumns = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); if (aColumns < Constants.MinCAColumns) { throw new CAExplorerException("The value of columns must be higher than " + Constants.MinCAColumns.ToString(CultureInfo.InvariantCulture) + "!"); } } break; case "Rows": { aRows = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); if (aRows < Constants.MinCARows) { throw new CAExplorerException("The value of rows must be higher than " + Constants.MinCARows.ToString(CultureInfo.InvariantCulture) + "!"); } } break; case "GridThickness": { caGrid2DVMIn.GridThickness = (LineThickness)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); } break; case "SelFrameThickness": { caGrid2DVMIn.SelFrameThickness = (LineThickness)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); } break; case "CellObjectWidth": { caGrid2DVMIn.CellObjectWidth = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); if (caGrid2DVMIn.CellObjectWidth < Constants.MinCellSizeX) { throw new CAExplorerException("The value of cell object width must be higher than " + Constants.MinCellSizeX.ToString(CultureInfo.InvariantCulture) + "!"); } } break; case "CellObjectHeight": { caGrid2DVMIn.CellObjectHeight = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); if (caGrid2DVMIn.CellObjectHeight < Constants.MinCellSizeY) { throw new CAExplorerException("The value of cell object height must be higher " + Constants.MinCellSizeY.ToString(CultureInfo.InvariantCulture) + "!"); } } break; case "StateColorAssigning": { caGrid2DVMIn.StateColorAssigning = (StateColorAssigningType)Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); } break; case "SelectedGridColor": { caGrid2DVMIn.SelectedGridColor = ReadColor(aXmlReader, "SelectedGridColor"); } break; case "SelectedSelectionFrameColor": { caGrid2DVMIn.SelectedSelectionFrameColor = ReadColor(aXmlReader, "SelectedSelectionFrameColor"); } break; case "SelectedMarkingColor": { caGrid2DVMIn.SelectedMarkingColor = ReadColor(aXmlReader, "SelectedMarkingColor"); } break; case "SelectedMouseOverColor": { caGrid2DVMIn.SelectedMouseOverColor = ReadColor(aXmlReader, "SelectedMouseOverColor"); } break; case "SelectedBackgroundColor": { caGrid2DVMIn.SelectedBackgroundColor = ReadColor(aXmlReader, "SelectedBackgroundColor"); } break; case "SelectedStartInterpColor": { caGrid2DVMIn.SelectedStartInterpColor = ReadColor(aXmlReader, "SelectedStartInterpColor"); } break; case "SelectedEndInterpColor": { caGrid2DVMIn.SelectedEndInterpColor = ReadColor(aXmlReader, "SelectedEndInterpColor"); } break; case "Cell": { string aXString = null; string aYString = null; string aIsMarkedString = null; int aAttributeCount = aXmlReader.AttributeCount; if (aAttributeCount != 3) { throw new CAExplorerException("The cell element must have exactly 3 attributes!"); } aXString = aXmlReader.GetAttribute(0); aYString = aXmlReader.GetAttribute(1); aIsMarkedString = aXmlReader.GetAttribute(2); int aXConverted = Convert.ToInt32(aXString, CultureInfo.InvariantCulture); int aYConverted = Convert.ToInt32(aYString, CultureInfo.InvariantCulture); aXList.Add(aXConverted); aYList.Add(aYConverted); bool aIsMarked = Convert.ToBoolean(aIsMarkedString, CultureInfo.InvariantCulture); aIsMarkedList.Add(aIsMarked); int aCurrentCellState = Convert.ToInt32(aXmlReader.ReadString(), CultureInfo.InvariantCulture); aCurrentCellStateList.Add(aCurrentCellState); } break; case "StateColor": { int aNr = 0; if (aXmlReader.HasAttributes) { aNr = Convert.ToInt32(aXmlReader.GetAttribute(0), CultureInfo.InvariantCulture); } else { throw new CAExplorerException("The attribute id must be present in the State Color element!"); } Color aStateColor = ReadColor(aXmlReader, "StateColor"); aStateToColorCollection.Add(new StateAndColor(aNr, aStateColor)); } break; } } } } catch (Exception) { throw; } finally { if (aXmlReader != null) { aXmlReader.Close(); } } if (aIsCA == false) { return(false); } //storing of read data in the viewmodels and models. CARuleFamilies?aCARuleFamilyEnum = null; foreach (ComboBoxItem aCARuleFamilyEnumItem in ConstantLists.CARuleFamilyItems) { if (aCARuleFamilyEnumItem.ComboBoxString == aCARuleFamilyString) { aCARuleFamilyEnum = (CARuleFamilies)aCARuleFamilyEnumItem.ComboBoxId; break; } } ICARuleData aCARuleData = null; foreach (ICARuleData aCARuleDataItem in caMainWindowVMIn.CAMainWindowModel.ListOfCARules) { if (aCARuleDataItem.CARuleFamily == aCARuleFamilyEnum && aCARule == aCARuleDataItem.CARuleName) { aCARuleData = aCARuleDataItem; break; } } ICARuleFamily aCARuleFamily = CARuleFactory.CreateCARuleFamily((CARuleFamilies)aCARuleFamilyEnum, aCARuleData); aCARuleData.CARuleName = aCARule; ICAGridCellInitialization aCAGridCellInitialization = CAGridCellInitializationFactory.CreateCAGridCellInitialization((CAGridInitializationMethodTypes)aCAGridCellInitializationMethod); caGrid2DVMIn.CreateCells(aColumns, aRows, aCARuleFamily, aCAGridCellInitialization); caGrid2DVMIn.StateToColorCollection.Clear(); foreach (StateAndColor aStateAndColorItem in aStateToColorCollection) { caGrid2DVMIn.StateToColorCollection.Add(aStateAndColorItem); } for (int aCellNr = 0; aCellNr < aCurrentCellStateList.Count; aCellNr++) { caGrid2DVMIn.Cells[aCellNr].IsMarked = aIsMarkedList[aCellNr]; caGrid2DVMIn.Cells[aCellNr].CellModel.CurrentCellState = aCurrentCellStateList[aCellNr]; caGrid2DVMIn.Cells[aCellNr].CellModel.X = aXList[aCellNr]; caGrid2DVMIn.Cells[aCellNr].CellModel.Y = aYList[aCellNr]; } return(true); }
//reading of configuration file, here are read default colors. public static void ReadCAMainWindowData(string fileNameIn, CAMainWindowVM caMainWindowVMIn) { XmlDocument aXmlDocument = new XmlDocument(); try { aXmlDocument.Load(fileNameIn); XmlNode aXmlNodeSelectedDefaultGridColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultGridColor"); if (aXmlNodeSelectedDefaultGridColor != null) { caMainWindowVMIn.SelectedDefaultGridColor = ReadColor(aXmlNodeSelectedDefaultGridColor); } XmlNode aXmlNodeSelectedDefaultSelectionFrameColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultSelectionFrameColor"); if (aXmlNodeSelectedDefaultSelectionFrameColor != null) { caMainWindowVMIn.SelectedDefaultSelectionFrameColor = ReadColor(aXmlNodeSelectedDefaultSelectionFrameColor); } XmlNode aXmlNodeSelectedDefaultMarkingColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultMarkingColor"); if (aXmlNodeSelectedDefaultMarkingColor != null) { caMainWindowVMIn.SelectedDefaultMarkingColor = ReadColor(aXmlNodeSelectedDefaultMarkingColor); } XmlNode aXmlNodeSelectedDefaultMouseOverColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultMouseOverColor"); if (aXmlNodeSelectedDefaultMouseOverColor != null) { caMainWindowVMIn.SelectedDefaultMouseOverColor = ReadColor(aXmlNodeSelectedDefaultMouseOverColor); } XmlNode aXmlNodeSelectedDefaultBackgroundColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultBackgroundColor"); if (aXmlNodeSelectedDefaultBackgroundColor != null) { caMainWindowVMIn.SelectedDefaultBackgroundColor = ReadColor(aXmlNodeSelectedDefaultBackgroundColor); } XmlNode aXmlNodeSelectedDefaultStartInterpColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultStartInterpColor"); if (aXmlNodeSelectedDefaultStartInterpColor != null) { caMainWindowVMIn.SelectedDefaultStartInterpColor = ReadColor(aXmlNodeSelectedDefaultStartInterpColor); } XmlNode aXmlNodeSelectedDefaultEndInterpColor = aXmlDocument.SelectSingleNode("/CAMainWindowData/SelectedDefaultEndInterpColor"); if (aXmlNodeSelectedDefaultEndInterpColor != null) { caMainWindowVMIn.SelectedDefaultEndInterpColor = ReadColor(aXmlNodeSelectedDefaultEndInterpColor); } XmlNodeList aDefaultStateColorsCollection = aXmlDocument.SelectNodes("/CAMainWindowData/DefaultStateColor"); caMainWindowVMIn.DefaultStateColorsCollection.Clear(); foreach (XmlNode aXmlNode in aDefaultStateColorsCollection) { int aId = 0; Color aColor; if (aXmlNode.Attributes.Count == 1) { aId = Convert.ToInt32(aXmlNode.Attributes[0].Value, CultureInfo.InvariantCulture); } else { throw new CAExplorerException("The attribute id must be present in Default State Color element!"); } aColor = ReadColor(aXmlNode); caMainWindowVMIn.DefaultStateColorsCollection.Add(new StateAndColor(aId, aColor)); } } catch (Exception ex) { if (ex is XPathException || ex is ArgumentException || ex is FormatException || ex is OverflowException || ex is PathTooLongException || ex is XmlException || ex is DirectoryNotFoundException || ex is IOException || ex is FileNotFoundException || ex is UnauthorizedAccessException || ex is System.Security.SecurityException || ex is CAExplorerException) { DialogMediator.ShowMessageBox(null, "Exception during reading CA Explorer Configuration", "Problem with reading CA Explorer configuration : \n" + fileNameIn + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); DialogMediator.ShowMessageBox(null, "Loading default backup configuration", "The default backup configuration will be loaded ! : \n", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); caMainWindowVMIn.SelectedDefaultGridColor = DefaultBackupValues.DefaultBackupGridColor; caMainWindowVMIn.SelectedDefaultSelectionFrameColor = DefaultBackupValues.DefaultBackupSelectionFrameColor; caMainWindowVMIn.SelectedDefaultMarkingColor = DefaultBackupValues.DefaultBackupMarkingColor; caMainWindowVMIn.SelectedDefaultMouseOverColor = DefaultBackupValues.DefaultBackupMouseOverColor; caMainWindowVMIn.SelectedDefaultBackgroundColor = DefaultBackupValues.DefaultBackupBackgroundColor; caMainWindowVMIn.SelectedDefaultStartInterpColor = DefaultBackupValues.DefaultBackupStartInterpColor; caMainWindowVMIn.SelectedDefaultEndInterpColor = DefaultBackupValues.DefaultBackupEndInterpColor; foreach (StateAndColor aStateAndColor in DefaultBackupValues.DefaultBackupStateColors) { caMainWindowVMIn.DefaultStateColorsCollection.Add(aStateAndColor); } } else { throw; } } }
//all available CAs are read here. public static void ReadAllAvailableCAs(CAMainWindowVM caMainWindowVMIn) { string[] aAvailableCAs = null; try { aAvailableCAs = Directory.GetFiles(@".\SavedCA\", "*.xml"); } catch (Exception ex) { if (ex is IOException || ex is UnauthorizedAccessException || ex is PathTooLongException || ex is DirectoryNotFoundException) { DialogMediator.ShowMessageBox(null, "Exception during reading CAs", "Problem with reading files from directory \\SavedCA\\ \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } else { throw; } } if (aAvailableCAs != null) { string aCAsWithProblem = null; string aCAExceptions = null; foreach (string aAvailableCAItem in aAvailableCAs) { try { CAGrid2DVM aCAGrid2DVM = caMainWindowVMIn.CreateNewEmptyCA(); bool aWasOK = CAGrid2DRW.ReadCA(aAvailableCAItem, caMainWindowVMIn, ref aCAGrid2DVM); if (aWasOK == true) { caMainWindowVMIn.AddNewCA(aCAGrid2DVM); } } catch (Exception ex) { if (ex is FileNotFoundException || ex is System.Security.SecurityException || ex is UriFormatException || ex is XmlException || ex is FormatException || ex is OverflowException || ex is ArgumentOutOfRangeException || ex is NullReferenceException || ex is CAExplorerException || ex is InvalidOperationException) { aCAsWithProblem += aAvailableCAItem + "\n"; aCAExceptions += ex.Message + "\n"; } else { throw; } } } if (aCAsWithProblem != null) { DialogMediator.ShowMessageBox(null, "Exception during reading CA", "Problem with reading following CA files : \n" + aCAsWithProblem + "\n" + "Following exceptions occured : \n" + aCAExceptions, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); } } }