//setting state colors for specific CA public void SetCAColorsInSelectedCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { try { mySelectedCAGrid2DViewModel.StopTimer(); //instantiating of SetCAColors viewmodel var aSetCAColorsVM = new SetCAColorsVM(); bool?aDialogResult = null; //copying data from CAGrid viewmodel to SetCAColors viewmodel. CopyFromCAGrid2DVM_To_SetCAColorsVM(mySelectedCAGrid2DViewModel, ref aSetCAColorsVM); //showing of SetCAColors dialog aDialogResult = DialogMediator.ShowModalDialog("Set CA colors", aSetCAColorsVM, this); if (aDialogResult == true) { //copying data from SetCAColors viewmodel to CAGrid2D viewmodel CopyFromSetCAColorsVM_To_CAGrid2DVM(aSetCAColorsVM, ref mySelectedCAGrid2DViewModel); //redrawing CA after CA colors modification mySelectedCAGrid2DViewModel.RedrawGrid(); } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during showing Set State Colors dialog", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } } }
//saving specific CA To file public void SaveCAToFile(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { bool?aDialogResult = null; string aDefaultExtension = ".xml"; string aFileExtensions = "Xml files (.xml)|*.xml"; string aFileName = null; aDialogResult = DialogMediator.ShowSaveFileDialog(this, aFileExtensions, aDefaultExtension, out aFileName); if (aDialogResult == true) { try { //writing specific CA to file. CAGrid2DRW.WriteCA(aFileName, mySelectedCAGrid2DViewModel); } catch (Exception ex) { if (ex is InvalidOperationException || ex is EncoderFallbackException || ex is ArgumentException || ex is UnauthorizedAccessException || ex is System.IO.DirectoryNotFoundException || ex is System.IO.IOException || ex is System.Security.SecurityException || ex is System.IO.PathTooLongException) { DialogMediator.ShowMessageBox(null, "Exception during writing of CA", "Problem with writing following CA file : \n" + aFileName + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } else { throw; } } } } }
//one specific CA is deleted here. public static void DeleteSpecificCA(CAGrid2DVM caGrid2DVMIn) { string aFilename = null; try { if (caGrid2DVMIn != null) { aFilename = @".\SavedCA\" + caGrid2DVMIn.CAName + ".xml"; File.Delete(aFilename); } } catch (Exception ex) { if (ex is UnauthorizedAccessException || ex is DirectoryNotFoundException || ex is IOException || ex is PathTooLongException) { DialogMediator.ShowMessageBox(null, "Exception during deleting CA", "Problem with deleting following CA : \n" + aFilename + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); } else { throw; } } }
//showing application settings dialog - default CA colors are defined here. public void ShowAppSettings(object messageIn) { try { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); } //instantiating of ApplicationSettings viewmodel var aAppSettingsVM = new AppSettingsVM(); bool?aDialogResult = null; //copying data - colors from this viewmodel (CAMainWindow) to Application settings viewmodel aAppSettingsVM.SelectedDefaultBackgroundColor = this.SelectedDefaultBackgroundColor; aAppSettingsVM.SelectedDefaultEndInterpColor = this.SelectedDefaultEndInterpColor; aAppSettingsVM.SelectedDefaultGridColor = this.SelectedDefaultGridColor; aAppSettingsVM.SelectedDefaultMarkingColor = this.SelectedDefaultMarkingColor; aAppSettingsVM.SelectedDefaultMouseOverColor = this.SelectedDefaultMouseOverColor; aAppSettingsVM.SelectedDefaultSelectionFrameColor = this.SelectedDefaultSelectionFrameColor; aAppSettingsVM.SelectedDefaultStartInterpColor = this.SelectedDefaultStartInterpColor; aAppSettingsVM.DefaultStateColorsCollection.Clear(); foreach (StateAndColor aStateAndColorItem in this.DefaultStateColorsCollection) { aAppSettingsVM.DefaultStateColorsCollection.Add(new StateAndColor(aStateAndColorItem)); } //showing Application settings viewmodel. aDialogResult = DialogMediator.ShowModalDialog("Application Settings", aAppSettingsVM, this); if (aDialogResult == true) { //copying data - colors from Application settings viewmodel to this (CAMainWindow) viewmodel. this.SelectedDefaultBackgroundColor = aAppSettingsVM.SelectedDefaultBackgroundColor; this.SelectedDefaultEndInterpColor = aAppSettingsVM.SelectedDefaultEndInterpColor; this.SelectedDefaultGridColor = aAppSettingsVM.SelectedDefaultGridColor; this.SelectedDefaultMarkingColor = aAppSettingsVM.SelectedDefaultMarkingColor; this.SelectedDefaultMouseOverColor = aAppSettingsVM.SelectedDefaultMouseOverColor; this.SelectedDefaultSelectionFrameColor = aAppSettingsVM.SelectedDefaultSelectionFrameColor; this.SelectedDefaultStartInterpColor = aAppSettingsVM.SelectedDefaultStartInterpColor; this.DefaultStateColorsCollection.Clear(); foreach (StateAndColor aStateAndColorItem in aAppSettingsVM.DefaultStateColorsCollection) { this.DefaultStateColorsCollection.Add(new StateAndColor(aStateAndColorItem)); } } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during showing Application setting dialog", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } }
//showing About dialog public void ShowAboutCAExplorer(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); } var aAboutCAExplorerVM = new AboutCAExplorerVM(); DialogMediator.ShowModalDialog("About CA Explorer", aAboutCAExplorerVM, this); }
//showing modeless Help dialog public void ShowViewHelp(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); } var aViewHelpVM = new ViewHelpVM(); DialogMediator.ShowModelessDialog("Help", aViewHelpVM, this); }
//add specific input CA to CA list. public void AddNewCA(CAGrid2DVM aCAGrid2DVM) { if (myGrid2DViewModelList.Count < Constants.MaxNrOfCellularAutomatons) { myGrid2DViewModelList.Add(aCAGrid2DVM); } else { DialogMediator.ShowMessageBox(this, "Add New CA", "There can be maximum " + Constants.MaxNrOfCellularAutomatons + " CAs loaded! \n" + "Delete some CA first, before creating new CA.", MessageBoxButton.OK, MessageBoxImage.Warning); } }
//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(); } }
//deleting selected CA from list and if file exists, then deleting file as well. public void DeleteSelectedCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); MessageBoxResult aMBR; aMBR = DialogMediator.ShowMessageBox(null, "Delete CA", "Do you really want to delete the selected CA", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (aMBR == MessageBoxResult.Yes) { for (int i = 0; i < myGrid2DViewModelList.Count; i++) { if (myGrid2DViewModelList[i] == mySelectedCAGrid2DViewModel) { CAMainWindowRW.DeleteSpecificCA(mySelectedCAGrid2DViewModel); mySelectedCAGrid2DViewModel.Dispose(); int aNewSelectedCAIndex = i + 1; // selected will be next CA after deleted if (i == (myGrid2DViewModelList.Count - 1)) { aNewSelectedCAIndex = (myGrid2DViewModelList.Count - 2); } if (i >= 1) { SelectedCAGrid2DViewModel = myGrid2DViewModelList[aNewSelectedCAIndex]; } else { SelectedCAGrid2DViewModel = null; } myGrid2DViewModelList.RemoveAt(i); break; } } } } }
//modifying the existing CA public void ModifySelectedCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { try { //stopping last selected CA before continuing with modification mySelectedCAGrid2DViewModel.StopTimer(); //instantiating Properties viewmodel for modification of CA. var aCAPropertiesDialogVM = new CAPropertiesDialogVM(myCAMainWindowModel.ListOfCARules, myGrid2DViewModelList); bool?aDialogResult = null; aCAPropertiesDialogVM.ReadOnlyCAProperty = true; //copying data from CA Grid viewmodel to Properties viewmodel. CopyFromCAGrid2DVM_To_CAPropertiesVM(mySelectedCAGrid2DViewModel, ref aCAPropertiesDialogVM); //showing Properties dialog aDialogResult = DialogMediator.ShowModalDialog("Modify CA Properties", aCAPropertiesDialogVM, this); if (aDialogResult == true) { //copying modified data from Properties viewmodel to CAGrid viewmodel. CopyFromCAPropertiesVM_To_CAGrid2DVM(aCAPropertiesDialogVM, false, ref mySelectedCAGrid2DViewModel); //redrawing CA after modification mySelectedCAGrid2DViewModel.RedrawGrid(); } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during modification of CA", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } } }
//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); } }
//loading specific CA from file public void LoadCAFromFile(object messageIn) { bool?aDialogResult = null; string aDefaultExtension = ".xml"; string aFileExtensions = "Xml files (.xml)|*.xml"; string aFileName = null; aDialogResult = DialogMediator.ShowOpenFileDialog(this, aFileExtensions, aDefaultExtension, out aFileName); if (aDialogResult == true) { var aCAGrid2DVM = new CAGrid2DVM(); try { //reading CA from specified file and adding it to list of CAs CAGrid2DRW.ReadCA(aFileName, this, ref aCAGrid2DVM); myGrid2DViewModelList.Add(aCAGrid2DVM); } catch (Exception ex) { if (ex is System.IO.FileNotFoundException || ex is System.Security.SecurityException || ex is UriFormatException || ex is System.Xml.XmlException || ex is FormatException || ex is OverflowException || ex is ArgumentOutOfRangeException || ex is NullReferenceException || ex is CAExplorerException) { DialogMediator.ShowMessageBox(null, "Exception during reading CA", "Problem with reading following CA file : \n" + aFileName + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } else { throw; } } } }
//loading background image from file. public void LoadBackgroundImageFromFile(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { bool?aDialogResult = null; string aDefaultExtension = ".jpg"; string aFileExtensions = "Images jpeg files (.jpg)|*.jpg|png files (.png)|*.png|gif files (.gif)|*.gif|bmp files (.bmp)|*.bmp"; string aFileName = null; aDialogResult = DialogMediator.ShowOpenFileDialog(this, aFileExtensions, aDefaultExtension, out aFileName); if (aDialogResult == true) { try { ImageBrush aCABackgroundImageBrush = new ImageBrush(); aCABackgroundImageBrush.ImageSource = new BitmapImage(new Uri(@aFileName, UriKind.Relative)); mySelectedCAGrid2DViewModel.CABackgroundImage = aCABackgroundImageBrush; } catch (Exception ex) { if (ex is OutOfMemoryException || ex is System.IO.FileNotFoundException || ex is ArgumentException || ex is UriFormatException) { DialogMediator.ShowMessageBox(null, "Exception during reading of background image", "Problem with reading of background image : \n" + aFileName + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } 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); } } }
//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; } } }
//creating and adding new CA public void CreateAndAddNewCA(object messageIn) { if (mySelectedCAGrid2DViewModel != null) { mySelectedCAGrid2DViewModel.StopTimer(); } if (myGrid2DViewModelList.Count < Constants.MaxNrOfCellularAutomatons) { try { //instantiating Properties viewmodel for configuration of new CA. var aCAPropertiesDialogVM = new CAPropertiesDialogVM(myCAMainWindowModel.ListOfCARules, myGrid2DViewModelList); bool?aDialogResult = null; var aCAGrid2DViewModel = new CAGrid2DVM(); aCAPropertiesDialogVM.ReadOnlyCAProperty = false; aCAPropertiesDialogVM.CAColumns = Constants.DefaultCAColumns; aCAPropertiesDialogVM.CARows = Constants.DefaultCARows; aCAPropertiesDialogVM.CACellSizeX = Constants.DefaultCellSizeX; aCAPropertiesDialogVM.CACellSizeY = Constants.DefaultCellSizeY; bool aCANameWasFound = true; int i = 0; string aNewCAName = null; //finding new default name of new CA, format is CA + increasing number do { aCANameWasFound = true; foreach (CAGrid2DVM aCAGrid2DVM in myGrid2DViewModelList) { if (aCAGrid2DVM.CAName == "CA" + i.ToString(CultureInfo.CurrentCulture)) { aCANameWasFound = false; break; } } if (aCANameWasFound == true) { aNewCAName = "CA" + i.ToString(CultureInfo.CurrentCulture); } i++; } while (aCANameWasFound != true); aCAPropertiesDialogVM.CAName = aNewCAName; //showing Properties dialog for configuration of new CA. aDialogResult = DialogMediator.ShowModalDialog("New CA Properties", aCAPropertiesDialogVM, this); if (aDialogResult == true) { //copying the inputted data from properties viewmodel to CAGrid viewmodel. CopyFromCAPropertiesVM_To_CAGrid2DVM(aCAPropertiesDialogVM, true, ref aCAGrid2DViewModel); //adding new CA to list myGrid2DViewModelList.Add(aCAGrid2DViewModel); //setting newly added CA as selected SelectedCAGrid2DViewModel = aCAGrid2DViewModel; } } catch (Exception ex) { DialogMediator.ShowMessageBox(null, "Exception during creating of CA", "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); throw; } } else { DialogMediator.ShowMessageBox(this, "Add New CA", "There can be maximum " + Constants.MaxNrOfCellularAutomatons + " CAs loaded! \n" + "Delete some CA first, before creating new CA.", MessageBoxButton.OK, MessageBoxImage.Warning); } }
public static void ReadCARules(string fileNameIn, ref IList <ICARuleData> caRuleListIn) { string aCARulesWithProblem = null; string aCARulesExceptions = null; XmlDocument aXmlDocument = new XmlDocument(); try { aXmlDocument.Load(fileNameIn); XmlNodeList aXmlNodeList = aXmlDocument.SelectNodes("/CARules/CARule"); foreach (XmlNode aXmlNode in aXmlNodeList) { string aCAName = null; string aCARuleFamilyString = null; try { //All data that specifies a rule is loaded here. IList <int> aBirthList = new List <int>(); IList <int> aSurvivalList = new List <int>(); XmlNode aXmlNodeName = aXmlNode.SelectSingleNode("./Name"); aCAName = aXmlNodeName.InnerText; XmlNode aXmlNodeRuleFamily = aXmlNode.SelectSingleNode("./CARuleFamily"); if (aXmlNodeRuleFamily == null) { throw new CAExplorerException("There was no rule family found in CA Rules configuration file!"); } aCARuleFamilyString = aXmlNodeRuleFamily.InnerText; XmlNodeList aXmlNodeListSurvival = aXmlNode.SelectNodes("./Survival"); foreach (XmlNode aXmlNodeSurvival in aXmlNodeListSurvival) { int aSurvival = Convert.ToInt32(aXmlNodeSurvival.InnerText, CultureInfo.InvariantCulture); aSurvivalList.Add(aSurvival); } XmlNodeList aXmlNodeListBirth = aXmlNode.SelectNodes("./Birth"); foreach (XmlNode aXmlNodeBirth in aXmlNodeListBirth) { int aBirth = Convert.ToInt32(aXmlNodeBirth.InnerText, CultureInfo.InvariantCulture); aBirthList.Add(aBirth); } XmlNode aXmlNodeThreshold = aXmlNode.SelectSingleNode("./Threshold"); int?aThreshold = null; if (aXmlNodeThreshold != null) { aThreshold = Convert.ToInt32(aXmlNodeThreshold.InnerText, CultureInfo.InvariantCulture); } if (aThreshold != null && aThreshold < 0) { throw new CAExplorerException("The value of Threshold cannot be lower than zero!"); } CARuleFamilies?aCARuleFamilyEnum = null; foreach (ComboBoxItem aCARuleFamilyItem in ConstantLists.CARuleFamilyItems) { if (aCARuleFamilyItem.ComboBoxString == aCARuleFamilyString) { aCARuleFamilyEnum = (CARuleFamilies)aCARuleFamilyItem.ComboBoxId; break; } } XmlNode aXmlNodeCountOfStates = aXmlNode.SelectSingleNode("./CountOfStates"); int?aCountOfStates = null; if (aXmlNodeCountOfStates != null) { aCountOfStates = Convert.ToInt32(aXmlNodeCountOfStates.InnerText, CultureInfo.InvariantCulture); } if (aCountOfStates != null && aCountOfStates < 0) { throw new CAExplorerException("The value of count of states cannot be lower than zero!"); } XmlNode aXmlNodeCANeighborhood = aXmlNode.SelectSingleNode("./CANeighborhood"); string aCANeighborhood = null; if (aXmlNodeCANeighborhood != null) { aCANeighborhood = aXmlNodeCANeighborhood.InnerText; } CANeighborhoodTypes?aCANeighborhoodEnum = null; foreach (ComboBoxItem aCANeighborhoodItem in ConstantLists.CANeighborhoodItems) { if (aCANeighborhoodItem.ComboBoxString == aCANeighborhood) { aCANeighborhoodEnum = (CANeighborhoodTypes)aCANeighborhoodItem.ComboBoxId; break; } } XmlNode aXmlNodeCANeighborhoodRange = aXmlNode.SelectSingleNode("./CANeighborhoodRange"); int?aCANeighborhoodRange = null; if (aXmlNodeCANeighborhoodRange != null) { aCANeighborhoodRange = Convert.ToInt32(aXmlNodeCANeighborhoodRange.InnerText, CultureInfo.InvariantCulture); } if (aCANeighborhoodRange == null) { throw new CAExplorerException("The value of CA neighborhood range must be specified!"); } else if (aCANeighborhoodRange <= 0) { throw new CAExplorerException("The value of CA neighborhood range must be higher than zero!"); } //creating new CARule data class that contains all data needed for specific rule. CARuleData aCARuleData = new CARuleData(); aCARuleData.CARuleName = aCAName; aCARuleData.CARuleFamily = (CARuleFamilies)aCARuleFamilyEnum; aCARuleData.Birth = aBirthList; aCARuleData.Survival = aSurvivalList; aCARuleData.CountOfStates = aCountOfStates; aCARuleData.Threshold = aThreshold; aCARuleData.CANeighborhoodType = (CANeighborhoodTypes)aCANeighborhoodEnum; aCARuleData.CANeighborhoodRange = (int)aCANeighborhoodRange; //adding new rule to rule list. caRuleListIn.Add(aCARuleData); } catch (Exception ex) { if (ex is XPathException || ex is FormatException || ex is OverflowException || ex is CAExplorerException) { aCARulesWithProblem += "Rule family : " + aCARuleFamilyString + " Rule : " + aCAName + "\n"; aCARulesExceptions += ex.Message + "\n"; } else { throw; } } } } 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 of available CA Rules", "Problem with reading of CA Explorer available rules : \n" + fileNameIn + "\n" + "Following exception occured : \n" + ex.Message, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); } else { throw; } } if (aCARulesWithProblem != null) { DialogMediator.ShowMessageBox(null, "Exception during reading of available CA Rules", "Problem with reading of CA Explorer available rules : \n" + aCARulesWithProblem + "\n" + "Following exception occured : \n" + aCARulesExceptions, System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Warning); } // if there was no rule found, asking user if backup rules should be created. if (caRuleListIn.Count == 0) { System.Windows.MessageBoxResult aMBResult = DialogMediator.ShowMessageBox(null, "Loading default 2 CA rules", "Should the default 2 backup CA rules be loaded ? (if no there will be no CA rules loaded!): \n", System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxImage.Warning); if (aMBResult == System.Windows.MessageBoxResult.Yes) { CARuleData aCARuleBackupData = new CARuleData(); aCARuleBackupData.CARuleName = "Conway's Life"; aCARuleBackupData.CARuleFamily = CARuleFamilies.Life; aCARuleBackupData.Birth = new List <int>() { 3 }; aCARuleBackupData.Survival = new List <int>() { 2, 3 }; aCARuleBackupData.CountOfStates = 2; aCARuleBackupData.CANeighborhoodType = CANeighborhoodTypes.Moore; aCARuleBackupData.CANeighborhoodRange = 1; caRuleListIn.Add(aCARuleBackupData); aCARuleBackupData = new CARuleData(); aCARuleBackupData.CARuleName = "Brian's Brain"; aCARuleBackupData.CARuleFamily = CARuleFamilies.Generations; aCARuleBackupData.Birth = new List <int>() { 2 }; aCARuleBackupData.Survival = new List <int>(); aCARuleBackupData.CountOfStates = 3; aCARuleBackupData.CANeighborhoodType = CANeighborhoodTypes.Moore; aCARuleBackupData.CANeighborhoodRange = 1; caRuleListIn.Add(aCARuleBackupData); } } }