예제 #1
0
        // Function to update the Dependencies
        private void UpdateDependencies(SpreadsheetCell cell)
        {
            // Create a new tree with current variables
            ExpTree tree = new ExpTree(VarTable);


            if (Dependencies.ContainsKey(cell))
            {
                // For each of the cells dependent on cell, update dependencies
                foreach (SpreadsheetCell myCell in Dependencies[cell])
                {
                    tree.SetExp(myCell.Text.Substring(1));
                    myCell.SetValue(tree.Eval().ToString());
                    CellPropertyChanged(myCell, new PropertyChangedEventArgs("Value"));
                    UpdateVars(myCell);
                    UpdateDependencies(myCell);
                }
            }
        }
예제 #2
0
        // Create SheetPropertyChanged method to raise event
        //
        private void SheetPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            ExpTree tree     = new ExpTree(VarTable);
            var     currCell = (SpreadsheetCell)sender;

            // Check if cell is a valid input. Will get finish in the future
            if (!IsValid(currCell, Dependencies))
            {
                currCell.SetValue("#REF");
                return;
            }

            if (currCell.Text != null && currCell.Text[0] == '=')
            {
                // Create a new expression tree if the cell text starts with =
                tree.SetExp(currCell.Text.Substring(1));

                // Set the cell value to the evaluation of the expression
                currCell.SetValue(tree.Eval().ToString());

                // If the list of variables inside this cell is not null then
                // Add each variable to the dependencies dictionary and
                // set the current cell as being dependent to that cell
                if (tree.VarsForDeps != null)
                {
                    foreach (string s in tree.VarsForDeps)
                    {
                        if (!Dependencies.ContainsKey(GetCell(s)))
                        {
                            Dependencies.Add((GetCell(s)), new List <Cell>());
                        }
                        Dependencies[GetCell(s)].Add(currCell);
                    }
                }
            }
            else
            {
                // If currCell doesn't start with '=', we set the value of the cell to the text that was entered
                currCell.SetValue(currCell.Text);

                // If variable is not currently in the variable table then
                // We add the cell name and its contents to the variable table
                string temp = CoordToName(currCell.ColumnIndex, currCell.RowIndex);
                if (!(VarTable.ContainsKey(temp)))
                {
                    VarTable.Add(temp, Convert.ToDouble(currCell.Value));
                }


                // If a cell got its value changed to a number, then it is no longer dependent on other cells
                // The foreach loops go through each cell in the dependencies dictionary
                // And make sure that the cell is no longer being written as being dependent
                // On another cell
                Dictionary <Cell, List <Cell> > NewDeps = new Dictionary <Cell, List <Cell> >();
                foreach (KeyValuePair <Cell, List <Cell> > cells in Dependencies)
                {
                    foreach (Cell cell in cells.Value)
                    {
                        if (currCell != cell)
                        {
                            if (!NewDeps.ContainsKey(cells.Key))
                            {
                                NewDeps.Add(cells.Key, new List <Cell>());
                            }
                            NewDeps[cells.Key].Add(cell);
                        }
                    }
                }
                // Update the dependencies dictionary.
                Dependencies = NewDeps;
            }

            // Update variables, dependencies, and call CellPropertyChanged.
            UpdateVars(currCell);
            UpdateDependencies(currCell);
            CellPropertyChanged(sender, e);
        }