예제 #1
0
        // Imports the contents of an XML file and adds them to the details-listview
        private void ImportFromXML(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Title  = "Open an XML file";
                dialog.Filter = "XML files|*.xml";
                dialog.ShowDialog();

                if (dialog.FileName != "")
                {
                    try
                    {
                        recipeManager.XMLDeSerialize(dialog.FileName);
                        addAllRecipesToListView();
                        currentFileType = CurrentFileType.XML;
                        currentFileName = dialog.FileName;
                    }
                    catch (NullReferenceException error)
                    {
                        MessageBox.Show("Failed to load XML-file. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    catch (InvalidOperationException ex)
                    {
                        MessageBox.Show("Failed to load XML-file. Reason: " + ex.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
예제 #2
0
        // Exports recipe objects in recipemanager to xml file
        private void ExportToXML(object sender, EventArgs e)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.DefaultExt   = "xml";
                dialog.AddExtension = true;
                dialog.Title        = "Export as XML file";
                dialog.Filter       = "XML files|*.xml";
                dialog.ShowDialog();

                if (dialog.FileName != "")
                {
                    try
                    {
                        recipeManager.XMLSerialize(dialog.FileName);
                        currentFileType = CurrentFileType.XML;
                        currentFileName = dialog.FileName;
                    }
                    catch (Exception error)
                    {
                        MessageBox.Show("Failed to export XML-file. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                unsavedChanges = false;
            }
        }
예제 #3
0
 // Resets some specific elements
 private void Reset()
 {
     animalManager.Reset();
     recipeManager.Reset();
     listViewDetails.Items.Clear();
     listViewAnimals.Items.Clear();
     currentFileType = CurrentFileType.None;
     currentFileName = "undefined";
 }
예제 #4
0
        // Imports the contents of a binary or a text file and adds them to the details-listview
        private void openFile(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                // The sender object keeps track of which menu option was clicked (under Open)
                if (sender.ToString() == "Binary File")
                {
                    dialog.Title    = "Open binary file";
                    dialog.Filter   = "Binary files|*.bin";
                    currentFileType = CurrentFileType.Binary;
                }
                // If the open text file option was clicked
                else if (sender.ToString() == "Text File")
                {
                    dialog.Title    = "Open text file";
                    dialog.Filter   = "JSON files|*.json";
                    currentFileType = CurrentFileType.Text;
                }

                dialog.ShowDialog();
                if (dialog.FileName.Trim() != "")
                {
                    // binary deserialize necessary
                    if (sender.ToString() == "Binary File")
                    {
                        try
                        {
                            animalManager.BinaryDeSerialize(dialog.FileName);
                            addAllAnimalsToListView();
                        }
                        catch (SerializationException error)
                        {
                            MessageBox.Show("Failed to deserialize. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else if (sender.ToString() == "Text File")
                    {
                        try
                        {
                            animalManager.JSONDeSerialize(dialog.FileName);
                            addAllAnimalsToListView();
                        }
                        catch (JsonSerializationException error)
                        {
                            MessageBox.Show("Failed to deserialize. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        catch (JsonReaderException error)
                        {
                            MessageBox.Show("Failed to deserialize. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    currentFileName = dialog.FileName;
                }
            }
        }
예제 #5
0
        // saves the animal objects to a file. Either as binary or plain text
        private void SaveToFile(string format)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                // if the user clicked save as binary file
                if (format == "Binary File")
                {
                    dialog.Title  = "Save as binary file";
                    dialog.Filter = "Binary files|*.bin";
                }
                // if the user clicked save as text file
                else if (format == "Text File")
                {
                    dialog.DefaultExt   = "json";
                    dialog.AddExtension = true;
                    dialog.Title        = "Save as text file";
                    dialog.Filter       = "JSON files|*.json";
                }
                dialog.ShowDialog();

                if (dialog.FileName.Trim() != "")
                {
                    if (format == "Binary File")
                    {
                        try
                        {
                            animalManager.BinarySerialize(dialog.FileName);
                            currentFileType = CurrentFileType.Binary;
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show("Could not serialize file in binary format. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    else if (format == "Text File")
                    {
                        try
                        {
                            animalManager.JSONSerialize(dialog.FileName);
                            currentFileType = CurrentFileType.Text;
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show("Could not serialize file to JSON format. Reason: " + error.Message, "Something went wrong!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    unsavedChanges  = false;
                    currentFileName = dialog.FileName;
                }
            }
        }