T
Tony Johansson
Helle!
Here is some code and all of these is locaded in the GUI class and I think
it belongs there because it it very tightly connected
to the OpenFileDialog and SaveFileDialog.
So I just want your opinion if you think that some of this should be moved
out og the GUI class.
I would say that if some of this code would be moved to the controller class
AnimalManager it would be a bad solution
because we still have the OpenFileDialog and SaveFileDialog which require
ShowDialog.
This event handler is called when you click the Open in the menu
private void MenuFileOpen_Click(object sender, EventArgs e)
{
try
{
openFileDialog.AddExtension = true;
openFileDialog.DefaultExt = "Data";
openFileDialog.Title = "Open File";
openFileDialog.ValidateNames = true;
openFileDialog.InitialDirectory =
Directory.GetCurrentDirectory();
openFileDialog.Filter = "Data files (*.data)|*.data|All
Files|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Utför binär Deserialisering av alla animal i filen
animalManager.AllAnimals =
BinarySerialization.BinaryFileDeserialize<Animal[]>(openFileDialog.FileName).ToList
();
UpdateGUI();
InitializeGUI();
}
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
This event handler is called when you click the Save in the menu
private void MenuFileSave_Click(object sender, EventArgs e)
{
if (animalManager.Animals.Count == 0)
{
return;
}
try
{
if (chosenFileName == string.Empty)
{
SaveToFile();
}
else
{
BinarySerialization.BinaryFileSerialize(animalManager.Animals.ToArray(),
chosenFileName);
//Indikera att data är sparad
animalManager.DataSaved = true;
}
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
//This helper method is called from MenuFileSave_Click and
MenuFileSaveAs_Click
private void SaveToFile()
{
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = "Data";
saveFileDialog.Title = "Save File";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
saveFileDialog.Filter = "Data files (*.data)|*.data|All Files|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
chosenFileName = saveFileDialog.FileName;
BinarySerialization.BinaryFileSerialize(animalManager.Animals.ToArray(),
chosenFileName);
//Indikera att data är sparad
animalManager.DataSaved = true;
}
}
This event handler is called when you click the SaveAs in the menu
private void MenuFileSaveAs_Click(object sender, EventArgs e)
{
if (animalManager.Animals.Count == 0)
{
return;
}
try
{
SaveToFile();
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
private void MenuFileNew_Click(object sender, EventArgs e)
{
//Om collection är tom gör ingenting
if (animalManager.Animals.Count == 0)
return;
//On collection inte är sparad skrävs ett svar om du ska göra reset
eller inte
if (!animalManager.DataSaved)
{
DialogResult result = MessageBox.Show(this, "The Contents has
not been saved. Do you want to reset anyway: ", "Confirmation on resetting",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (DialogResult.No == result)
{
return;
}
}
animalManager.Reset();
UpdateGUI();
InitializeGUI();
}
//Tony
Here is some code and all of these is locaded in the GUI class and I think
it belongs there because it it very tightly connected
to the OpenFileDialog and SaveFileDialog.
So I just want your opinion if you think that some of this should be moved
out og the GUI class.
I would say that if some of this code would be moved to the controller class
AnimalManager it would be a bad solution
because we still have the OpenFileDialog and SaveFileDialog which require
ShowDialog.
This event handler is called when you click the Open in the menu
private void MenuFileOpen_Click(object sender, EventArgs e)
{
try
{
openFileDialog.AddExtension = true;
openFileDialog.DefaultExt = "Data";
openFileDialog.Title = "Open File";
openFileDialog.ValidateNames = true;
openFileDialog.InitialDirectory =
Directory.GetCurrentDirectory();
openFileDialog.Filter = "Data files (*.data)|*.data|All
Files|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Utför binär Deserialisering av alla animal i filen
animalManager.AllAnimals =
BinarySerialization.BinaryFileDeserialize<Animal[]>(openFileDialog.FileName).ToList
();
UpdateGUI();
InitializeGUI();
}
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
This event handler is called when you click the Save in the menu
private void MenuFileSave_Click(object sender, EventArgs e)
{
if (animalManager.Animals.Count == 0)
{
return;
}
try
{
if (chosenFileName == string.Empty)
{
SaveToFile();
}
else
{
BinarySerialization.BinaryFileSerialize(animalManager.Animals.ToArray(),
chosenFileName);
//Indikera att data är sparad
animalManager.DataSaved = true;
}
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
//This helper method is called from MenuFileSave_Click and
MenuFileSaveAs_Click
private void SaveToFile()
{
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = "Data";
saveFileDialog.Title = "Save File";
saveFileDialog.ValidateNames = true;
saveFileDialog.OverwritePrompt = true;
saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory();
saveFileDialog.Filter = "Data files (*.data)|*.data|All Files|*.*";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
chosenFileName = saveFileDialog.FileName;
BinarySerialization.BinaryFileSerialize(animalManager.Animals.ToArray(),
chosenFileName);
//Indikera att data är sparad
animalManager.DataSaved = true;
}
}
This event handler is called when you click the SaveAs in the menu
private void MenuFileSaveAs_Click(object sender, EventArgs e)
{
if (animalManager.Animals.Count == 0)
{
return;
}
try
{
SaveToFile();
}
catch (IOException ex)
{
MessageBox.Show("An IOException occured " + ex.Message);
}
catch (Exception ex)
{
MessageBox.Show("An Exception occured " + ex.Message);
}
}
private void MenuFileNew_Click(object sender, EventArgs e)
{
//Om collection är tom gör ingenting
if (animalManager.Animals.Count == 0)
return;
//On collection inte är sparad skrävs ett svar om du ska göra reset
eller inte
if (!animalManager.DataSaved)
{
DialogResult result = MessageBox.Show(this, "The Contents has
not been saved. Do you want to reset anyway: ", "Confirmation on resetting",
MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (DialogResult.No == result)
{
return;
}
}
animalManager.Reset();
UpdateGUI();
InitializeGUI();
}
//Tony