Пример #1
0
        public object Visit(Scenario n)
        {
            var root = new XElement("Scenario");
            root.Add(new XAttribute("Title", NullCheck(n.Title)));
            root.Add(new XAttribute("Description", NullCheck(n.Description)));
            root.Add(new XAttribute("Author", NullCheck(n.Author)));
            root.Add(new XAttribute("CreationDate", n.CrationDate));
            root.Add(new XAttribute("Rating", n.Rating));

            var inputElements = this.SaveInputs(n);
            if (inputElements != null) root.Add(inputElements);

            var intermediateElements = this.SaveIntermediates(n);
            if (intermediateElements != null) root.Add(intermediateElements);

            var resultElements = this.SaveResults(n);
            if (resultElements != null) root.Add(resultElements);

            return root;
        }
Пример #2
0
        private XElement SaveSanityValueCells(Scenario n)
        {
            var root = new XElement("sanityValueCells");

            foreach (var result in n.SanityValueCells)
            {
                root.Add(result.Accept(this) as XElement);
            }

            return root;
        }
Пример #3
0
        private XElement SaveResults(Scenario n)
        {
            var root = new XElement("Results");

            foreach (var result in n.Results)
            {
                root.Add(result.Accept(this) as XElement);
            }

            return root;
        }
Пример #4
0
        private XElement SaveIntermediates(Scenario n)
        {
            var root = new XElement("Intermediates");

            foreach (var intermediate in n.Intermediates)
            {
                root.Add(intermediate.Accept(this) as XElement);
            }

            return root;
        }
Пример #5
0
        private XElement SaveInputs(Scenario n)
        {
            var root = new XElement("Inputs");

            foreach (var input in n.Inputs)
            {
                root.Add(input.Accept(this) as XElement);
            }

            return root;
        }
Пример #6
0
        public void Start(Core.WorkbookModel wb)
        {
            if (this.newScenario != null) return;
            lock (syncScenario)
            {
                if (this.newScenario != null) return;
                this.newScenario = new Scenario()
                    {
                        Title = "Untiteled Scenario - " + DateTime.Now.ToString(),
                        CrationDate = DateTime.Now,
                        Author = this.GetDocumentProperty(wb, "Last Author")
                    };

            }
            this.workbook = wb.Workbook;

            var workingList = wb.InputCells.Union(wb.IntermediateCells).Union(wb.OutputCells).ToList();

            //sort working list column first
            #region sort

            workingList.Sort(delegate(Core.Cell x, Core.Cell y)
            {
                //sort by worksheet
                var xSheet = workbook.Sheets[Cells.CellManager.Instance.ParseWorksheetName(x.Location)] as Worksheet;
                var ySheet = workbook.Sheets[Cells.CellManager.Instance.ParseWorksheetName(x.Location)] as Worksheet;

                if (xSheet.Index < ySheet.Index)
                {
                    return -1;
                }
                else if (xSheet.Index > ySheet.Index)
                {
                    return 1;
                }
                else
                {
                    //sort by column
                    var xRange = xSheet.Range[Cells.CellManager.Instance.ParseCellLocation(x.Location)];
                    var yRange = ySheet.Range[Cells.CellManager.Instance.ParseCellLocation(y.Location)];

                    if (xRange.Column < yRange.Column)
                    {
                        return -1;
                    }
                    else if (xRange.Column > yRange.Column)
                    {
                        return 1;
                    }
                    else
                    {
                        //sort by row
                        if (xRange.Row < yRange.Row)
                        {
                            return -1;
                        }
                        else if (xRange.Row > yRange.Row)
                        {
                            return 1;
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }

            });

            #endregion

            CreateScenarioDataFieldContainer containerFirst = null;
            CreateScenarioDataFieldContainer containerBefore = null;
            foreach (var c in workingList)
            {
                //create cell data
                CellData cellData;

                #region create cell data
                if (c is Cells.InputCell)
                {
                    cellData = new InputCellData();
                    cellData.Location = c.Location;
                    cellData.SifLocation = c.SifLocation;
                    newScenario.Inputs.Add(cellData as InputCellData);
                }
                else if (c is Cells.IntermediateCell)
                {
                    cellData = new IntermediateCellData();
                    cellData.Location = c.Location;
                    cellData.SifLocation = c.SifLocation;
                    newScenario.Intermediates.Add(cellData as IntermediateCellData);
                }
                else if (c is Cells.OutputCell)
                {
                    cellData = new ResultCellData();
                    cellData.Location = c.Location;
                    cellData.SifLocation = c.SifLocation;
                    newScenario.Results.Add(cellData as ResultCellData);
                }
                else
                {
                    //abort
                    lock (syncScenario)
                    {
                        //clear this object
                        this.containers.Clear();
                        this.workbook = null;
                        this.newScenario = null;

                        return;
                    }
                }

                #endregion

                //get worksheet
                var currentWorksheet = workbook.Sheets[Cells.CellManager.Instance.ParseWorksheetName(c.Location)] as Worksheet;
                var vsto = Globals.Factory.GetVstoObject(currentWorksheet);

                //create container
                var container = new CreateScenarioDataFieldContainer();
                container.createScenarioDataField.DataContext = cellData;
                containers.Add(container);

                //register for focus handling
                #region focus handling

                if (c == workingList.First())
                {
                    containerFirst = container;
                }
                else if (containerBefore != null)
                {
                    containerBefore.createScenarioDataField.RegisterNextFocusField(container.createScenarioDataField);
                }
                containerBefore = container;

                #endregion

                //create control
                var control = vsto.Controls.AddControl(
                    container,
                    currentWorksheet.Range[Cells.CellManager.Instance.ParseCellLocation(c.Location)],
                    Guid.NewGuid().ToString());
                control.Placement = Microsoft.Office.Interop.Excel.XlPlacement.xlMove;
            }

            //set focus to first control
            if (containerFirst != null)
            {
                containerFirst.createScenarioDataField.SetFocus();
            }

        }
Пример #7
0
        public Scenario End()
        {
            if (this.newScenario == null) return null;

            //delete data contexts of the containers
            foreach (var c in this.containers)
            {
                c.createScenarioDataField.DataContext = null;
            }

            //destroy controls
            foreach (Worksheet ws in workbook.Worksheets)
            {
                var vsto = Globals.Factory.GetVstoObject(ws);
                for (int i = vsto.Controls.Count - 1; i >= 0; i--)
                {
                    var control = vsto.Controls[i];
                    if (control.GetType() == typeof(CreateScenarioDataFieldContainer))
                        vsto.Controls.Remove(control);
                }
            }

            // delete cell datas with out values
            #region delete cell datas
            //inputs
            var removeInputs = (from q in newScenario.Inputs
                                where q.Content == null
                                select q).ToList();
            foreach (var input in removeInputs)
            {
                newScenario.Inputs.Remove(input);
            }

            //intermediates
            var removeIntermediates = (from q in newScenario.Intermediates
                                       where q.Content == null
                                       select q).ToList();
            foreach (var intermediate in removeIntermediates)
            {
                newScenario.Intermediates.Remove(intermediate);
            }

            //results
            var removeResults = (from q in newScenario.Results
                                 where q.Content == null
                                 select q).ToList();
            foreach (var result in removeResults)
            {
                newScenario.Results.Remove(result);
            }

            #endregion

            // end up and clear
            var resultScenario = this.newScenario;

            lock (syncScenario)
            {
                //clear this object
                this.containers.Clear();
                this.workbook = null;
                this.newScenario = null;

                if (resultScenario.Inputs.Count == 0 &&
                    resultScenario.Intermediates.Count == 0 &&
                    resultScenario.Results.Count == 0)
                {
                    return null;
                }
                else
                {
                    return resultScenario;
                }
            }
        }