public MainPresentation(ExpenseRepository expenseRepository, CategoryRepository categoryRepository)
        {
            _expenseRepository         = expenseRepository;
            _categoryRepository        = categoryRepository;
            _categorizer               = new Categorizer(categoryRepository);
            _newCategoryCommand        = new NewCategoryCommand(this);
            _editCategoryCommand       = new EditCategoryCommand(this);
            _deleteCategoryCommand     = new DeleteCategoryCommand(this);
            _newExpenseCategoryCommand = new NewExpenseCategoryCommand(this);

            _results = new ResultsPresentation(_categorizer, _expenseRepository);
            Categories.CollectionChanged += CategoriesChanged;
        }
Example #2
0
        /// <summary>
        /// Action that is performed, when the "Calculate" button is clicked.
        /// Input file is read, calculation is performed, result is saved to output file.
        /// </summary>
        /// <param name="sender">Reference to the event sender.</param>
        /// <param name="e">Reference to the event arguments.</param>
        private void calculateBtn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            //disable button, so that it can not be clicked, while calculations are still performed
            btn.Enabled = false;

            loggerTextBox.Clear();

            //read i/o paths
            string inputPath  = inputFileTextBox.Text;
            string outputPath = outputFileTextBox.Text;

            //read number of selected threads
            int threadNum = Int32.Parse(threadNumComboBox.Text);

            //set default library to ASM
            LibraryType lib = LibraryType.ASM;

            //check if C++ and Asm libraries could be loaded
            if (!ComplexNumUtils.areLibrariesPresent())
            {
                //show error dialog and break calculation
                alertLibsNotPresent();
                resetCalculationEvent(btn);
                return;
            }

            //check whether the c++ library is chosen
            if (cppRadioBtn.Checked)
            {
                lib = LibraryType.CPP;
            }

            loggerTextBox.AppendText("======================================\n");
            loggerTextBox.AppendText("Loading input data...\n");

            //buffer for file input
            string inputJson;

            //buffer destined input representation
            List <ComplexAlgebraic> inputs = null;

            //input file size in MB
            double fileLenMB = 0;

            try
            {
                //check input file size in MB
                fileLenMB = new System.IO.FileInfo(inputPath).Length / 1000000.0;

                //check if the file is large
                if (fileLenMB > 20)
                {
                    //about 800 000 roots to calculate
                    //file is too large, to be processed
                    if (fileLenMB >= 30)
                    {
                        //show error dialog and break calculation
                        alertFileTooLarge();
                        resetCalculationEvent(btn);
                        return;
                    }

                    //show alert that the file is dangerously large
                    //prompt the user to decide, whether calculation shoul be continued or not
                    DialogResult result = alertFileSize();

                    //User chose not to continue the calculation
                    if (result == DialogResult.No)
                    {
                        //show error dialog and break calculation
                        resetCalculationEvent(btn);
                        return;
                    }
                }

                //read file data
                inputJson = File.ReadAllText(inputPath);

                //deserialize json data
                inputs = JsonConvert.DeserializeObject <List <ComplexAlgebraic> >(inputJson);
            }
            //catch exceptions which occur while deserializing input
            catch (System.ArgumentException)
            {
                //show error dialog and break calculation
                alertInvalidInputFile();
                resetCalculationEvent(btn);
                return;
            }
            catch (Newtonsoft.Json.JsonReaderException)
            {
                //show error dialog and break calculation
                alertInvalidJSON();
                resetCalculationEvent(btn);
                return;
            }
            catch (Newtonsoft.Json.JsonSerializationException)
            {
                //show error dialog and break calculation
                alertInvalidJSON();
                resetCalculationEvent(btn);
                return;
            }

            loggerTextBox.AppendText("Input data has been loaded successfully.\n");
            loggerTextBox.AppendText("Number of input entries: " + inputs.Count + "\n");
            loggerTextBox.AppendText("Input file size (MB): " + fileLenMB + "\n");

            //perform the calculations
            ResultsPresentation resultsPresentation = ComplexNumUtils.calculateRoots(
                inputs, threadNum, lib, loggerTextBox);

            loggerTextBox.AppendText("Saving results...\n");

            //serialize results
            string resultsJson = JsonConvert.SerializeObject(resultsPresentation, Formatting.Indented);

            //write results to file
            File.WriteAllText(outputPath, resultsJson);

            loggerTextBox.AppendText("Results have been saved to output file successfully.\n");
            loggerTextBox.AppendText("======================================\n");

            //wait until file is completely written and closed
            FileInfo fi = new FileInfo(outputPath);

            while (IsFileLocked(fi))
            {
            }

            //enable button, make new calculation possible
            btn.Enabled = true;
        }